Automation value_template not working - help needed

Hi…

I have run into a problem and breaking my head last 24 hours, unable to figure out the right syntax to get it working. The validation part goes through but I get run time error.

The code is :slight_smile:

###  SET Input Boolean Status and Timer

- alias: Set Status and Timer 1
  trigger:
    - platform: state
      entity_id: 
        - input_boolean.hall_tv
        - input_boolean.home_theater
        - input_boolean.dinnig_hall
        - input_boolean.dining_radio

  action:
    - service_template: script.turn_on
      data_template:      
         entity_id: '{{"script." + trigger.entity_id.split(".")[1] + "_" + trigger.to_state.state }}'

    - service_template: '{{"input_boolean.turn_" + trigger.to_state.state }}'
      data_template:
        entity_id: '{{"input_boolean." + trigger.entity_id.split(".")[1] + "_status" }}'

    - service: input_number.set_value
      data_template:
        entity_id: '{{"input_number." + trigger.entity_id.split(".")[1] + "_timer" }}'
        value_template: 
              {% if trigger.to_state.state == 'on' %} 
                 '{{float (states.input_number.auto_off_timer.state) }}'
              {% else %} '0'
              {% endif %}

###---------------------------------------------------------------

your field name is wrong at the value_template line. Also, you can optimize your code a bit:

  • Nothing major, your code works but you don’t need to split the entity_id to get the object_id when you have a state object (to_state, from_state).
  • You don’t need to add strings together for some of these. They can be pulled out of the template.
  • The value_template field needs to be changed to ‘value’. You already indicate that the whole data section is a template section via the ‘data_template’ field.
  • You need an > to indicate a multiline template inside the value field.
  • You had quotes around your output template ‘{{float (states.input_number.auto_off_timer.state) }}’. You don’t need quotes here. You’re already templating. You just need the template output indicators {{}}.
  • You should use the states() method over the the state object when you can. i.e. states(entity_id) vrs states.domain.object_id.state.
  • You should use filters over functions when you can. Not really a big one here, but some have more functionality. Filters are indicated by the pipe symbol. Take a look at Jinja filters to find out more information.
  • You don’t need quotes around your else statements return. Everything that is not inside a template is treated as a string.

With all those changes, this should work:

###  SET Input Boolean Status and Timer

- alias: Set Status and Timer 1
  trigger:
    - platform: state
      entity_id: 
        - input_boolean.hall_tv
        - input_boolean.home_theater
        - input_boolean.dinnig_hall
        - input_boolean.dining_radio

  action:
    - service_template: script.turn_on
      data_template:      
         entity_id: 'script.{{ trigger.to_state.object_id }}_{{ trigger.to_state.state }}'

    - service_template: 'input_boolean.turn_{{ trigger.to_state.state }}'
      data_template:
        entity_id: 'input_boolean.{{ trigger.to_state.object_id }}_status'

    - service: input_number.set_value
      data_template:
        entity_id: 'input_number.{{ trigger.to_state.object_id }}_timer'
        value: >
          {% if trigger.to_state.state == 'on' %} 
            {{ states('input_number.auto_off_timer') | float }}
          {% else %}
            0
          {% endif %}
###---------------------------------------------------------------

Hi Petro,

[SOLVED] That worked well. Don’t know how to thank you for pointing out the syntax errors, which helps me learn a lot and looks like there is a long way to go for me to learn it as good as you have guided. Thanks a million.

Could you help me also by pointing out where I can pick up the best way to code these scripts. Some online tutorials…?

You should learn yaml, and jinja.

Yaml is the format of the file. Jinja is all the templating. I don’t think there is any jinja tutorials. You can always play around in the template editor. Also, the documents for templating will help alot.

Jinja

http://jinja.pocoo.org/docs/2.10/

Specific to home assistant:

Thanks for the pointers. Will learn fast to get there.