I tried some examples in the Template Editor:
Actually I tried to get the daily max. temperature (21C)
from the Met.no- Integration
Ich lese anscheinend den Max.-Wert der folgenden Stunde. Wie komme ich an den Tages-Max-Wert?
I tried some examples in the Template Editor:
Actually I tried to get the daily max. temperature (21C)
from the Met.no- Integration
Ich lese anscheinend den Max.-Wert der folgenden Stunde. Wie komme ich an den Tages-Max-Wert?
I cannot guess which sensor you use in which card but…
the weather sensor by met.no only delivers one (1) temperature and I am not sure if this is sort-of-current or max, I guess it is current as it shows 12.9
Hence you would need to create the forecast/template sensor as per above link. On top of that you can run a script to extract the max value
I am really grateful for all the answers and tips, but I am not able to create the solution.
Here is a screenshot after I tapped on the card. The sensor is called Home from Met.no and generates the forecast data (hourly or next days).
I have never written a script before and I can’t get any further with the examples from the links.
I understand the challenge but (always a but) knowing how to create Template sensors is sort of an important part when using HA. I also understand the learning curve and will provide some support but as I am travelling till Thu, it will have to wait till later this week (unless someone else steps in)
If ever you want to try before…what needs to be done
I have used some time today trying to figure out this my self, and have made this that will give you:
Just change weather.hjem for your weather entity and add this in your configuration.yaml
(I’m no expert, so there might be some thing that should be done different, but this works for me)
template:
- trigger:
- platform: state
entity_id: weather.hjem
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
action:
- service: weather.get_forecasts
data:
type: daily
target:
entity_id: weather.hjem
response_variable: daily
sensor:
- name: Forecast Daily
unique_id: weather_forecast_daily
state: "{{ states('weather.hjem') }}"
attributes:
forecast: "{{ daily['weather.hjem'].forecast }}"
- trigger:
- platform: state
entity_id: weather.hjem
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
action:
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: weather.hjem
response_variable: hourly
sensor:
- name: Forecast Hourly
unique_id: weather_forecast_hourly
state: "{{ states('weather.hjem') }}"
attributes:
forecast: "{{ hourly['weather.hjem'].forecast }}"
- sensor:
- name: "High temperature today"
device_class: temperature
state: "{{ (state_attr('sensor.forecast_hourly', 'forecast')|selectattr('datetime', 'lt', (now().replace(hour=23,minute=59)).isoformat()))| map(attribute='temperature') | list | max }}"
- sensor:
- name: "Low temperature today"
device_class: temperature
state: "{{ (state_attr('sensor.foreceast_hourly', 'forecast')|selectattr('datetime', 'lt', (now().replace(hour=23,minute=59)).isoformat()))| map(attribute='temperature') | list | min }}"
EDIT:
After feedback from petro I have changed the high and low temperature sensors.
Keeping the old code so the rest of the thread still makes sense
New code for the sensors:
- sensor:
- name: "High temperature today"
unique_id: High_temperature_today
device_class: temperature
unit_of_measurement: °C
state: >
{% set midnight = (today_at() + timedelta(days=1)).isoformat() %}
{{ state_attr('sensor.forecast_hourly', 'forecast')| selectattr('datetime', 'lt', midnight) | map(attribute='temperature') | list | max }}
- sensor:
- name: "Low temperature today"
unique_id: Low_temperature_today
device_class: temperature
unit_of_measurement: °C
state: >
{% set midnight = (today_at() + timedelta(days=1)).isoformat() %}
{{ state_attr('sensor.foreceast_hourly', 'forecast')| selectattr('datetime', 'lt', midnight) | map(attribute='temperature') | list | min }}
Great…as it works… you should be happy
Great, I would never have been able to put that together! Thank you very much!
Many thanks also to you for your time, help and explanations
This also helped me a ton, thanks so much!
There was a small typo with “forceast” instead of “forecast”, but it was easy to fix.
Great Work, thanks
Hi vingerha (or anyone else), is this for the current day or the coming 23 hours and 59 minutes?
Sorry, found it… → for the current day
Current day. And the proper way to actually get it would be to use todays date + 1 day.
template:
- trigger:
- platform: state
entity_id: weather.openweathermap
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
action:
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: weather.openweathermap
response_variable: result
sensor:
- name: Max Hourly Temperature
unique_id: max_hourly_temperature
device_class: temperature
unit_of_measurement: °F
state: >
{% set midnight = (today_at() + timedelta(days=1)).isoformat() %}
{{ result['weather.openweathermap'].forecast | selectattr('datetime', 'lt', midnight) | map(attribute='temperature') | list | max }}
Hi Petro, thanks for jumping in!
Would you mind explaining briefly why this is the proper way?
TIA
Replacing hours/minutes on a datetime object can lead to unforeseen issues if you don’t have the correct timezone. It’s best to use the template methods that we added to make time calculations easier.
The issue is, most forum posts use old methods which run into issues with timezones, leading to undesired effects.
You should avoid using .replace
if you can. You should avoid using as_timestamp
if you can.
Both can be supplimented with timedelta
which is essentially a duration. YOu can add and subtract them from datetimes. If the datetime has a TZ attached to it, you can assume that it will be the correct time in your TZ regardless what TZ the datetime is in.
I tried this and it shows UndefinedError: ‘result’ is undefined. Everything else seems to be ok in there. Not sure what it doesn’t like. I can call the service manually on the service tab just fine.
You have to configure that entire chunk of code in your configuration file, not in a UI template sensor.
Petro,
Thanx for your help!
Did you get it working?
Hi, For me , the states always remain ‘unknown’