Creating a script where only one action is subject to an {%if} statement

Hi all.

I’m new to this and blown away by the knowledge of this community, and the support of the more experienced! I’m hoping you can help me set something up.

Scenario
I have a motion sensor on our landing. If it detects motion during the day I want it to turn on the lights in 3 bedrooms (easy enough). But… I want it to leave out one bedroom if our baby is in bed. I have set up a boolean that switches based on a Hue Tap in his room - if we turn on his nightlight it sets the state to On. Any other button sets it to off.

Methodology
I’ve considered the following:

  1. Write 2 scripts - one that turns on all rooms, one that omits his room. Use the boolean in an automation to determine which script to run. I think I could do this myself, but it feels clunky…
  2. Write 1 script that includes an {%if} statement to decide whether to turn on his lights or not. I’m not sure how to begin to put this in.

Here’s what I’ve got so far

bedroom_lights_daytime:
  sequence:
  - service: hue.hue_activate_scene
    data:
      group_name: child_1_room
      scene_name: Daytime
  - service: hue.hue_activate_scene
    data:
      group_name: child_2_room
      scene_name: Daytime
  - service: hue.hue_activate_scene
    data:
      group_name: child_3_room
      scene_name: Daytime
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - device_id: randomstuff
    domain: light
    entity_id: light.child_1_pendant
    type: brightness_decrease
  - device_id: randomstuff
    domain: light
    entity_id: light.child_2_pendant
    type: brightness_decrease
  - device_id: randomstuff
    domain: light
    entity_id: light.child_3_pendant
    type: brightness_decrease
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - service: light.turn_off
    target:
      area_id:
      - child_1_room
      - child_2_room
      - child_3_room
    data:
      transition: 5
  mode: restart

I guess what I’m looking to do is this:

      scene_name: Daytime
{% if is_state('input_boolean.is_child_3_in_bed', 'off') %}
  - service: hue.hue_activate_scene
    data:
      group_name: child_3_room
      scene_name: Daytime
{% endif %}
  - delay:

But I don’t know how to actually code it to make it work. Any assistance would be greatly appreciated!

One option is to substitute Choose actions for your Call Service actions related to child 3 room:

bedroom_lights_daytime:
  sequence:
  - service: hue.hue_activate_scene
    data:
      group_name: child_1_room
      scene_name: Daytime
  - service: hue.hue_activate_scene
    data:
      group_name: child_2_room
      scene_name: Daytime
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.is_child_3_in_bed
            state: 'off'
        sequence:
          - service: hue.hue_activate_scene
            data:
              group_name: child_3_room
              scene_name: Daytime
    default: []
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - device_id: randomstuff
    domain: light
    entity_id: light.child_1_pendant
    type: brightness_decrease
  - device_id: randomstuff
    domain: light
    entity_id: light.child_2_pendant
    type: brightness_decrease
   - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.is_child_3_in_bed
            state: 'off'
        sequence:
          - device_id: randomstuff
            domain: light
            entity_id: light.child_3_pendant
            type: brightness_decrease
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - service: light.turn_off
    target:
      area_id:
      - child_1_room
      - child_2_room
      - child_3_room
    data:
      transition: 5
  mode: restart

Thank you so much for the swift response!
I’ll have to test tomorrow as they’re all in bed now, but this looks promising (no red lines in VSC!)

What’s the function of the default: []?

You can set a default action to execute if none of your conditions render true. If you don’t have a default action you want to run, it’s best to declare it as null or you can end up with errors. If you use the UI script editor it will do this automatically when you don’t fill in a default action.

Managed to test using some other lights - this works perfectly, thank you!

And I didn’t even realise you could do this in the UI! :man_facepalming:

1 Like