How to use parameters in data_template inside a script

Hello everybody

I wish you all a happy new year !
and I need a little help from you !!!

In an automation I call many times a script to set an input_datetime.
This is a part of the automation:

  - service: script.heure_radiateur
    data:
      radiateur: salon
      moment: debut
  - service: script.heure_radiateur
    data:
      radiateur: parents
      moment: debut
  - service: script.heure_radiateur
    data:
      radiateur: bureau
      moment: debut
  - service: script.heure_radiateur
    data:
      radiateur: camille
      moment: debut

and the script is the following:

heure_radiateur:
  mode: queued
  sequence: 
  - service: system_log.write
    data:
      level: info
      message: "Début Heure Radiateur: {{radiateur}} - {{moment}}"
  - choose:
    - conditions: "{{ moment == 'debut' }}"
      sequence:
        - service: system_log.write
          data:
            level: info
            message: "Initialisation radiateur début"
        - service: system_log.write
          data:
            level: info
            message: "datetime: 'input_datetime.{{radiateur}}_debut'"
        - service: input_datetime.set_datetime
          data_template:
            entity_id: "input_datetime.{{radiateur}}_debut"
            time: >
              {% set location = '{{radiateur}}' %}
              {{state_attr ('input_datetime.'+location+'_6_matin_debut', 'timestamp')|int |timestamp_custom('%H:%M', False)}}                    
        - service: system_log.write
          data_template:
            level: info
            message: "Initialisation radiateur faite à {{states('input_datetime.{{radiateur}}_debut')|timestamp_custom('%H:%M', False)}}"
  - service: system_log.write
    data:
      level: info
      message: "Fin Heure Radiateur: {{radiateur}}"

For the time being, the parameter ‘moment’ is of no use. but it will be in the future.
I want to get the value of an input_datetime (related to the radiateur -heater in english) to set another input_datetime.

The issue is with the service set_datetime.
The entity_id is correctly evaluated.
but the time is always set to 00:00.

and the last message in the log is also weird. I get "“Initialisation radiateur faite à unknown”

If I change {% set location = ‘{{radiateur}}’ %}
and put {% set location = ‘salon’ %}
I get the correct input_datetime linked to the heater salon

My problem is in the use of the parameter of the script {{radiateur}} in the jinja.

I can copy the script in the first automation (to replace the call to the script and immediatly call the service set_datetime),and it works well.
But I prefer to write a script and call it many times it will help for the maintenance.

Any idea how to solve the issue ?

            time: >
              {{state_attr('input_datetime.' ~ radiateur ~ '_6_matin_debut', 'timestamp') | timestamp_custom('%H:%M', False)}} 

Thanks a lot !
If works !

1 Like