[solved] Template that checks boolean and turn extra light on (or not)

I’m trying to put together an automation for which I guess I need a template.
This is the part of the action code:

service: light.turn_on
data:
  brightness_pct: 10
target:
  entity_id:
    - light.sr1

If I add another action with a condition, the rest of the automation will not run, right? (that is if the result is false)
So I guess I need a template in the action above that checks for that boolean and depending on it’s state it turns another light on.

Can someone please give me a hand with this?
Any help is highly appreciated!
N.

If you have another action that you want to perform, but only if a condition is met you can use either a Condition action or Choose action.

Option 1: Condition action
Actions are executed sequentially from top to bottom so only actions after the Condition action will be affected by it.

action:
  - service: light.turn_on
    data:
      brightness_pct: 10
    target:
      entity_id: light.sr1
  - condition: state
    entity_id: input_boolean.XXXX
    state: 'on'
  - service: light.turn_on
    data:
      brightness_pct: 20
    target:
      entity_id: light.sr2

Option 2: Choose action

action:
  - service: light.turn_on
    data:
      brightness_pct: 10
    target:
      entity_id: light.sr1
  - choose:
    - conditions:
        - condition: state
          entity_id: input_boolean.XXXX
          state: 'on'
      sequence:
        - service: light.turn_on
          data:
            brightness_pct: 20
          target:
            entity_id: light.sr2
1 Like

Hi Drew, thank you very much for picking this one up and helping!

I was trying to force all in 1 ‘action block’ in the UI, switched back and forth between UI <-> YAML but couldn’t see how to get this working.
With your code, and using VSC, it was easier then in the UI and when seeing the result in the UI it’s so obvious… :blush:
I went for option 1 since I wanted to get this done without user intervention.

Like I wrote: I got stuck on thinking that if the rest of the automation would not run.
Thanks A LOT!
:+1:

I’m happy to have helped, but I wanted to clarify part of your response…

Using a Choose action does not require user input/response, it’s more like an if/then/else. It allows us to easily create automations with more complex logic.

For example if you wanted the second light to turn on at 20% when the boolean is on, but 100% when the boolean is off, you could use a Choose action.

action:
  - service: light.turn_on
    data:
      brightness_pct: 10
    target:
      entity_id: light.sr1
  - choose:
    - conditions:
        - condition: state
          entity_id: input_boolean.XXXX
          state: 'on'
      sequence:
        - service: light.turn_on
          data:
            brightness_pct: 20
          target:
            entity_id: light.sr2
    - conditions:
        - condition: state
          entity_id: input_boolean.XXXX
          state: 'off'
      sequence:
        - service: light.turn_on
          data:
            brightness_pct: 100
          target:
            entity_id: light.sr2

For your original question the two options are mostly interchangeable, but the Choose action is very useful.

1 Like

Oops, that’s what ‘choose’ stands for!

Well, thank you for adding/clarifying this information and providing that link to that documentation page: I have learned something new.
So it seems to be an (easier) alternative to templates… any big differences/(dis)advantages?

Thanks a lot!