Condition Else If -- help

Hi there! Can you help me understand how to write a proper Condition template?

I have several lights scripts written that triggers on a specific date that goes on loop.

So let’s say these two are my current “Light Themes”:

#Birthday
birthdaytlights:
  alias: Birthday Lights
  sequence:
  - data:
      brightness_pct: '80'
      color_name: magenta
      entity_id: light.bulb1
      transition: '2'
    service: light.turn_on
  - data:
      brightness_pct: '80'
      color_name: cyan
      entity_id: light.bulb2
      transition: '2'
    service: light.turn_on
  - delay: '00:00:05'

  - data:
      brightness_pct: '80'
      color_name: cyan
      entity_id: light.bulb1
      transition: '2'
    service: light.turn_on
  - data:
      brightness_pct: '80'
      color_name: magenta
      entity_id: light.bulb2
      transition: '2'
    service: light.turn_on
  - data:
      entity_id: script.birthdayloop
    service: script.turn_on
#Anniversary
annivtlights:
  alias: Anniv Lights
  sequence:
  - data:
      brightness_pct: '80'
      color_name: white
      entity_id: light.bulb1
      transition: '2'
    service: light.turn_on
  - data:
      brightness_pct: '80'
      color_name: yellow
      entity_id: light.bulb2
      transition: '2'
    service: light.turn_on
  - delay: '00:00:05'

  - data:
      brightness_pct: '80'
      color_name: yellow
      entity_id: light.bulb1
      transition: '2'
    service: light.turn_on
  - data:
      brightness_pct: '80'
      color_name: white
      entity_id: light.bulb2
      transition: '2'
    service: light.turn_on
  - data:
      entity_id: script.annivloop
    service: script.turn_on

and I have another set of scripts that loops it.

birthdayloop:
  alias: Birthday Loop
  sequence:
  - delay: '00:00:05'
  - service: script.turn_on
    data:
      entity_id: script.birthdaylights
annivloop:
  alias: Anniv Loop
  sequence:
  - delay: '00:00:05'
  - service: script.turn_on
    data:
      entity_id: script.annivlights

What I’m trying to do is to combine the “loop” scripts into one if possible, using IF ELSE condition. Like if birthday lights is off, it will ignore it and check if anniv lights is on. Basically loop whatever is active, and also turn off the scripts on a specific time at ‘21:00’.

This is my current loop test script, and it’s not looking good at all.

loop:
  alias: Test Loop
  sequence:
  condition:
  - condition: template
    value_template: "{{ is_state('script.loop','on') }} "
    action:
      service_template: >
        {% if is_state('script.birthdaylights','on') %} script.birthdaylights
        {% else is_state('script.annivlights','on') %} script.annivlights
        {% else is_state('script.sundaylights','on') %} script.sundaylights
        {% else is_state('script.holidaylights','on') %} script.holidaylights
        {% else %} script.turn_off {% endif %}

Thanks!

Do it by passing a variable. E.g.:

#Birthday
birthdaytlights:
  alias: Birthday Lights
  sequence:
  ...
  - data:
      entity_id: script.birthdaytlights
    service: script.loop

loop:
  alias: Loop
  sequence:
  - delay: '00:00:05'
  - service_template: "{{ entity_id }}"

At least that’s how you can loop each of the first scripts using a common loop script.

For the stopping part, not exactly sure what you want, but maybe:

loop:
  alias: Loop
  sequence:
  - condition: template
    value_template: "{{ now().hour < 21 }}"
  - delay: '00:00:05'
  - service_template: "{{ entity_id }}"

Thanks, I’ll try this in a bit.

