peledies / homebridge-ambient-weather-sensors

Apache License 2.0
2 stars 2 forks source link

Plugin stops reporting after a few hours and requires reboot #9

Open cytan299 opened 9 months ago

cytan299 commented 9 months ago

Describe The Bug:

After a few hours of running, the sensors stop reporting because the plugin is unable to read the JSON data. See the line from the log below.

A restart of the plugin fixes the problem for a while.

To Reproduce:

Just wait for a few hours after reboot.

Expected behavior:

This problem has happened ever since I've used this plugin. Also once in a while, after rebooting, the plugin fails to read the JSON file properly as well.

Logs:

Show the Homebridge logs here, remove any sensitive information.
[23/01/2024, 16:41:38] [AmbientWeatherSensors] ERROR: invalid json response body at https://rt.ambientweather.net/v1/devices?applicationKey=xxxxxx&apiKey=xxxxx reason: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
[23/01/2024, 16:41:38] [AmbientWeatherSensors] ERROR: Cannot read properties of undefined (reading 'map')

Plugin Config:


{
    "name": "AmbientWeatherSensors",
    "apiKey": "xxx",
    "applicationKey": "xxx",
    "temperatureSensors": true,
    "humiditySensors": true,
    "solarRadiationSensors": false,
    "_bridge": {
        "username": "xx:xx:xx:xx:xx:xx",
        "port": 41348
    },
    "platform": "AmbientWeatherSensors"
}

Screenshots:

Environment:

WotAGitUR commented 9 months ago

I have a similar issue. [edit] This is the same environment as previously but now running plugin V1.2.2 I get the same type of error, here

[25/01/2024, 12:46:06] [Our Weather] ERROR: invalid json response body at https://rt.ambientweather.net/v1/devices?applicationKey=xxxxx&apiKey=xxxxx reason: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
[25/01/2024, 12:46:06] TypeError: Cannot read properties of undefined (reading 'filter')
    at TemperatureAccessory.updateData (/var/lib/homebridge/node_modules/homebridge-ambient-weather-sensors/src/temperatureAccessory.ts:52:28)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
[25/01/2024, 12:46:06] Got SIGTERM, shutting down Homebridge...
[25/01/2024, 12:46:28] [Our Weather] Initializing AmbientWeatherSensors platform...
[25/01/2024, 12:46:28] [Our Weather] Loading accessory from cache: 48:3F:DA:xx:xx:xx-tempinf
[25/01/2024, 12:46:28] [Our Weather] Loading accessory from cache: 48:3F:DA:xx:xx:xx-humidityin
[25/01/2024, 12:46:28] [Our Weather] Loading accessory from cache: 48:3F:DA:xx:xx:xx-tempf
[25/01/2024, 12:46:28] [Our Weather] Loading accessory from cache: 48:3F:DA:xx:xx:xx-humidity
[25/01/2024, 12:46:28] [Our Weather] Loading accessory from cache: 48:3F:DA:xx:xx:xx-solarradiation
[25/01/2024, 12:48:09] [Our Weather] ERROR: invalid json response body at https://rt.ambientweather.net/v1/devices?applicationKey=xxxxx&apiKey=xxxxx reason: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
[25/01/2024, 12:48:09] [Our Weather] ERROR: Cannot read properties of undefined (reading 'map')

This time it's different, this time at 12:46:06 a SIGTERM was triggered and restarted Homebridge, you can see it reloaded from cache after it restarted. The other plugins are still working and connected - and this plugin is still connected to HomeKit, but it's now only reporting the last cached data and not retrying the API call. Happy to open a different issue if needed.

peledies commented 9 months ago

I think this has to do with the AWN API. I have seen it return HTML instead of json from time to time. Probably need to check the response to ensure it has an Application: JSON header before proceeding.

WotAGitUR commented 9 months ago

happy to provide more info if you need it

gbridge0 commented 9 months ago

First, thank you so much for creating this plugin. I have 7 old AW F007TH sensors. With your plugin and a WS-100 gateway, I can now make the temperature and humidity data available in homeKit, without a need for the complex setup of rtl-sdr.

I also encountered a similar issue.

[1/30/2024, 5:45:02 PM] [AmbientWeatherSensors] ERROR: invalid json response body at https://rt.ambientweather.net/v1/devices?applicationKey=***&apiKey=**** reason: Unexpected token < in JSON at position 0 TypeError: Cannot read properties of undefined (reading 'filter') at HumidityAccessory.updateData (/var/lib/homebridge/node_modules/homebridge-ambient-weather-sensors/src/humidityAccessory.ts:49:28) at processTicksAndRejections (node:internal/process/task_queues:95:5)

The cause seems to be AWN API returning HTML occasionally instead of JSON, and the response.json() in node-fetch does not handle this gracefully. The response.json() is used by platform.ts at line 119:

  const data = await response.json();

The following stackflow thread provides a potential solution using JSON.parse(), which will throw an error: https://stackoverflow.com/questions/56178321/invalid-json-response-body-error-with-express-node-fetch-using-formdata

Hopefully the above link is useful.