Beginner's templating question

that’s a string

that’s an int


You can’t convert datetime to an int without using a forloop, so if you want to keep your code the way it is, you need to adjust {% set end = start + 86400000 %} to be a datetime string. And to configure that line, we’d need to know the format of datetime.

EDIT: to further clarify, post a screenshot of your attributes for weather.home

Dear Petro
Thank you for your response. Here is the screenshot of the attributes of weather.home:

How can I adjust the datetime string?

You can’t adjust the datetime string, so we adjust what you’re comparing to.

  - platform: template
    sensors:
      day_max_temp:
        friendly_name: "Max. Tagestemperatur"
        unit_of_measurement: "°C"
        entity_id: weather.home
        value_template: >
          {% set format = '%Y-%m-%dT%H:%M:%S+00:00' %}
          {% set start = now().replace(hour=0,minute=0,second=0, microsecond=0) %}
          {% set end = (start + timedelta(days=1)) | timestamp_custom(format, false) %}
          {% set start = start | timestamp_custom(format, false) %}
          {{ state_attr('weather.home', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | max }}

Thank you Petro
Hmmmm… When I copy and paste your configuration to the Template-Tool I still get the an error, but a little different one:

TypeError: '<=' not supported between instances of 'int' and 'datetime.datetime'

Am I still comparing apples with pears?

1 Like

Just in case someone else has this same issue, I changed a bit the code to make it work (I’m a n00b, so it might be a better way of doing this)

This works with Accuweather, but you can adjust it to you service

template:
    - sensor:
      - name: "Today Maximum Temperature"
        unique_id: "today_maximum_temperature"
        device_class: temperature
        unit_of_measurement: "°F"
        state: >
            {% set start = now().replace(hour=0,minute=0,second=0, microsecond=0) %}
            {% set end = (start + timedelta(days=1)) %}
            {% set start = start.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
            {% set end = end.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
            {{ state_attr('weather.home', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | max }}
3 Likes

Just wanted to thank DaveH for this. Just what I was looking for. Well, almost.
Here is the code modified for Open Weather Map showing minimum temp today with a second sensor for showing minimum forecast temperature over the next 24 hours which I plan to use as a frost warning for garden plant protection purposes


template:
  - sensor:
    - name: "Today Minimum Temperature"
      unique_id: "today_minimum_temperature"
      device_class: temperature
      unit_of_measurement: "°C"
      state: >
          {% set start = now().replace(hour=0,minute=0,second=0, microsecond=0) %}
          {% set end = (start + timedelta(days=1)) %}
          {% set start = start.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
          {% set end = end.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
          {{ state_attr('weather.openweathermap', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | min }}
#
  - sensor:
    - name: "24H Minimum Temperature"
      unique_id: "24h_minimum_temperature"
      device_class: temperature
      unit_of_measurement: "°C"
      state: >
          {% set start = now() %}
          {% set end = (start + timedelta(days=1)) %}
          {% set start = start.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
          {% set end = end.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
          {{ state_attr('weather.openweathermap', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | min }}