Problème avec du Template pour avoir la température maximum de la journée

Mon problème

Bonjour je souhaiterais faire un sensor qui afficherais la température maximum du forecast de météofrance

Voici les attribus

temperature: 28.3
humidity: 45
pressure: 1016.8
wind_speed: 4
attribution: Data provided by Météo-France
forecast: 
- datetime: '2021-06-15T02:00:00+02:00'
  condition: cloudy
  temperature: 29.9
  templow: 12.1
  precipitation: 0
- datetime: '2021-06-16T02:00:00+02:00'
  condition: sunny
  temperature: 31.8
  templow: 14.3
  precipitation: 0
- datetime: '2021-06-17T02:00:00+02:00'
  condition: sunny
  temperature: 33.5
  templow: 16.6
  precipitation: 0.2
- datetime: '2021-06-18T02:00:00+02:00'
  condition: rainy
  temperature: 30.8
  templow: 19.3
  precipitation: 1
- datetime: '2021-06-19T02:00:00+02:00'
  condition: partlycloudy
  temperature: 29.4
  templow: 16.8
  precipitation: 0.3
- datetime: '2021-06-20T02:00:00+02:00'
  condition: lightning
  temperature: 30.8
  templow: 17.6
  precipitation: 1.1
- datetime: '2021-06-21T02:00:00+02:00'
  condition: lightning
  temperature: 25.6
  templow: 16.5
  precipitation: 1.3
- datetime: '2021-06-22T02:00:00+02:00'
  condition: rainy
  temperature: 22.2
  templow: 14.1
  precipitation: 0.9
- datetime: '2021-06-23T02:00:00+02:00'
  condition: partlycloudy
  temperature: 22.4
  templow: 13.1
  precipitation: 0.3
- datetime: '2021-06-24T02:00:00+02:00'
  condition: partlycloudy
  temperature: 23.3
  templow: 12.4
  precipitation: 0.1
- datetime: '2021-06-25T02:00:00+02:00'
  condition: sunny
  temperature: 25.2
  templow: 13.2
  precipitation: 0
- datetime: '2021-06-26T02:00:00+02:00'
  condition: partlycloudy
  temperature: 25.7
  templow: 14.1
  precipitation: 0.2
- datetime: '2021-06-27T02:00:00+02:00'
  condition: rainy
  temperature: 26.5
  templow: 14.4
  precipitation: 0.4
- datetime: '2021-06-28T02:00:00+02:00'
  condition: sunny
  temperature: 26.1
  templow: 14.4
  precipitation: 0.4
- datetime: '2021-06-29T02:00:00+02:00'
  condition: sunny
  temperature: 26.2
  templow: 13.9
  precipitation: 0.4

friendly_name: XXXXXX

Et si je fais un template

{{ state_attr('weather.XXXXXX', 'forecast') | map(attribute='temperature') | list | max }}

Cela me donne la valeur maximum de tous le forecast (33.5) et pas seulement du jour

Si quelqu’un à une idée afin de récupérer la température maximum du jour je serais preneur.


Salut,

Essaye ça

{{ state_attr('weather.XXXXXX', 'forecast') | map(attribute='temperature') | first }}

Il n’y a qu’une valeur par jour, donc pas besoin de chercher le max… Et le jour courant est forcement le premier

Tip top ça marche nickel merci @Pulpy-Luke enfin je vais pouvoir faire un scénario pour la canicule

Bonjour avec les récentes mise a jour j’aimerai savoir comment rajouter la valeur par défaut:

#############################################################
###     TEMPERATURE MIN/MAX DU JOUR                        ##
#############################################################
  - platform: template
    sensors:
      day_max_temp:
        friendly_name: "Max.temperature"
        unit_of_measurement: "°C"
        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.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | max }}

  - platform: template
    sensors:
      day_min_temp:
        friendly_name: "Min.temperature"
        unit_of_measurement: "°C"
        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.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='templow') | list | max }}

Après avoir ajouter default: ERREUR

 {% set end = (start + timedelta(days=1)) | timestamp_custom(format, false, default) %}
TemplateError('TypeError: timestamp_custom() takes from 1 to 4 positional arguments but 5 were given') while processing template '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, 0, default) %} {% set start = start | timestamp_custom(format, false) %} {{ state_attr('weather.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | max }}")' for attribute '_attr_native_value' in entity 'sensor.day_max_temp'
TemplateError('UndefinedError: 'default' is undefined') while processing template '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, default) %} {% set start = start | timestamp_custom(format, false) %} {{ state_attr('weather.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='templow') | list | max }}")' for attribute '_attr_native_value' in entity 'sensor.day_min_temp'

