If-then-else templating in automation

Hi all, I am trying to learn some templating in automations. Here is my code, what am I doing wrong?
Basically I want to check if a backlight is on, if it is then just do what the automation is supposed to do (set a value to a input_number), otherwise turn on that backlight (light.displ_bedroom_backlight).

    action:
    - service_template: >
    	{% if (is_state("light.displ_bedroom_backlight", "on")) -%}
    	input_number.set_value
    	{%- else -%}
    	light.turn_on
    	{%- endif %}
    - data_template: >
    	{% if (is_state("light.displ_bedroom_backlight", "on")) -%}
    	entity_id: 'input_number.hasp_displ_bedroom_activepage'
        value: '{{states.input_number.hasp_displ_bedroom_pagebutton1page.state|int}}'
        {%- else -%}
        entity_id: light.displ_bedroom_backlight
        value: 'on'

This is what i had before (working code):

   action:
    - service: input_number.set_value
      data_template:
        entity_id: 'input_number.hasp_displ_bedroom_activepage'
        value: '{{states.input_number.hasp_displ_bedroom_pagebutton1page.state|int}}'

Any help is super appreciated!
Thanks :slight_smile:

What you’re trying to template would be extremely difficult. Just use the choose method for automations without templates.

1 Like

Thank you, I wasn’t aware of it! I will give it a try :slight_smile:

Sorry, just need some kickstart on this, could you give a hand :)?

    action:
    - choose:
      - conditions:
        - condition: template
          value_template: (is_state("light.displ_bedroom_backlight", "on"))
        sequence:
        - service: light.turn_on
          entity_id: light.displ_bedroom_backlight
     default:
      - service: input_number.set_value
        data_template:
          entity_id: 'input_number.hasp_displ_bedroom_activepage'
          value: '{{states.input_number.hasp_displ_bedroom_pagebutton1page.state|int}}'

default needs to be in line with choose

EDIT: Your value_template is incorrect too. But you don’t need a value template, just use a normal state condition.

Something like this?

    action:
    - choose:
      - conditions:
        - condition: template
          state: off
          entity_id: light.displ_bedroom_backlight
        sequence:
        - service: light.turn_on
          entity_id: light.displ_bedroom_backlight
      default:
       - service: input_number.set_value
         data_template:
           entity_id: 'input_number.hasp_displ_bedroom_activepage'
           value: '{{states.input_number.hasp_displ_bedroom_pagebutton1page.state|int}}'

yep try it out

Ok I get the following error:
* Invalid config for [automation]: [state] is an invalid option for [automation]. Check: automation->action->0->choose->0->conditions->0->state. (See ?, line ?).
I guess the mistake is here?:

        - condition: template
          state: off
          entity_id: light.displ_bedroom_backlight

is that a template condition or a state condition?

It should be a state condition
EDIT: even putting state instead of template yields the same error.

are you sure it’s the same error? That would change what’s after Check:


    Invalid config for [automation]: [state] is an invalid option for [automation]. Check: automation->action->0->choose->0->conditions->0->state. (See ?, line ?).
    Invalid config for [automation]: [value] is an invalid option for [automation]. Check: automation->action->0->choose->0->conditions->0->value. (See ?, line ?).
    Invalid config for [automation]: expected str for dictionary value @ data['action'][0]['choose'][0]['conditions'][0]['state']. Got None. (See ?, line ?).

That’s not the same error ;).

put quotes around the word ‘off’

1 Like

Thank you so much for walking me through my mistakes. That’s the best way to learn and now I can reproduce the “choose” method for all my other automations!

1 Like

Sorry @petro , I need to take advantage of your patience once again. I realized that even if a light is dimmed, it is still considered on (obviously). So I have to modify my automation to check if the light is below 255 brightness instead of checking if the light is “off”. Here is my proposal which obviously doesnt work:

    action:
    - choose:
      - conditions:
        - condition: template
          data_template: "{{ state_attr('light.displ_bedroom_backlight', 'brightness')|int < 255 }}"
          entity_id: light.displ_bedroom_backlight
        sequence:
        - service: light.turn_on
          data:
            entity_id: light.displ_bedroom_backlight
            brightness: 255
      default:
       - service: input_number.set_value
         data_template:
           entity_id: 'input_number.hasp_displ_bedroom_activepage'
           value: '{{states.input_number.hasp_displ_bedroom_pagebutton1page.state|int}}'
    Invalid config for [automation]: [state] is an invalid option for [automation]. Check: automation->action->0->choose->0->conditions->0->state. (See ?, line ?).
    Invalid config for [automation]: [value] is an invalid option for [automation]. Check: automation->action->0->choose->0->conditions->0->value. (See ?, line ?).
    Invalid config for [automation]: expected str for dictionary value @ data['action'][0]['choose'][0]['conditions'][0]['state']. Got None. (See ?, line ?).
    Invalid config for [automation]: [data_template] is an invalid option for [automation]. Check: automation->action->0->choose->0->conditions->0->data_template. (See ?, line ?).

Ok I worked it out myself via the old “trial and error”:

    action:
    - choose:
      - conditions:
        - condition: template
          value_template: "{{ state_attr('light.displ_bedroom_backlight', 'brightness')|int < 255 }}"
        sequence:
        - service: light.turn_on
          data:
            entity_id: light.displ_bedroom_backlight
            brightness: 255
      default:
       - service: input_number.set_value
         data_template:
           entity_id: 'input_number.hasp_displ_bedroom_activepage'
           value: '{{states.input_number.hasp_displ_bedroom_pagebutton1page.state|int}}'
1 Like

@petro, I don’t understand why what is written after “default:” is executed anyway. I mean, if the condition is met (i.e. brightness<255), why the following part continues to be executed (the part after “default:”)?
EDIT: probably after the brightness is restored to 255 the script goes on because it reads now that is 255…?
EDIT2: restarting home assistant instead of just reloading the automations solved the issue!