Trouble using a template with input_number.set_value

I have an automation that triggers from the home assistant complication on my watch. There are two switches that are being controlled. The first time the action is pressed, both switches should come on. The second time it’s pressed, one of them (bedside_lamp) turns off. The third time it’s pressed, the second (wild2) turns off and the first one (bedside_lamp) turns back on. The fourth time it’s pressed, both come on and the cycle restarts.

An input number (scent_increment) tracks the cycle iteration. It’s defined with min=0, max=4, step=1, default=1.

For the most part, this automation is working. The problem is when scent_increment == 1. Both switches correctly turn on, but the value doesn’t update (it should be set to 2, but it stays at 1). If I manually change the value to 2 on the front end, the automation proceeds correctly (that is, bedside_lamp turns off, scent_increment updates to 3). It also works correctly when scent_increment == 3.

Any thoughts??

- alias: Burst of Scent
  trigger:
  - platform: event
    event_type: ios.action_fired
    event_data:
      actionName: Burst of Scent
  mode: restart
  action:
  - data_template:
       entity_id: >
          {% if (states('input_number.scent_increment') | int) == 1 %}
             switch.bedside_lamp, switch.wild2
          {% elif (states('input_number.scent_increment') | int) == 2 %}
             switch.wild2 
          {% else %}
             switch.bedside_lamp
          {% endif %}  
    service: homeassistant.turn_on           
  - data_template:
       entity_id: >
          {% if (states('input_number.scent_increment') | int) == 2 %}
             switch.bedside_lamp 
          {% elif (states('input_number.scent_increment') | int) == 3 %}
             switch.wild2
          {% endif %}  
    service: homeassistant.turn_off          
  - service: input_number.set_value
    entity_id: input_number.scent_increment  
    data_template:
       value: >
          {% if (states('input_number.scent_increment') | int) == 3 %}
             1
          {% else %}   
             {{ (states('input_number.scent_increment') | int) + 1 }}
          {% endif %}        
  - delay: 0:30:00
  - service: homeassistant.turn_off
    entity_id:
    - switch.bedside_lamp
    - switch.wild2
  - service: input_number.set_value
    target:
       entity_id: input_number.scent_increment
    data:
       value: 1

Let’s think about the logic for a moment and hopefully you’ll see the issue.

When the counter(c) = 1 the following happens:

the first service call is performed with the “if” portion of the template since c = 1.

All good so far.

But then the very next step is checking that c = 2 or 3. BUT since c hasn’t been incremented yet it still = 1.

There is no option for the second service call to perform since both “if” and “elif” are false (c != 2 or 3). And since you gave it no default action (using “else”) at that point the automation ends.

Ah! I didn’t realize that one if thread would stop the automation from continuing. Thanks so much!

1 Like