jdejaegh / irm-kmi-ha

Home Assistant weather provider using data from Belgian IRM KMI 🇧🇪 🇱🇺 🇳🇱
MIT License
40 stars 0 forks source link

hourly weather data via attributes #11

Closed JanDetrez closed 5 months ago

JanDetrez commented 5 months ago

Would it be possible to expose the hourly weather data via attributes? More specifically I'm interested to retrieve the rain in the upcoming hours to see if I will let out my mowing robot or not.

The default weather integration does it like below. image

I can retrieve the rain data for the coming hours like: {{ (state_attr(''weather.forecast_home'',''forecast'')[0].precipitation | float(0) + state_attr(''weather.forecast_home'',''forecast'')[1].precipitation | float(0) + state_attr(''weather.forecast_home'',''forecast'')[2].precipitation | float(0) ) < 5 }}

I suppose you have the data because i see it in the lovelace, but I don't see any state attributes I could retrieve. image

image

jdejaegh commented 5 months ago

Hello @JanDetrez

When setting up the integration (or go in Settings > Devices & services > KMI and then select 'Configure' if you already added it), you can select an option for 'Use the deprecated forecast attribute'. If you set it to 'Use for hourly forecast' then you'll have something similar to your screenshot of weather.forecast_home.

image

With that setting, the state will include a forecast attribute for the upcoming hours. precipitation_probability is a value in % and precipitation is in mm, just like in the KMI app.

image

As the feature is already there, I am closing this issue. Feel free to comment if something is unclear

JanDetrez commented 4 months ago

This worked, but it seemed to have reset itself after updating. I had to re-enable it. I'm not sure if this is expected behaviour, and if there is a way to make this permanent? Why deprecate some and why not have all attributes at all times? Thank you.

JanDetrez commented 4 months ago

Would it also be possible to have the hourly and daily forecasts at the same time? Now I have to choose between either one of them. For example, I would like to have the hourly for the reason mention in my first post (forecast link to starting robot mower), but would be nice to also have the daily forecast, to extract parameters like wind gust which are helpful to see if the weather will be nice or not the coming days :-)

Btw it also seems "daily" is in the dropdown twice for me image

Btw, thanks to KMI I now have this pretty nice and most importantly accurate weather card :-) the wind and rain map are conditionally showing in case there is wind gust forecasted for the current day (would like to add forecast for several days as well, but currently not possible), and if there is rain forecasted the next hours.

type: vertical-stack title: Weather cards:

  • show_current: true show_forecast: true type: weather-forecast entity: weather.home forecast_type: daily
  • type: custom:hourly-weather entity: weather.home icons: true show_wind: 'false' show_date: 'false' show_precipitation_amounts: 1 name: null label_spacing: '1' num_segments: '6' colors: sunny: '#fdd93c' pouring: '#005c92' rainy: '#0082cd' cloudy: '#d4d4d4' partlycloudy: '#fddd4f'
  • type: conditional conditions:
    • condition: numeric_state entity: sensor.wind_gust_helper above: 50 card: type: tile entity: sensor.wind_gust_helper name: Wind Gust icon: mdi:wind-power-outline vertical: false
  • type: conditional conditions:
    • condition: numeric_state entity: sensor.rain_next_hours_helper above: 0.03 card: show_state: false show_name: false camera_view: auto type: picture-entity entity: camera.radar_home

image

jdejaegh commented 4 months ago

This worked, but it seemed to have reset itself after updating. I had to re-enable it. I'm not sure if this is expected behaviour, and if there is a way to make this permanent?

It should keep the configuration and not reset between updates. Do you know from which version to which version you updated? I'll try to reproduce and fix it in the future.

Why deprecate some and why not have all attributes at all times?

Not sure to understand what your question relates to. I assume it's about the forecast attribute.

Home Assistant deprecated this attribute in the past and deleted it in 2024.4. I had to provide the forecast attribute in another way for it to still be available to you and the other users of this integration.

Some background discussion about the attribute being removed:

Basically, Home Assistant want to move towards service call for weather forecast. the weather.get_forecasts service call replaces the attribute.

Would it also be possible to have the hourly and daily forecasts at the same time? Now I have to choose between either one of them.

It is not possible using the attribute. That's one of the motivation for Home Assistant to move to a service call instead of an attribute. If you really want it, make a second zone a the same location and get another instance of the integration (not super efficient but well).

My advice would be to update your setup with service calls to weather.get_forecasts. It's currently supported by all weather integrations (this one included). It allows you to get daily, twice daily and hourly data depending on the parameter and what the weather integration provides. This integration provides the three types of forecasts so enjoy!

I know that some things are only possible using the attribute. That's why I am keeping it a little bit longer. However, in the long run, I'll move to a service call only; just like Home Assistant did with 2024.4.

Btw it also seems "daily" is in the dropdown twice for me

Typo in the translation file, it will be fixed in the next release. One of them is supposed to be 'twice daily'. Thanks

Btw, thanks to KMI I now have this pretty nice and most importantly accurate weather card :-)

Cool setup! Thanks for the feedback and I hope this reply helps with your questions :)

JanDetrez commented 4 months ago

Many thanks for the feedback. I had missed this bit on the service calls. I solved my request to both have daily and hourly detailed forecast by making two template sensor:

template:

  • trigger:
    • platform: time_pattern seconds: /10 action:
    • service: weather.get_forecasts data: type: hourly target: entity_id: weather.home response_variable: response sensor:
    • name: KMI Forecast hourly unique_id: kmi_forecast_hourly state: "{{ now().isoformat() }}" attributes: forecast: "{{ response['weather.home'].forecast }}"
  • trigger:
    • platform: time_pattern seconds: /10 action:
    • service: weather.get_forecasts data: type: daily target: entity_id: weather.home response_variable: response sensor:
    • name: KMI Forecast daily unique_id: kmi_forecast_daily state: "{{ now().isoformat() }}" attributes: forecast: "{{ response['weather.home'].forecast }}"

I run into one issue now, it seems that for gust speed, when there is none, the variable is not present in the template sensor I created above. I'm not sure how to work my way around the errors that come from this. image

If I try to convert to float, it will give an errror image

Extracting it from day 5, which has gust winds forecasted is fine image

In the (deprecated) attribute you have it would say null/none and would by float conversion go to 0 image

jdejaegh commented 4 months ago

I see the issue.

When you use the (deprecated) attribute, I am in full control of what I return there: the output generated by the integration is blindly returned to the user.

When you use the forecast service, some Home Assistant Core logic handles my output before sending it to the user. Part of that logic is to remove the keys for which the value is None (to not clog up the output I guess). There is no way for Home Assistant Core to know if the value is None because the integration does not implement that feature or because the None is meaningful (like, there is no wind gusts today).

For your template, try to go from this:

{{ states.sensor.kmi_forecast_daily.attributes.forecast[0].wind_gust_speed | float(0) }}

to something like this

{{ states.sensor.kmi_forecast_daily.attributes.forecast[0].wind_gust_speed | default | float(0) }}

The Jinja default filter is probably what your are looking for. Check their short and self-explanatory doc about it, they have examples that helped me to figure out how to use it: https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.default

JanDetrez commented 4 months ago

Great, that worked. Thanks a lot!