I really need help to make a very simple wind sensor that takes the average of the next 3 hours from the integrated weather forecast for wind… I’ve looked at many discussions here and the doc, and I though I understood but obviously not. It updates each hour with no values in it,
any advice ?
Thanks
The response from your weather.get_forecasts action will be in your response variable hourly_forecast. As far as I can tell you never do anything with that variable…?
I have never used that action and it sure sounds like you haven’t either, so I’d start by running it manually in the Developer Tools > Action panel (or an automation/script) so you can see exactly how the response looks.
AFAIK a response variable always contains a dict/mapping at its root level, so you probably cannot assume to use list syntax directly at the root. The example you linked to uses the entity id of your forecast service as its initial key, probably so that multiple services can be queried in the same action.
The forecast is not an attribute. Why are you trying to access it as an attribute? You need to run the weather.get_forecasts action, then process the response variable.
To see what the response variable contains, use the Developer Tools > Actions panel. Do not attempt to use the Developer Tools > Template panel, it will not help you.
Now that I am no longer on the phone but in front of a computer I looked at how the weather.get_forecasts action works, and it was pretty much exactly as expected. Something like this should produce what you are after:
{% set forecast_hours = 3 %}
{{ (hourly_forecast['weather.forecast_maison']['forecast'][:forecast_hours]
| map(attribute='wind_speed') | sum / forecast_hours) | round(2) }}
I might move the definition of forecast_hours out of the template and into a YAML-level variable instead though. Looks a little cleaner and more obvious if you want to change it later, or make a blueprint out of it or whatever. If so maybe also put the entity id of the weather service into a variable rather than hardcoding it in multiple places.
Thanks a lot for the explanations and your patience (I’m a noob, but the action panel is really cool )
And you’re solution is working perfectly in a single line of code that’s perfect
And I agree that I need to clean several things (but I was trying to make it work first):
agree tthat forecast hours should be defined in a more cleaner way to adapt to the situation
but I want a more general / cleaner way to call the weather service something like this has proved to work to set a wind speed template sensor:
{% set weather_entities = states.weather | selectattr('attributes.wind_speed', 'defined') | list %}
{{ (weather_entities[0].attributes.wind_speed | float) / 3.6 }}
But I don’t know how to use it in the action get_forecasts as I need the entity_id…
I tried this terrible thing (and that’s not working)
That expression produces a generator object, and if use a list filter what you get is a list of state objects. So you may as well turn those state objects into entity ids first, then finish by turning the generator into a list (which is what the action expects to get, either a single entity id (string) or a list of them). Thus:
So if you feed that into weather.get_forecasts, you will end up with a dict containing separate forecasts from each weather service for which you have an integration in HASS.
Thanks
Finally, I think that the sensor should work now, but I need to wait next hour to be sure as I understood the hourly trigger will happen each hour (but I successfully tested the lines in action panel and model panel)