home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
74.21k stars 31.16k forks source link

Openweathermap Rain sensor not reporting correctly #20494

Closed txNgineer closed 5 years ago

txNgineer commented 5 years ago

Home Assistant release with the issue:

0.85.1 and prior

Last working Home Assistant release (if known): none, to my knowledge

Operating environment (Hass.io/Docker/Windows/etc.):

homeassistant on Raspian running on Raspberry Pi 3. OS: Raspbian 9.6 stretch Kernel: armv7l Linux 4.14.79-v7+ CPU: ARMv7 rev 4 (v7l) @ 1.2GHz RAM: 346MiB / 926MiB

Component/platform:

sensor.owm_rain Likely code error in: homeassistant/components/openweathermap.py

Description of problem: The sensor always shows "Not Raining" even when a query to the API directly shows a value for the rain sensor.

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):

in sensors.yaml
#### Openweathermap ##############################
  - platform: openweathermap
    api_key: !secret openwx_API
    forecast: True
    monitored_conditions:
      - weather
      - temperature
      - wind_speed
      - wind_bearing
      - humidity
      - pressure
      - clouds
      - rain
      - snow

Traceback (if applicable):

2019-01-26 20:54:34 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.owm_rain fails
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity.py", line 204, in async_update_ha_state
    yield from self.async_device_update()
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity.py", line 327, in async_device_update
    yield from self.hass.async_add_job(self.update)
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/sensor/openweathermap.py", line 164, in update
    self._state = round(data.get_rain()['3h'], 0)
KeyError: '3h'

Additional information: Line 164 of the python file gets the rain data with

       if data.get_rain():
           self._state = round(data.get_rain()['3h'], 0)
           self._unit_of_measurement = 'mm'

But this is not suitable in all situations. For example the API query and JSON response at this time show: Query: https://api.openweathermap.org/data/2.5/weather?zip=77423,us&units=imperial&id= with Response: {"coord":{"lon":-96,"lat":29.83},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"stations","main":{"temp":55.29,"pressure":1021,"humidity":84,"temp_min":53.06,"temp_max":57.2},"visibility":16093,"wind":{"speed":7.47,"deg":159.004},"rain":{"1h":0.25},"clouds":{"all":90},"dt":1548554100,"sys":{"type":1,"id":3968,"message":0.0038,"country":"US","sunrise":1548594998,"sunset":1548633431},"id":420037180,"name":"Houston","cod":200} Notice that only a 1h number is shown and no 3h is available for this location. Refer to API note that Rain item is showing "1h" in the case when data are received from weather stations directly. . Also note that this data is often a fraction of a mm and should be rounded to 2 decimal places.

txNgineer commented 5 years ago

Here is my understanding of the API conditions:

B-Kramer commented 5 years ago

If some digs into this, I think the api condition codes need to be changed too... "exceptional" comes up too often, when better information is being reported by the api.

txNgineer commented 5 years ago

The biggest issue I have had when digging into it is the lack of information from the API web site on what the allowable values are. I will try contacting OpenWeatherMap directly and see if I can get any info directly. Should a more complete API document become available, I will be glad to do the digging. While I can code the changes, my style may not be properly "pythonic", and while it works for me it makes me hesitant to share.

txNgineer commented 5 years ago

Further info from the API shows the JSON decoder must handle missing parameters more fully:

If you do not see some of the parameters in your API response it means that these weather phenomena are just not happened for the time of measurement for the city or location chosen. Only really measured or calculated data is displayed in API response.

txNgineer commented 5 years ago

I did find the following table on the weather station POST description. It has a minimum of what needs to be considered to avoid exceptions (Note - the units could also be returned in imperial quantities if the GET request specified them that way):

Parameter Type & Units Description
station_id   The internal ID of the station which is generated during creation
dt Unix Timestamp Time of measurement
temperature Celsius The air temperature
wind_speed m/s Wind speed
wind_gust m/s Maximum speed of wind gusts
wind_deg Degrees from 0 to 360 Wind direction
pressure Hectopascal Atmospheric pressure
humidity % Relative air humidity
rain_1h mm Rainfall in the last hour
rain_6h mm Rainfall in the last 6 hours
rain_24h mm Rainfall for the last days
snow_1h mm Snow in the last hour
snow_6h mm Snow in the last 6 hours
snow_24h mm Snow for the last days
dew_point Celsius Dew point
humidex Celsius Index of temperature and humidity
heat_index Celsius Heat index
visibility_distance km Distance of direct visibility, METAR
visibility_prefix N, E, S, W Prefix at distinctions of visibility in the directions, METAR
clouds[]:distance m Height of the lower bound of overcast, METAR
clouds[]:condition SKC, NSC, FEW, SCT, BKN, OVC Cloud amount, METAR
clouds[]:cumulus CB, TCU Convective overcast, METAR
weather[]:precipitation   Additional description, METAR
weather[]:descriptor   Additional description, METAR
weather[]:intensity   Additional description, METAR
weather[]:proximity   Additional description, METAR
weather[]:obsruration   Additional description, METAR
weather[]:other   Additional description, METAR
stale[bot] commented 5 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue now has been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.