Pas d’idée ?

Logger: homeassistant.helpers.template
Source: helpers/template.py:1331
First occurred: 22:04:00 (12 occurrences)
Last logged: 22:06:00

Template warning: 'timestamp_custom' got invalid input '2022-06-08 00:00:00+02:00' when rendering 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.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='templow') | list | max }}' but no default was specified. Currently 'timestamp_custom' will return '2022-06-08 00:00:00+02:00', however this template will fail to render in Home Assistant core 2022.6
Template warning: 'timestamp_custom' got invalid input '2022-06-07 00:00:00+02:00' when rendering 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.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='templow') | list | max }}' but no default was specified. Currently 'timestamp_custom' will return '2022-06-07 00:00:00+02:00', however this template will fail to render in Home Assistant core 2022.6
Template warning: 'timestamp_custom' got invalid input '2022-06-08 00:00:00+02:00' when rendering 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.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | max }}' but no default was specified. Currently 'timestamp_custom' will return '2022-06-08 00:00:00+02:00', however this template will fail to render in Home Assistant core 2022.6
Template warning: 'timestamp_custom' got invalid input '2022-06-07 00:00:00+02:00' when rendering 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.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | max }}' but no default was specified. Currently 'timestamp_custom' will return '2022-06-07 00:00:00+02:00', however this template will fail to render in Home Assistant core 2022.6

C’est pas si compliqué … c’est marqué.
Personnellement je ne pense pas que ce soit un bonne solution que de mettre autre chose que 0 (tout court !) comme valeur et ce quelque soit le template…

Un extrrait chez moi

timestamp_custom("%j",0)

Et ça marche très bien

je viens d’appliquer mais j’ai encore un autre message maintenant, je me demande si j’ai tout bien fait

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:172
Integration: Template (documentation, issues)
First occurred: 10:07:10 (2 occurrences)
Last logged: 10:07:10

TemplateError('TypeError: '>=' not supported between instances of 'datetime.datetime' and 'int'') while processing template '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, 0,) %} {% set start = start | timestamp_custom(format, false, 0,) %} {{ state_attr('weather.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | max }}")' for attribute '_attr_native_value' in entity 'sensor.day_max_temp'
TemplateError('TypeError: '>=' not supported between instances of 'datetime.datetime' and 'int'') while processing template '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, 0,) %} {% set start = start | timestamp_custom(format, false, 0,) %} {{ state_attr('weather.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='templow') | list | max }}")' for attribute '_attr_native_value' in entity 'sensor.day_min_temp'

C’est un souci de format : tu cherches à comparer une date/heure avec une valeur entière …
Il faut découper ton template et le tester dans les outils dev

J’avoue ne pas très bien saisir la manière de procéder, a l’époque c’est un template trouver sur le net que j’aii trouver intéressant mais aujourd’hui… n’étant pas le créateur ni du métier je sèche…

Du coup, il faut procéder comme ça :

  • quel est ton jeu de données ?
  • que veux tu récupérer ?

Je voudrai récupéré la température en °C la plus haute du jour « J » et la plus basse du jour « J »

Oui j’avais bien l’idée. Ça réponds pas totalement aux questions précédentes : quel jeu de données par exemple

Bonjour

Il y a cela

HADailySensor

Daily Sensor custom component for Home Assistant. It takes aggregates an input sensor until midnight. Then it resets. Aggregation is configurable - available options are: minimum, maximum, sum, average (mean), median, standard deviation and variation.

Désolé @Pulpy-Luke mais je ne comprend pas la question
je voudrai juste récupéré ces infos dans weather.ville
image

Si tu reprends l’exemple au dessus :

{{ state_attr('weather.XXXXXX','forecast') | map(attribute='temperature') | first }}
{{ state_attr('weather.XXXXXX','forecast') | map(attribute='templow') | first }}

ça doit prendre le min max de la 1ere date

Donc je n’aurai pas besoin de definir la date: (j’essai d’apprendre :slight_smile: )

 {% 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) %}

Oui c’est exactement l’idée

D’accord, mais je suppose que ce n’est pas aussi simple si je venais simplement a supprimer les lignes précedentes ?

  - platform: template
    sensors:
      day_max_temp:
        friendly_name: "Max.temperature"
        unit_of_measurement: "°C"
        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.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | max }}

  - platform: template
    sensors:
      day_min_temp:
        friendly_name: "Min.temperature"
        unit_of_measurement: "°C"
        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.ville', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='templow') | list | max }}

il faut aussi remplacer la dernière ligne de ton sensor par 1 ligne de mon exemple, sinon les appels à ce que tu viens de commenter sont toujours là