Because the loop is in script, it will not stop even if I turn the lights off (the script will turn the lights on and continue). So to turn off the lights, it needs the script [script.turn_off and a light.turn_off] (which I already have. But I just thought it will be more neat to add this in the condition loop too if possible?

I’m not completely following you. The scripts you posted above have nothing for stopping or turning off the lights. Even the combined loop script you proposed really didn’t have anything (and wouldn’t have worked as-is in any case.)

So, it’s possible to do other things, but you’d have to be more specific about what exactly you want.

Sorry. So this is one my light themes I have.

#Birthday
birthdaylights:
  alias: Birthday Lights
  sequence:
  - data:
      brightness_pct: '80'
      color_name: magenta
      entity_id: light.bulb1
      transition: '2'
    service: light.turn_on
  - data:
      brightness_pct: '80'
      color_name: cyan
      entity_id: light.bulb2
      transition: '2'
    service: light.turn_on
  - delay: '00:00:05'

  - data:
      brightness_pct: '80'
      color_name: cyan
      entity_id: light.bulb1
      transition: '2'
    service: light.turn_on
  - data:
      brightness_pct: '80'
      color_name: magenta
      entity_id: light.bulb2
      transition: '2'
    service: light.turn_on
  - data:
      entity_id: script.birthdayloop
    service: script.turn_on

All the themes have the same format of script. And to loop this, I’m using a loop for each light theme. For this script.birthday, I use this script:

birthdayloop:
  alias: Birthday Loop
  sequence:
  - delay: '00:00:05'
  - service: script.turn_on
    data:
      entity_id: script.birthdaylights

And to stop the loop, I use this automation:

- id: light_themes_off
  trigger:
  - at: '21:00'
    platform: time
  condition:
    condition: or
    conditions:
    - condition: state
      entity_id: script.birthdaylights
      state: 'on'
    - condition: state
      entity_id: script.annivlights
      state: 'on'
  action:
  - data:
      entity_id: light.bulb1
    service: light.turn_off
  - data:
      entity_id: light.bulb2
    service: light.turn_off
  - data:
      entity_id: script.birthdaylights
    service: script.turn_off
  - data:
      entity_id: script.annivlights
    service: script.turn_off

So it’s like, I have to create a loop for each light theme. I want to create one loop script that will loop any active light theme and ignore all the inactive themes.

The condition I added to the loop script will take care of stopping all the loops at (or shortly after) 21:00. It won’t, however, turn the lights off. Assuming these scripts are just controlling light.bulb1 and light.bulb2, maybe this would do it all:

loop:
  alias: Loop
  sequence:
  - delay: '00:00:05'
  - service_template: >
      {% if now().hour < 21 %}
        {{ entity_id }}
      {% elif is_state('script.turn_lights_off', 'off') %}
        script.turn_lights_off
      {% else %}
        script.nop
      {% endif %}

turn_lights_off:
  alias: Turn lights off
  sequence:
    - service: light.turn_off
      entity_id:
        - light.bulb1
        - light.bulb2

nop:
  alias: No operation
  sequence:

Not 100% sure this will work. You’ll need to test it and maybe adjust a bit. But hopefully it gives you an idea.

Thanks!

The loop is working, but manually turning off the lights turn_lights_off: doesn’t turn off the active script. The loop will keep turning the lights on and continue executing the active loop.

What should I add to stop the current loop along with the lights?

How do you add hours and minutes? I tried changing {% if now().hour < 21 %} to {% if now().time == '19:30' %} and it wouldn’t loop.

...
  - data:
      entity_id: script.christmaslights
    service: script.themesloop

themesloop:
  alias: Themes Loop
  sequence:
  - delay: '00:00:00'
  - service_template: >
      {% if is_state('sensor.time','19:30') %}
        {{ entity_id }}
      {% elif is_state('script.light_themes_off', 'off') %}
        script.light_themes_off
      {% endif %}

light_themes_off:
  alias: Light Themes Off
  sequence:
    - service: light.turn_off
      entity_id:
        - light.test1
        - light.test2

Actually I think the time works now – it turns off the light and the loop at a specific time. I’ll continue testing it. I restarted Hass and the loop worked again.

And I also changed the now().hour with {% if is_state('sensor.time','00:00') %}.

Thanks @pnbruckner