Get warmest hours of the day (in array) to be used later in automation for house ventilation

Hi,
new to HA , jaml and jinja2, but i hope someone can help me/ set me off in the right direction.
i’m moving from a different domotica system (supporting lua) to HA and i would like to do the following in HA.
In the morning fetch the weather (open weather maps) forecast for every hour of the day. and save relevant info in an array of sorts.
and in an automation check every hour if current hour is in that array. if so, activate ventilation, if not stop ventilation.

i set open weather maps to option “one call hourly” so i get hourly forecast info.
what i got so far in the template develop tool:

{{state_attr('weather.openweathermap','forecast') | sort(attribute='temperature' , reverse=true) }}

gives me a long list op (json) :slight_smile:

[
   {
      "condition":"cloudy",
      "datetime":"2023-11-23T11:00:00+00:00",
      "wind_bearing":267.9,
      "temperature":12.8,
      "templow":10.9,
      "wind_speed":31.7,
      "precipitation":0.2,
      "humidity":79
   },
   {
      "condition":"rainy",
      "datetime":"2023-11-24T11:00:00+00:00",
      "wind_bearing":311.7,
      "temperature":10.1,
      "templow":5.7,
      "wind_speed":35.3,
      "precipitation":5.6,
      "humidity":69
   },
   {
      "condition":"rainy",
      "datetime":"2023-11-25T11:00:00+00:00",
      "wind_bearing":334.1,
      "temperature":7.8,
      "templow":4.7,
      "wind_speed":27.7,
      "precipitation":4.9,
      "humidity":76
   },
   {
      "condition":"cloudy",
      "datetime":"2023-11-26T11:00:00+00:00",
      "wind_bearing":256.2,
      "temperature":7.7,
      "templow":4.8,
      "wind_speed":11.5,
      "precipitation":1.6,
      "humidity":81
   },
   {
      "condition":"rainy",
      "datetime":"2023-11-27T11:00:00+00:00",
      "wind_bearing":112.1,
      "temperature":4.7,
      "templow":1.6,
      "wind_speed":25.6,
      "precipitation":18.5,
      "humidity":100
   },
   {
      "condition":"sunny",
      "datetime":"2023-11-28T11:00:00+00:00",
      "wind_bearing":13.0,
      "temperature":1.4,
      "templow":-1.8,
      "wind_speed":23.4,
      "precipitation":0.6,
      "humidity":73
   }
][
   12.8,
   10.1,
   7.8,
   7.7,
   4.7,
   1.4
][
   {
      "condition":"cloudy",
      "precipitation_probability":0,
      "datetime":"2023-11-23T14:00:00+00:00",
      "wind_bearing":262,
      "cloud_coverage":66,
      "temperature":12.4,
      "pressure":1020.0,
      "wind_speed":31.32,
      "precipitation":0.0,
      "humidity":80
   },
   {
      "condition":"cloudy",
      "precipitation_probability":19,
      "datetime":"2023-11-23T19:00:00+00:00",
      "wind_bearing":281,
      "cloud_coverage":97,
      "temperature":12.4,
      "pressure":1017.0,
      "wind_speed":31.86,
      "precipitation":0.0,
      "humidity":78
   },
   {
      "condition":"cloudy",
      "precipitation_probability":35,
      "datetime":"2023-11-23T20:00:00+00:00",
      "wind_bearing":288,
      "cloud_coverage":98,
      "temperature":12.4,
      "pressure":1017.0,
      "wind_speed":32.44,
      "precipitation":0.0,
      "humidity":78
   },
   {
      "condition":"cloudy",
      "precipitation_probability":0,
      "datetime":"2023-11-23T18:00:00+00:00",
      "wind_bearing":278,
      "cloud_coverage":55,
      "temperature":12.2,
      "pressure":1017.0,
      "wind_speed":31.18,
      "precipitation":0.0,
      "humidity":81
   },
   {
      "condition":"cloudy",
      "precipitation_probability":0,
      "datetime":"2023-11-23T13:00:00+00:00",
      "wind_bearing":265,
      "cloud_coverage":59,
      "temperature":12.1,
      "pressure":1021.0,
      "wind_speed":31.1,
      "precipitation":0.0,
      "humidity":82
   },
   {
      "condition":"partlycloudy",
      "precipitation_probability":0,
      "datetime":"2023-11-23T17:00:00+00:00",
      "wind_bearing":275,
      "cloud_coverage":49,
      "temperature":12.1,
      "pressure":1018.0,
      "wind_speed":33.16,
      "precipitation":0.0,
      "humidity":82
   },
   {
      "condition":"partlycloudy",
      "precipitation_probability":0,
      "datetime":"2023-11-23T15:00:00+00:00",
      "wind_bearing":264,
      "cloud_coverage":46,
      "temperature":12.0,
      "pressure":1018.0,
      "wind_speed":33.73,
      "precipitation":0.0,
      "humidity":82
   },
   {
      "condition":"rainy",
      "precipitation_probability":55,
      "datetime":"2023-11-23T21:00:00+00:00",
      "wind_bearing":295,
      "cloud_coverage":98,
      "temperature":11.6,
      "pressure":1016.0,
      "wind_speed":32.22,
      "precipitation":0.36,
      "humidity":84
   },
   {
      "condition":"cloudy",
      "precipitation_probability":0,
      "datetime":"2023-11-23T12:00:00+00:00",
      "wind_bearing":266,
      "cloud_coverage":69,
      "temperature":11.5,
      "pressure":1022.0,
      "wind_speed":28.94,
      "precipitation":0.0,
      "humidity":85
   },
   {
      "condition":"partlycloudy",
      "precipitation_probability":0,
      "datetime":"2023-11-23T16:00:00+00:00",
      "wind_bearing":271,
      "cloud_coverage":37,
      "temperature":11.5,
      "pressure":1018.0,
      "wind_speed":35.14,
      "precipitation":0.0,
      "humidity":84
   },
   {
      "condition":"cloudy",
      "precipitation_probability":0,
      "datetime":"2023-11-23T11:00:00+00:00",
      "wind_bearing":265,
      "cloud_coverage":65,
      "temperature":11.4,
      "pressure":1022.0,
      "wind_speed":26.71,
      "precipitation":0.0,
      "humidity":85
   },
   {
      "condition":"cloudy",
      "precipitation_probability":0,
      "datetime":"2023-11-23T09:00:00+00:00",
      "wind_bearing":271,
      "cloud_coverage":62,
      "temperature":11.3,
      "pressure":1022.0,
      "wind_speed":23.54,
      "precipitation":0.0,
      "humidity":85
   },
   {
      "condition":"cloudy",
      "precipitation_probability":0,
      "datetime":"2023-11-23T10:00:00+00:00",
      "wind_bearing":268,
      "cloud_coverage":64,
      "temperature":11.2,
      "pressure":1022.0,
      "wind_speed":25.63,
      "precipitation":0.0,
      "humidity":84
   },
   {
      "condition":"rainy",
      "precipitation_probability":67,
      "datetime":"2023-11-23T22:00:00+00:00",
      "wind_bearing":330,
      "cloud_coverage":99,
      "temperature":10.4,
      "pressure":1016.0,
      "wind_speed":26.86,
      "precipitation":0.3,
      "humidity":80
   },
   {
      "condition":"cloudy",
      "precipitation_probability":86,
      "datetime":"2023-11-23T23:00:00+00:00",
      "wind_bearing":323,
      "cloud_coverage":99,
      "temperature":9.9,
      "pressure":1017.0,
      "wind_speed":27.76,
      "precipitation":0.0,
      "humidity":72
   },
   {
      "condition":"cloudy",
      "precipitation_probability":83,
      "datetime":"2023-11-24T00:00:00+00:00",
      "wind_bearing":319,
      "cloud_coverage":97,
      "temperature":8.7,
      "pressure":1017.0,
      "wind_speed":26.89,
      "precipitation":0.0,
      "humidity":72
   },
   {
      "condition":"cloudy",
      "precipitation_probability":14,
      "datetime":"2023-11-24T02:00:00+00:00",
      "wind_bearing":315,
      "cloud_coverage":73,
      "temperature":8.5,
      "pressure":1016.0,
      "wind_speed":26.28,
      "precipitation":0.0,
      "humidity":63
   },
   {
      "condition":"partlycloudy",
      "precipitation_probability":14,
      "datetime":"2023-11-24T01:00:00+00:00",
      "wind_bearing":315,
      "cloud_coverage":47,
      "temperature":8.4,
      "pressure":1016.0,
      "wind_speed":26.75,
      "precipitation":0.0,
      "humidity":68
   },
   {
      "condition":"cloudy",
      "precipitation_probability":15,
      "datetime":"2023-11-24T03:00:00+00:00",
      "wind_bearing":310,
      "cloud_coverage":82,
      "temperature":8.3,
      "pressure":1016.0,
      "wind_speed":23.62,
      "precipitation":0.0,
      "humidity":64
   },
   {
      "condition":"rainy",
      "precipitation_probability":24,
      "datetime":"2023-11-24T04:00:00+00:00",
      "wind_bearing":305,
      "cloud_coverage":87,
      "temperature":8.1,
      "pressure":1015.0,
      "wind_speed":23.69,
      "precipitation":0.13,
      "humidity":70
   },
   {
      "condition":"rainy",
      "precipitation_probability":32,
      "datetime":"2023-11-24T05:00:00+00:00",
      "wind_bearing":311,
      "cloud_coverage":89,
      "temperature":8.1,
      "pressure":1014.0,
      "wind_speed":26.06,
      "precipitation":0.19,
      "humidity":69
   },
   {
      "condition":"rainy",
      "precipitation_probability":48,
      "datetime":"2023-11-24T06:00:00+00:00",
      "wind_bearing":325,
      "cloud_coverage":91,
      "temperature":7.9,
      "pressure":1014.0,
      "wind_speed":25.6,
      "precipitation":0.19,
      "humidity":69
   },
   {
      "condition":"rainy",
      "precipitation_probability":96,
      "datetime":"2023-11-24T11:00:00+00:00",
      "wind_bearing":328,
      "cloud_coverage":99,
      "temperature":7.7,
      "pressure":1014.0,
      "wind_speed":29.41,
      "precipitation":0.21,
      "humidity":60
   },
   {
      "condition":"rainy",
      "precipitation_probability":100,
      "datetime":"2023-11-24T12:00:00+00:00",
      "wind_bearing":325,
      "cloud_coverage":87,
      "temperature":7.6,
      "pressure":1013.0,
      "wind_speed":33.08,
      "precipitation":0.37,
      "humidity":61
   },
   {
      "condition":"rainy",
      "precipitation_probability":93,
      "datetime":"2023-11-24T10:00:00+00:00",
      "wind_bearing":322,
      "cloud_coverage":99,
      "temperature":7.5,
      "pressure":1014.0,
      "wind_speed":27.86,
      "precipitation":0.2,
      "humidity":61
   },
   {
      "condition":"rainy",
      "precipitation_probability":100,
      "datetime":"2023-11-24T13:00:00+00:00",
      "wind_bearing":320,
      "cloud_coverage":31,
      "temperature":7.4,
      "pressure":1013.0,
      "wind_speed":34.49,
      "precipitation":0.25,
      "humidity":62
   },
   {
      "condition":"rainy",
      "precipitation_probability":76,
      "datetime":"2023-11-24T07:00:00+00:00",
      "wind_bearing":329,
      "cloud_coverage":100,
      "temperature":7.1,
      "pressure":1014.0,
      "wind_speed":23.22,
      "precipitation":0.17,
      "humidity":75
   },
   {
      "condition":"rainy",
      "precipitation_probability":100,
      "datetime":"2023-11-24T14:00:00+00:00",
      "wind_bearing":318,
      "cloud_coverage":35,
      "temperature":6.8,
      "pressure":1012.0,
      "wind_speed":35.46,
      "precipitation":0.24,
      "humidity":65
   },
   {
      "condition":"rainy",
      "precipitation_probability":84,
      "datetime":"2023-11-24T09:00:00+00:00",
      "wind_bearing":323,
      "cloud_coverage":100,
      "temperature":6.7,
      "pressure":1014.0,
      "wind_speed":27.18,
      "precipitation":0.18,
      "humidity":71
   },
   {
      "condition":"rainy",
      "precipitation_probability":80,
      "datetime":"2023-11-24T08:00:00+00:00",
      "wind_bearing":331,
      "cloud_coverage":100,
      "temperature":6.5,
      "pressure":1015.0,
      "wind_speed":24.95,
      "precipitation":0.24,
      "humidity":77
   },
   {
      "condition":"rainy",
      "precipitation_probability":74,
      "datetime":"2023-11-25T08:00:00+00:00",
      "wind_bearing":332,
      "cloud_coverage":100,
      "temperature":6.5,
      "pressure":1012.0,
      "wind_speed":20.59,
      "precipitation":0.21,
      "humidity":70
   },
   {
      "condition":"cloudy",
      "precipitation_probability":80,
      "datetime":"2023-11-24T18:00:00+00:00",
      "wind_bearing":331,
      "cloud_coverage":68,
      "temperature":6.4,
      "pressure":1013.0,
      "wind_speed":35.57,
      "precipitation":0.0,
      "humidity":61
   },
   {
      "condition":"rainy",
      "precipitation_probability":100,
      "datetime":"2023-11-24T15:00:00+00:00",
      "wind_bearing":325,
      "cloud_coverage":37,
      "temperature":6.3,
      "pressure":1012.0,
      "wind_speed":38.81,
      "precipitation":0.28,
      "humidity":66
   },
   {
      "condition":"rainy",
      "precipitation_probability":100,
      "datetime":"2023-11-24T17:00:00+00:00",
      "wind_bearing":328,
      "cloud_coverage":62,
      "temperature":6.2,
      "pressure":1013.0,
      "wind_speed":37.44,
      "precipitation":0.15,
      "humidity":64
   },
   {
      "condition":"rainy",
      "precipitation_probability":60,
      "datetime":"2023-11-25T04:00:00+00:00",
      "wind_bearing":315,
      "cloud_coverage":55,
      "temperature":6.2,
      "pressure":1013.0,
      "wind_speed":21.96,
      "precipitation":0.27,
      "humidity":70
   },
   {
      "condition":"cloudy",
      "precipitation_probability":80,
      "datetime":"2023-11-24T16:00:00+00:00",
      "wind_bearing":324,
      "cloud_coverage":53,
      "temperature":6.1,
      "pressure":1012.0,
      "wind_speed":36.9,
      "precipitation":0.0,
      "humidity":65
   },
   {
      "condition":"rainy",
      "precipitation_probability":71,
      "datetime":"2023-11-25T07:00:00+00:00",
      "wind_bearing":322,
      "cloud_coverage":100,
      "temperature":6.1,
      "pressure":1012.0,
      "wind_speed":20.12,
      "precipitation":0.14,
      "humidity":72
   },
   {
      "condition":"cloudy",
      "precipitation_probability":28,
      "datetime":"2023-11-24T19:00:00+00:00",
      "wind_bearing":335,
      "cloud_coverage":72,
      "temperature":6.0,
      "pressure":1013.0,
      "wind_speed":34.45,
      "precipitation":0.0,
      "humidity":63
   },
   {
      "condition":"rainy",
      "precipitation_probability":63,
      "datetime":"2023-11-25T06:00:00+00:00",
      "wind_bearing":313,
      "cloud_coverage":64,
      "temperature":6.0,
      "pressure":1012.0,
      "wind_speed":23.0,
      "precipitation":0.13,
      "humidity":72
   },
   {
      "condition":"cloudy",
      "precipitation_probability":28,
      "datetime":"2023-11-24T20:00:00+00:00",
      "wind_bearing":344,
      "cloud_coverage":55,
      "temperature":5.8,
      "pressure":1013.0,
      "wind_speed":30.82,
      "precipitation":0.0,
      "humidity":65
   },
   {
      "condition":"cloudy",
      "precipitation_probability":28,
      "datetime":"2023-11-24T21:00:00+00:00",
      "wind_bearing":344,
      "cloud_coverage":65,
      "temperature":5.7,
      "pressure":1014.0,
      "wind_speed":26.21,
      "precipitation":0.0,
      "humidity":67
   },
   {
      "condition":"cloudy",
      "precipitation_probability":32,
      "datetime":"2023-11-24T22:00:00+00:00",
      "wind_bearing":343,
      "cloud_coverage":60,
      "temperature":5.6,
      "pressure":1013.0,
      "wind_speed":21.64,
      "precipitation":0.0,
      "humidity":66
   },
   {
      "condition":"rainy",
      "precipitation_probability":48,
      "datetime":"2023-11-25T03:00:00+00:00",
      "wind_bearing":313,
      "cloud_coverage":40,
      "temperature":5.4,
      "pressure":1013.0,
      "wind_speed":18.11,
      "precipitation":0.23,
      "humidity":72
   },
   {
      "condition":"cloudy",
      "precipitation_probability":63,
      "datetime":"2023-11-25T05:00:00+00:00",
      "wind_bearing":310,
      "cloud_coverage":56,
      "temperature":5.4,
      "pressure":1013.0,
      "wind_speed":20.3,
      "precipitation":0.0,
      "humidity":70
   },
   {
      "condition":"cloudy",
      "precipitation_probability":35,
      "datetime":"2023-11-24T23:00:00+00:00",
      "wind_bearing":337,
      "cloud_coverage":52,
      "temperature":5.3,
      "pressure":1014.0,
      "wind_speed":17.03,
      "precipitation":0.0,
      "humidity":66
   },
   {
      "condition":"partlycloudy",
      "precipitation_probability":31,
      "datetime":"2023-11-25T00:00:00+00:00",
      "wind_bearing":326,
      "cloud_coverage":46,
      "temperature":5.2,
      "pressure":1014.0,
      "wind_speed":16.96,
      "precipitation":0.0,
      "humidity":66
   },
   {
      "condition":"partlycloudy",
      "precipitation_probability":27,
      "datetime":"2023-11-25T01:00:00+00:00",
      "wind_bearing":308,
      "cloud_coverage":12,
      "temperature":4.7,
      "pressure":1013.0,
      "wind_speed":13.32,
      "precipitation":0.0,
      "humidity":68
   },
   {
      "condition":"partlycloudy",
      "precipitation_probability":35,
      "datetime":"2023-11-25T02:00:00+00:00",
      "wind_bearing":294,
      "cloud_coverage":25,
      "temperature":4.6,
      "pressure":1013.0,
      "wind_speed":13.28,
      "precipitation":0.0,
      "humidity":69
   }
]

