Hey all, I was looking for ages but wasn’t able to find a concise answer to extracting (and publishing) the hourly weather.
This small automation will use the service as described here:
- id: forward_hourly_weather
alias: "Forward Hourly Weather to MQTT"
trigger:
- platform: time_pattern
# You can also match on interval. This will match every 5 minutes
minutes: "/5"
action:
- service: weather.get_forecasts
target:
entity_id: weather.forecast_home
data:
type: hourly
response_variable: my_hourly_forecast
- delay: '00:00:05' # Adjust the delay as needed to give the service call time to complete
- service: mqtt.publish
data_template:
topic: home/weather_hourly/set
payload: >-
{
{% for forecasts in my_hourly_forecast['weather.forecast_home'].forecast %}
{{ forecasts.datetime}} : {{ forecasts.condition}} : {{ forecasts.precipitation}}
{% endfor %}
}
The part I struggled with was what to do with the “response_variable”.
This simply iterates throught it and extracts the datetime, condition and precipitation.
You can add others as you need.
I hope this helps someone.
This is nice.
I don’t see anything exactly like this in the Blueprints exchange.
I think it would be awesome for the community and for yourself if you converted this into a blueprint to share.
If it’s an hourly forecast, why is the Time Pattern Trigger set to publish it every 5 minutes?
Is delay: '00:00:05' necessary? Because there are many examples employing weather.get_forecasts, even in a Trigger-based Template Sensor, and none intentionally pause after executing the service call.
Yeah, the thing is that you can’t simply use the service, if you put the forecast into the states you only see the Daily forecast, to get the hourly you have to use the service, then use some template trickery to “exctract” the value from the returned varaible.
For testing purposes, you can temporarily change rainy to sunny (or whatever is your current weather condition) just to confirm the message’s appearance is correct.
Or just plan an outdoor activity and then the weather is sure to become rainy.
You’re welcome and glad to hear the message is displayed correctly.
Please consider marking my post above with the Solution tag. It will automatically place a checkmark next to the topic’s title and place a link in the first post that leads to the Solution. This helps other users find answers to similar questions.