Actions with chose to select scenes to run

My current code works to change the scene based on motion and time of day based on TOD sensors I setup.

However I wanted to expand it and for each chose condition I wanted to add to checks for whether the lights were off and whether a helper I have called sleep mode is activated or not . I could not get this to work without having to create an automation for each scene I wanted.

It looked like this is something that could easily be done using a template without creating so many unneeded lines of code and automations. I added some comments to give an idea of what I would like to do as I want to refine the additional conditions over time but wanted guidance for the structure.

Thanks for your hlep


- id: '1608850590263'
  alias: TODSceneRoomActivation
  trigger:
  - platform: state
    entity_id: binary_sensor.nursery_motion
    to: 'on'
  action:
  - choose:
    - conditions:
      - condition: state
# Add condition to execute only if light is on and sleep mode is off  to all of these
        entity_id: binary_sensor.mode_manager_early_morning
        state: 'on'
      sequence:
      - service: scene.turn_on
        data:
          entity_id: scene.zoe_earlymorning
    - conditions:
#Excute only if all lights were off in the room
      - condition: state
        entity_id: binary_sensor.mode_manager_morning
        state: 'on'
      sequence:
      - service: scene.turn_on
        data:
          entity_id: scene.zoe_morning
    - conditions:
      - condition: state
        entity_id: binary_sensor.mode_manager_afternoon
        state: 'on'
      sequence:
      - service: scene.turn_on
        data:
          entity_id: scene.zoe_afternoon
    - conditions:    
# only execute if my home helper is on
      - condition: state
        entity_id: binary_sensor.mode_manager_evening
        state: 'on'
      sequence:
      - service: scene.turn_on
        data:
          entity_id: scene.zoe_evening
    

What you have is fine but if you want it as a template:

  action:
  - service: scene.turn_on
    target:
      entity_id: >
        scene.zoe_{{ 'morning' if is_state('binary_sensor.mode_manager_morning', 'on')
          else 'afternoon' if is_state('binary_sensor.mode_manager_afternoon', 'on') 
          else 'evening' if is_state('binary_sensor.mode_manager_evening', 'on')
          else 'error' }}

FWIW, the template would be simpler if instead of using three binary sensors, one for each time of day, it referenced a single sensor whose value indicated the time of day.

Thanks so much for your help . Good point with that sensor , I will combine at some point as I have other automations I would need to update.

I took your template and attempted to modify it further but did not get the intended results. Basically I want scenes to run scenes only if the lights were off . If someone is in the room with the light on I don’t want to change the current light setting. I also want to check if my input_boolean.sleepmode is off or on before running some scenes. My attempt is below.

Many thanks

  action:
  - service: scene.turn_on
    target:
      entity_id: >
        scene.zoe_{{ 'morning' if is_state('binary_sensor.mode_manager_morning', 'on') 
          else 'earlymorning' if is_state('binary_sensor.mode_manager_night', 'on') 
          else 'afternoon' if is_state('binary_sensor.mode_manager_afternoon', 'on') 
          else 'evening' if is_state('binary_sensor.mode_manager_evening', 'on') and states(input_boolean.zoesleepmode)=='off' # I made this change to test 
          else 'nightlights' if is_state('binary_sensor.mode_manager_night', 'on')
          else 'error' }}

The problem with the modification is that it doesn’t enclose the entity name in quotes.

Add quotes like this:

and states('input_boolean.zoesleepmode') == 'off'

If you wish, you can use this format:

and is_state('input_boolean.zoesleepmode', 'off')

Thanks again