and if i put it in a variable i can loop it to get first/highest temperature segments. (must be a better way than a loop i think but ok) so i thought it best to create a json but this is furthest i got:

{% for i in state_attr('weather.openweathermap','forecast')[:6] | sort(attribute='temperature' , reverse=true) %}
{{ 'index :' ~ loop.index }}
{{ 'temperature :' ~ i.temperature }} 
{{ 'darte :' ~ i.datetime }}
{% endfor %}

resulting in :
index :1
temperature :12.4
date :2023-11-23T14:00:00+00:00

index :2
temperature :12.1
date :2023-11-23T13:00:00+00:00

index :3
temperature :12.0
date :2023-11-23T15:00:00+00:00

index :4
temperature :11.6
date :2023-11-23T12:00:00+00:00

index :5
temperature :11.5
date :2023-11-23T11:00:00+00:00

index :6
temperature :11.4
date :2023-11-23T10:00:00+00:00

is there any way i can get this in a (global/helper) json or other type of array that can be addressed from within an automation?

any help will be highly appreciated, i tried all sorts of things but can’t seem to get it in a helper.

The forecast attribute is being deprecated, so you will want to use the weather.get_forecasts service in a trigger-based template sensor.

template:
  - trigger:
      - platform: time
        at: "00:00:00"
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.openweathermap
        response_variable: hourly
    sensor:
      - name: Forecast Hottest Time
        state: |
          {% set date = now().date()|string %}
          {{ (hourly['weather.openweathermap'].forecast | selectattr('datetime', 'search', date)
          | sort(attribute='temperature' , reverse=true))[0]['datetime'] }}
        device_class: timestamp

