Is there a more efficient way to have two flows morning and night?

I’m using this automation to turn on and off two lights based on whether Sleep mode is active or not. Is there a better way to do this?

And also the code confuses me

  1. After choose: we have conditions: and then condition “off” , but then why do we have conditions again? right before condition: on? Shouldn’t there be a single conditions: list
  2. why is the sequence keyword at the same indentation level as conditions? Shouldn’t it be at a lower indentation level than condition: “off”

YAML is so confusing

alias: Bedroom motion activated lights with state
description: >-
  Turn lights on when Motion detected in bedroom. 
triggers:
  - entity_id:
      - binary_sensor.ikea_of_sweden_tradfri_motion_sensor_motion
    from: "off"
    to: "on"
    trigger: state
conditions:
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.sleep
            state: "off"
        sequence:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              label_id: bedroom_light
            enabled: true
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              label_id: bedroom_night_light
          - wait_for_trigger:
              - entity_id:
                  - binary_sensor.ikea_of_sweden_tradfri_motion_sensor_motion
                from: "on"
                to: "off"
                trigger: state
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              label_id: bedroom_light
          - action: switch.turn_off
            metadata: {}
            data: {}
            target:
              label_id: bedroom_night_light
      - conditions:
          - condition: state
            entity_id: input_boolean.sleep
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              label_id: bedroom_night_light
          - wait_for_trigger:
              - entity_id:
                  - binary_sensor.ikea_of_sweden_tradfri_motion_sensor_motion
                from: "on"
                to: "off"
                trigger: state
          - action: switch.turn_off
            metadata: {}
            data: {}
            target:
              label_id: bedroom_night_light
mode: restart

Not sure I got this correct but I guess you can use variables.

alias: Bedroom motion activated lights with state
description: >-
  Turn lights on when Motion detected in bedroom. 
triggers:
  - entity_id:
      - binary_sensor.ikea_of_sweden_tradfri_motion_sensor_motion
    from: "off"
    to: "on"
    trigger: state
    variables:
      bedroom_light: "{{ 'on' if states('input_boolean.sleep') == 'off' else 'off' }}"
      wait: "{{ 5 if states('input_boolean.sleep') == 'off' else 0 }}"
conditions: []
actions:
  - action: "light.turn_{{ bedroom_light }}"
    metadata: {}
    data: {}
    target:
      label_id: bedroom_light
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      label_id: bedroom_night_light
  - wait_for_trigger:
      - entity_id:
          - binary_sensor.ikea_of_sweden_tradfri_motion_sensor_motion
        from: "on"
        to: "off"
        trigger: state
  - delay:
      hours: 0
      minutes: 0
      seconds: "{{ wait |int }}"
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      label_id: bedroom_light
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      label_id: bedroom_night_light
mode: single

I guess they could be used in the actual code also but it might looks better like this in my opinion.
The idea with the bedroom is that if asleep then it does a off command, waits then off again.
You could us a if/then only on that light… but a off command wont hurt

Yeah, that seems like a good solution. I haven’t dived into jinja templates yet, looks kinda complicated.
And I guess I’ll have to use another variable if I wanted to change the light properties as well.

I don’t see the wait variable being used anywhere, was that supposed to be referenced somewhere else?

Thanks

In the delay.

I would wait with the solution. There are others that is better than me on these things that could help out.

I tried using variables in the developer tools and if one is not set then {{ a or b }} does seem to work. If both is set then it sems to use a.
Perhaps it’s different in automations.

alias: Bedroom motion activated lights with state
description: >-
  Turn lights on when Motion detected in bedroom. 
triggers:
  - entity_id:
      - binary_sensor.ikea_of_sweden_tradfri_motion_sensor_motion
    from: "off"
    to: "on"
    trigger: state
    variables:
      bedroom_light: "{{ 'on' if states('input_boolean.sleep') == 'off' else 'off' }}"
  - entity_id:
      - binary_sensor.ikea_of_sweden_tradfri_motion_sensor_motion
    from: "on"
    to: "off"
    for: 
      seconds: 5
    trigger: state
conditions: []
actions:
  - action: "light.turn_{{ trigger.to_state.state or bedroom_light }}"
    metadata: {}
    data: {}
    target:
      label_id: bedroom_light
  - action: "switch.turn_{{ trigger.to_state.state }}"
    metadata: {}
    data: {}
    target:
      label_id: bedroom_night_light
mode: single

So now I moved the wait to the trigger using for:.
Maybe this does not work as expected. You need to test it.

I have not even looked at the script or anything. But, do want to try to explain the choose syntax that you questioned.

Choose allows multiple paths based on specific conditions. Each condition for a choose can actually be multiple conditions. Meaning for each path, you need to be able to provide a list of conditions.

Conditions, by default, are and. So, if you have a list under conditions of more than one, they all must be true for it to be true. If you need them to be or, you can make your first condition in the list an or condition with a list of the conditions you need to be ored.

That is just a brief primer (although probably confusing) on conditions. Better info at that link.

Now, back to choose …
Each path the choose can take is identified by a list of conditions. The sequence goes with each conditions statement.

actions:
  - choose:
      - conditions:                                       * Path 1
          - condition: state                              * Single condition for Path 1
            entity_id: input_boolean.sleep
            state: "off"
        sequence:                                         * Sequence for Path 1
          - action: blah
      - conditions:                                       * Path 2
          - condition: state                              * And conditions for Path 2
            entity_id: input_boolean.sleep2
            state: "off"
          - condition: state                              
            entity_id: input_boolean.sleep3
            state: "off"
        sequence:                                         * Sequence for Path 2
          - action: blah
      - conditions:                                       * Path 3
          - condition: or                                 * Or conditions for Path 3
            conditions:                                   * Another list of conditions for the Or
              - condition: state
                entity_id: input_boolean.sleep2
                state: "off"
              - condition: state                              
                entity_id: input_boolean.sleep3
                state: "off"
        sequence:                                         * Sequence for Path 3
          - action: blah

I hope this actually helps and does not make everything more confusing.

1 Like