Using Templates as Conditions for Script Actions (SOLVED)

Hello all!

I’m just starting to dip my toes into the power of templating in Home Assistant. I’m a little confused on the proper syntax and usage of these templates in scripts though.


SITUATION:
I have several LED strips around the house, and each has several presets as assigned by WLED. I have also set a few “Alarm” presets, so when something happens, I am alerted through the LEDs. After a few seconds, the lights go back to a hard-coded preset.

While I have a few such alarms, here’s a specific example of the one I’m working on now. Rather than flipping on all the lights to go upstairs to bed, I have a breakbeam sensor at the bottom of my staircase. When I pass this, it turns on a light to see the stairs, and other lights along the way. Based on time of day, it can also turn off other house lights (i.e. for when I go to bed). Works great, but limits the number of presets I can use, since the return preset is hard-coded.

I recently developed a “previous state” script using input_text variables for a similar automation for my office LEDs. This automation works great, as it stores the current playlist in the input_text, then restores it after a delay.


PROBLEM:
Now I’m trying to apply the above logic to a more complex situation. I’m almost always running my office LEDs, but I would like similar logic for my stiarcase, which I run much less frequently. I have two scripts to control this: one to store the state before the automation runs, and one to restore the state:


stairway_leds_save_state:
  alias: Save State
  sequence:
    - action: input_text.set_value
      data_template: 
        entity_id: input_text.stairway_upstairs_leds_state_prev
        value: '{{ states("light.stairway_upstairs_leds") }}'

    - action: input_text.set_value
      data_template:
        entity_id: input_text.stairway_upstairs_leds_preset_prev
        value: '{{ states("select.stairway_upstairs_leds_preset") }}'

stairway_leds_restore_state:
  alias: Restore State
  sequence:
    - if:
      condition: '{{ states("input_text.stairway_upstairs_leds_state_prev") == 'on' }}'
    - then: 
        - action: light.turn_on
          target:
            entity_id:  light.stairway_upstairs_leds

        - action: select.select_option
          target:
            entity_id: select.stairway_upstairs_leds_preset
          data:
            option: '{{ states("input_text.stairway_upstairs_leds_preset_prev") }}'
    

My thought process is that if the saved state prior to triggering the automation is ‘on’, then turn those lights back on, then apply the preset before the automation changed it. My automation automatically turns off everything after a delay, hence the need for the ‘on’ check. I was hoping I could use a select_option like I did for the preset, but again, I’m a noob to this and haven’t figured all out yet :smiley:

I’m getting an error in the second script to restore the state. What am I missing? I’m sure this is just a silly syntax error on my part.

Thanks in advance for your help!!

try this your if is really old you need to add what type of condition you need and the value

stairway_leds_restore_state:
  alias: Restore State
  sequence:
    - if:
        - condition: template
          value_template: "{{ states('input_text.stairway_upstairs_leds_state_prev') == 'on' }}"
      then:
        - service: light.turn_on
          target:
            entity_id: light.stairway_upstairs_leds

        - service: select.select_option
          target:
            entity_id: select.stairway_upstairs_leds_preset
          data:
            option: "{{ states('input_text.stairway_upstairs_leds_preset_prev') }}"

is a good measure to add delay

this is optional

stairway_leds_restore_state:
  alias: Restore State
  sequence:
    - if:
        - condition: template
          value_template: "{{ states('input_text.stairway_upstairs_leds_state_prev') == 'on' }}"
      then:
        - service: light.turn_on
          target:
            entity_id: light.stairway_upstairs_leds
        - delay: "00:00:02"
        - if:
            - condition: template
              value_template: >
                {% set saved_preset = states('input_text.stairway_upstairs_leds_preset_prev') %}
                {% set preset_options = state_attr('select.stairway_upstairs_leds_preset', 'options') %}
                {{ saved_preset in preset_options }}
          then:
            - service: select.select_option
              target:
                entity_id: select.stairway_upstairs_leds_preset
              data:
                option: "{{ states('input_text.stairway_upstairs_leds_preset_prev') }}"

you can try both

Thanks! Had a couple other syntax error, but after cleaning them up, everything looks good!!

Thanks again!!