By giving the sensor the timestamp device class it allows you to use it in a time trigger for your automation instead of checking every hour.

trigger:
  - platform: time
    at: sensor.forecast_hottest_time

EDIT: Corrected typos

2025-02-28: In the time since this thread was opened the service weather.get_forecast has been deprecated in favor of weather.get_forecasts which can query multiple calendar entities. The answer above has been updated to use the new service/action.

3 Likes

thanks for this!
i have been fiddling with it but i am missing some basics i think.

the template part seems an automation, but i get an error if i add the “sensor” part in that automation

Message malformed: extra keys not allowed @ data['template']

i tried adding the “sensor” in the template.yaml but then i don’t see how the “response_variable hourly” is passed on the “sensor” in templates.yaml.
type or paste code here

It’s not you, it’s me… that should have been trigger not template I have corrected the typo above.

Thanks again! seems i got it doing something and i will try to adopt this technique in my automation.

had to change one thing though because i got an error .

changed to : - platform: time

maybe helpful for someone:
i ended up not triggering like the method above. that is a slick way to do it but i also need to deactivate the ventilation when it is not one of the warmer hours. so i ended up triggering per hour (5 minutes over) and checking an “array” that holds the warmer hours like for example : [‘12’,‘11’,‘14’,‘13’,‘15’]

