BUG with this template

Am I in the right place in the forum

Hello, for several days I have been trying to resolve the malfunction of this template. I made several versions and carried out numerous tests, but without results. My goal is for the Then or Else switch to work correctly. I need help. THANKS

Purpose of these instructions: to validate whether the automation was executed today. If so, we stop. If not, we continue.

{% set last_run = states(‘input_datetime.last_off_cd_matin_interieur_1er_2e’) %}
{% if last_run == ‘unknown’ or last_run == ‘unavailable’ %}
{{ true }} # Si aucune valeur n’est disponible (cas initial ou erreur), retourner vrai
{% else %}
{% set last_run_date = strptime(last_run, “%Y-%m-%d %H:%M:%S”).date() %}
{% set now_date = now().date() %}
{{ last_run_date != now_date }} # Si les dates ne correspondent pas, retourne vrai
{% endif %}

Looks like TRUE is not considered by IF

Another clarification, the variable “input_datetime.last_off_cd_matin_interieur_1er_2e” is initialized at each execution of the automation by the following instructions:

action: input_datetime.set_datetime
metadata: {}
data:
datetime: >-
{{
(state_attr(‘automation.eclairage_fermeture_luminosite_exterieure_lux_atteint_2’,
‘last_triggered’) | as_datetime | as_local).strftime(‘%Y-%m-%d %H:%M:%S’) }}
target:
entity_id: input_datetime.last_off_cd_matin_interieur_1er_2e

This is a YAML comment. It starts with #

# Si aucune valeur n’est disponible (cas initial ou erreur), retourner vrai

You cannot put YAML comments inside a Jinja2 template (it will be treated as a literal string).

Change it to a Jinja2 comment. It starts with {# and ends with #}

{# Si aucune valeur n’est disponible (cas initial ou erreur), retourner vrai #}

Thank you, I applied the syntax adjustments and everything works fine.

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.