gieljnssns / buienalarm-sensor-homeassistant

Buienalarm custom_component for Home-Assistant
MIT License
35 stars 6 forks source link

Update for sensor fails. #19

Closed Veldkornet closed 3 years ago

Veldkornet commented 3 years ago

I’m started getting errors (my log is basically spammed with it) with this sensor as of HA 2021.04, add-on version 1.5:

2021-04-14 17:54:40 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.buienalarm_temperature fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 292, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 490, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/buienalarm/sensor.py", line 136, in update
    self.ba_data.update()
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 278, in wrapper
    result = method(*args, **kwargs)
  File "/config/custom_components/buienalarm/sensor.py", line 183, in update
    self.buienalarm = Buienalarm(
  File "/usr/local/lib/python3.8/site-packages/buienalarm/pybuienalarm.py", line 40, in __init__
    self.update()
  File "/usr/local/lib/python3.8/site-packages/buienalarm/pybuienalarm.py", line 83, in update
    data = resp.json()
  File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/simplejson/__init__.py", line 525, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
2021-04-14 17:54:41 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.buienalarm_precipitation fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 292, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 490, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/buienalarm/sensor.py", line 136, in update
    self.ba_data.update()
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 278, in wrapper
    result = method(*args, **kwargs)
  File "/config/custom_components/buienalarm/sensor.py", line 183, in update
    self.buienalarm = Buienalarm(
  File "/usr/local/lib/python3.8/site-packages/buienalarm/pybuienalarm.py", line 40, in __init__
    self.update()
  File "/usr/local/lib/python3.8/site-packages/buienalarm/pybuienalarm.py", line 83, in update
    data = resp.json()
  File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/simplejson/__init__.py", line 525, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
2021-04-14 17:54:41 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.buienalarm_precipitation_forecast_average fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 292, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 490, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/buienalarm/sensor.py", line 136, in update
    self.ba_data.update()
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 278, in wrapper
    result = method(*args, **kwargs)
  File "/config/custom_components/buienalarm/sensor.py", line 183, in update
    self.buienalarm = Buienalarm(
  File "/usr/local/lib/python3.8/site-packages/buienalarm/pybuienalarm.py", line 40, in __init__
    self.update()
  File "/usr/local/lib/python3.8/site-packages/buienalarm/pybuienalarm.py", line 83, in update
    data = resp.json()
  File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/simplejson/__init__.py", line 525, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
2021-04-14 17:54:41 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.buienalarm_precipitation_forecast_total fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 292, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 490, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/buienalarm/sensor.py", line 136, in update
    self.ba_data.update()
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 278, in wrapper
    result = method(*args, **kwargs)
  File "/config/custom_components/buienalarm/sensor.py", line 183, in update
    self.buienalarm = Buienalarm(
  File "/usr/local/lib/python3.8/site-packages/buienalarm/pybuienalarm.py", line 40, in __init__
    self.update()
  File "/usr/local/lib/python3.8/site-packages/buienalarm/pybuienalarm.py", line 83, in update
    data = resp.json()
  File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/simplejson/__init__.py", line 525, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
gieljnssns commented 3 years ago

I'm seeing the same errors, but I don't think I can fix this... After some search I found this error is thrown when the server could not be reached https://github.com/barban-dev/homeassistant-midea-dehumidifier/issues/10

Veldkornet commented 3 years ago

I suspected that it might be that... any way though that a friendlier error can be printed though so that we don't get this massive error on repeat the whole time? :)

dutch-erik commented 3 years ago

Just wanted to open issue for the same thing. Around 400 error messages the last 20hrs. :( I hope there will be a (friendlier) solution.

Veldkornet commented 3 years ago

Not sure if I'm comparing pears with apples here, but I have the following sensor (Also looking at buienalarm) for a neerslag card, and it doesn't seem to give any errors...

- platform: command_line
  command: python3 -c "import requests; import json; import random; dataRequest = requests.get('https://cdn-secure.buienalarm.nl/api/3.4/forecast.php?lat={{state_attr("zone.home", "latitude") | round(3)}}&lon={{state_attr("zone.home", "longitude") | round(3)}}&region=nl&unit=mm%2Fu&c='+str(random.randint(0,999999999999999)) ).text; dataRequest = dataRequest.replace('\r\n',' '); data = '{\"data\":'+dataRequest+'}';    print(data);"
  name: Neerslag_Buienalarm_Regen_Data
  json_attributes:
    - data
  value_template: "last_changed: {{states.sensor.neerslag_buienalarm_regen_data.last_changed | default(now())}}"
  scan_interval: 240
TheFes commented 3 years ago

I'm seeing the same errors, but I don't think I can fix this... After some search I found this error is thrown when the server could not be reached barban-dev/homeassistant-midea-dehumidifier#10

So, is this integration broken beyond repair then? For me it doesn't work anymore.

peternijssen commented 3 years ago

I had a look myself as I was getting nuts with all of the errors. I've noticed that Buienalarm throws a "502 bad gateway" for whatever reason. I think what could be a simple fix to limit the amount of errors is by checking the status code returned by Buienalarm before actually performing an action on the data.

So here https://github.com/gieljnssns/buienalarm/blob/master/buienalarm/pybuienalarm.py#L81 add something like

if response.status_code != 200:

It only means that the data is not updated, but in the current way it isn't anyway, but at least all errors are gone 😅

dutch-erik commented 3 years ago

@peternijssen Were you able to solve it by adding the code above?

peternijssen commented 3 years ago

Yeah that seems to work. Still checking as I recently changed it. This is how the try / catch block currently looks like;

        try:
            resp = requests.get(self.__REQUEST_URL, params=payload)
            if resp.ok:
                data = resp.json()
                if data["success"] is False:
                    LOG.error(data.get("reason"))
                else:
                    self.data = data
            else:
                LOG.error("Failed to get a response from Buienalarm. Response code: " + str(resp.status_code))
        except requests.exceptions.RequestException as e:
            LOG.error(e)

Bad part is that I had to change this in the library, but it's a starting point to try and then I can add a PR to the original library. Unless the author wants to fix it :)

gieljnssns commented 3 years ago

I'm open for a PR. I don't have the time to do it myself.

peternijssen commented 3 years ago

Pushed the change to the library. I wonder if we also should make sure that after a failure, it just continues with the old data perhaps. Since it failed to update, the next sensor will just try again and fail again, thus leading to the same error happening over and over again in the same second. Perhaps we should set the renew time to now + 10 seconds.

Schermafbeelding 2021-07-03 om 23 11 46