template.yaml (or template dictionary if you add it in configuration.yaml. but here as if we use template.yaml (that is !includ(ed) in configuration.yaml):

- trigger:
    - platform: time
      at: "00:01:00"
  action:
    - service: weather.get_forecast
      data:
        type: hourly
      target:
        entity_id: weather.openweathermap
      response_variable: hourly
  sensor:
    - name: forecast_hottest_hours
      state: |-
        {%- set date = now().date()|string %}
        {%- set hours = namespace(hour=[]) %}
        {%- set forecastlist = (hourly.forecast | selectattr('datetime', 'search', date)  |  sort(attribute='temperature' , reverse=true)) %}  
        {% for index in forecastlist[:6] %}
        {% set hours.hour = hours.hour + [as_timestamp(index.datetime)|timestamp_custom('%H')] %}
        {%- endfor %}   
        {{ hours.hour }}

then i could use it in an automation condition like :

alias: house_ventilation_automation_winter
description: ""
trigger:
  - platform: time_pattern
    minutes: "5"
condition: []
action:
  - if:
      - condition: template
        value_template: "{{ now().strftime("%H") in states('sensor.forecast_hottest_hours') }}"
    then:
~
~

thanks again Drew, your example helped me a lot (also in more global understanding of HA)

1 Like

update. as per 2023.12 weather.get_forecast is deprecated pending removal in 2024.06

so in order to get example in previous post to work, change “weather.get_forecast” to “weather.get_forecasts”
this wil intorduce an extra level in the dict / json ( being the same as the entity_is it seems. so “weather.openweathermap” in this example … but will be “weather.forecast_home” when using “weather_home” )

so you need to change :
{%- set forecastlist = (hourly.forecast | selectattr(‘datetime’, ‘search’, date) | sort(attribute=‘temperature’ , reverse=true)) %}

to
{%- set forecastlist = (hourly[‘weather.openweathermap’].forecast | selectattr(‘datetime’, ‘search’, date) | sort(attribute=‘temperature’ , reverse=true)) %}

Hi,
i could need some help with this.
I try to get the warmest hour of the day, as discussed in this thread. However, the code above always returns the error “hourly is not defined”.

I tried around now for several hours - ended up tried the sample-code in the documentation
Weather - Home Assistant (home-assistant.io)
and even this code does not work in the developertools → template-tab.

I am using openweathermap with API 3.0.
If i execute the serice in the developer tools with this code:

service: weather.get_forecasts
target:
  entity_id: weather.openweathermap
data:
  type: hourly

I get a response like this

weather.openweathermap:
  forecast:
    - datetime: "2024-06-15T13:00:00+00:00"
      condition: cloudy
      temperature: 19.8
      pressure: 1009
      cloud_coverage: 93
      wind_speed: 22.03
      wind_bearing: 245
      uv_index: 3.12
      precipitation_probability: 0
      precipitation: 0
      apparent_temperature: 19.2
      dew_point: 9.1
      wind_gust_speed: 35.78
      humidity: 50
    - datetime: "2024-06-15T14:00:00+00:00"
      condition: cloudy
      temperature: 19.7
      pressure: 1009
      cloud_coverage: 89
      wind_speed: 21.92
      wind_bearing: 250
      uv_index: 3.29
      precipitation_probability: 0
      precipitation: 0
      apparent_temperature: 19
      dew_point: 8.4
      wind_gust_speed: 33.98
      humidity: 48

I now want to get the warmest hour of the current day from.
But the variable, that stores the output of the service seems to be empty.

hi,
i think you are missing the : response_variable: hourly
i don know if or how you can test this (completely) in the developer tools for i don think it supports “template code” or i don know how…

i moved away from openweather… but if you want to use it replace the ‘weather.forecast_home’ with ‘weather.openweathermap’

and copy this into the template.yaml or under the template section in your configuration.yaml (if you did not split te template section into a seperate file)

change the trigger time.
this sould result in a ‘forecast_hottest_hours’ sensor wit a value like [‘13’, ‘14’, ‘16’, ‘15’, ‘17’, ‘12’]
if you need only hottest hour, change the 6 into 1

- trigger:
    - platform: time
      at: "00:02:00"
  action:
    - service: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.forecast_home
      response_variable: hourly
  sensor:
    - name: forecast_hottest_hours
      state: |-
        {%- set date = now().date()|string %}
        {%- set hours = namespace(hour=[]) %}
        {%- set forecastlist = (hourly['weather.forecast_home'].forecast | selectattr('datetime', 'search', date)  |  sort(attribute='temperature' , reverse=true)) %}  
        {% for index in forecastlist[:6] %}
        {% set hours.hour = hours.hour + [as_timestamp(index.datetime)|timestamp_custom('%H')] %}
        {%- endfor %}   
        {{ hours.hour }}
4 Likes

Hi.

Thanks. Using this one:

template:
- trigger:
    - platform: time
      at: "00:02:00"
  action:
    - service: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.xy
      response_variable: hourly
  sensor:
    - name: forecast_hottest_hour
      state: |-
        {%- set date = now().date()|string %}
        {%- set hours = namespace(hour=[]) %}
        {%- set forecastlist = (hourly['weather.xy'].forecast | selectattr('datetime', 'search', date)  |  sort(attribute='temperature' , reverse=true)) %}  
        {% for index in forecastlist[:1] %}
        {% set hours.hour = hours.hour + [as_timestamp(index.datetime)|timestamp_custom('%H')] %}
        {%- endfor %}   
        {{ hours.hour }}

Background: Want to use the warmest our to trigger an automation for my heat pump. The result of the sensor is e.g. (14).
Is it possible to get the result as a timestamp? I added

  device_class: timestamp

but this result in “unknown”.

Any idea(s)?

The expected state for a timestamp sensor is a datetime string. So you need to change your output to the fulle datetime string from the forecast instead of just the hour.

2 Likes

Great thanks.
Can you kindly check if this will work?
AI generated :frowning:

template:
  - trigger:
      - platform: time
        at: "00:02:00"
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.xy
        response_variable: hourly
  - sensor:
      - name: forecast_hottest_hour
        state: |-
          {%- set date = now().date()|string %}
          {%- set hottest_forecast = (hourly['weather.xy'].forecast 
                                      | selectattr('datetime', 'search', date)  
                                      | sort(attribute='temperature', reverse=true) 
                                      | first) %}
          {{ hottest_forecast.datetime }}
        device_class: timestamp

Thanks for this!

I was searching for a way to have the min/max temp for the next day + the timestamp and bumped on this topic so I have the time already.
Could this also be used to get the temperature at that time?

Sure.

template:
  - trigger:
      - platform: time
        at: "00:00:00"
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.example
        response_variable: hourly
      - variables:
          today: |
            {% set remaining = 24 - now().hour %}
            {{ hourly['weather.forecast_home'].forecast[:remaining] | list }}
          hottest_obj: |
            {{ (today | sort(attribute='temperature', reverse=true))[0] }}
          coldest_obj: |
            {{ (today | sort(attribute='temperature'))[0] }}
    sensor:
      - name: Forecast Hottest Time
        state: "{{ hottest_obj.datetime }}"
        device_class: timestamp
      - name: Forecast Hottest Temp
        state: "{{ hottest_obj.temperature }}"
        device_class: temperature
      - name: Forecast Coldest Time
        state: "{{ coldest_obj.datetime }}"
        device_class: timestamp
      - name: Forecast Coldest Temp
        state: "{{ coldest_obj.temperature }}"
        device_class: temperature
2 Likes

Thanks man!
There were some indentation errors:

- trigger:
    - platform: time
      at: "00:00:00"
  action:
    - service: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.home
      response_variable: hourly
    - variables:
        hottest_obj: |
          {{ (hourly['weather.home'].forecast[24]
          | sort(attribute='temperature' , reverse=true))[0] }}
        coldest_obj: |
          {{ (hourly['weather.home'].forecast[24]
          | sort(attribute='temperature'))[0] }}
  sensor:
    - name: Forecast Hottest Time
      state: "{{ hottest_obj.datetime }}"
      device_class: timestamp

    - name: Forecast Hottest Temp
      state: "{{ hottest_obj.temperature }}"
      device_class: temperature
      unit_of_measurement: "degrees"

    - name: Forecast Coldest Time
      state: "{{ coldest_obj.datetime }}"
      device_class: timestamp

    - name: Forecast Coldest Temp
      state: "{{ coldest_obj.temperature }}"
      device_class: temperature
      unit_of_measurement: "degrees"

but there is still an error concerning the temp sensors and I cannot find how to fix it.

ValueError: Sensor sensor.forecast_hottest_temp has device class ‘temperature’, state class ‘None’ unit ‘degrees’ and suggested precision ‘None’ thus indicating it has a numeric value; however, it has the non-numeric value: ‘’ (<class ‘str’>)

and also

  • Template variable warning: ‘str object’ has no attribute ‘datetime’ when rendering ‘{{ hottest_obj.datetime }}’
  • Template variable warning: ‘str object’ has no attribute ‘temperature’ when rendering ‘{{ hottest_obj.temperature }}’

Does this work?


- trigger:
    - platform: time
      at: "00:00:00"
  action:
    - service: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.home
      response_variable: hourly
    - variables:
        hottest_obj: |
          {{ (hourly['weather.home'].forecast | sort(attribute='temperature', reverse=true))[0] }}
        coldest_obj: |
          {{ (hourly['weather.home'].forecast | sort(attribute='temperature'))[0] }}
  sensor:
    - name: Forecast Hottest Time
      state: "{{ hottest_obj.datetime }}"
      device_class: timestamp

    - name: Forecast Hottest Temp
      state: "{{ hottest_obj.temperature }}"
      device_class: temperature
      unit_of_measurement: "°C"

    - name: Forecast Coldest Time
      state: "{{ coldest_obj.datetime }}"
      device_class: timestamp

    - name: Forecast Coldest Temp
      state: "{{ coldest_obj.temperature }}"
      device_class: temperature
      unit_of_measurement: "°C"

2 Likes

Hi, thanks for helping! :+1:

I don’t think that it is now the unit_of_measurement anymore but I don’t understand much of this templating mambo jambo and have looked left and right to get a better understanding - alas :blush:

Maybe I better open a new thread for this since I haven’t found a way to achieve this and it might serve others as well.

1 Like

Drop a message where to find the thread.
Still struggling to get the result of „warmest hour of day“ as timestamp.

This post from @Didgeridrew works though.

I did get the timestamp but also wanted the get the temperature at that specific moment.

1 Like

Great thanks.
Can confirm that this is working. Thanks so much!