I need some advice on what I think is a more complex automation.

Newbie HA user here. I need some advice on what I think is a more complex automation.

In my kitchen I have 2 sets of lights that are controlled by motion. I have a number of different scenarios and was wondering if I could do this all in one automation or do I need to set up separate automations for each scenario.

The 2 sets of lights are:

  • Main Kitchen Lights which are on a dimmer
  • Island Light which is on an on/off switch.

I have 3 helper toggles:

  • Home Mode
  • Watching TV
  • Night Mode

Senario 1:

When motion is detected and Home Mode is on and Watching TV is off and Lux is <200 turn on Island Light and Main Lights 100%
When motion is detected and Home Mode is on and Watching TV is off and Lux is >200 and < 300 turn on Island Light (no Main Lights)
When motion is detected and Home Mode is on and Watching TV is off and Lux is >300 no lights turn on.
When no motion is detected for 10 min turn off the lights.

Senario 2:

When motion is detected and Watching TV is on turn on Main Lights 50% (no Island Light)
When no motion is detected for 5 min turn off the lights.

Senario 3:

When motion is detected and Night Mode is on turn on Main Lights 5% (no Island Light)
When no motion is detected for 5 min turn off the lights.

If this can be done with one automation would some please let me know how to do it.

Thanks.

Yes, it can be done in one automation. But, since you are new to HA, it might be best to start by doing it with separate automations first. Once you get them working the way you want, if you still want to, we can help you combine them.

1 Like

Try this:

alias: Kitchen Motion Lighting (All Modes)
mode: restart

trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_motion
    to: "on"
    id: motion_on

  - platform: state
    entity_id: binary_sensor.kitchen_motion
    to: "off"
    for:
      minutes: 5
    id: motion_off_short

  - platform: state
    entity_id: binary_sensor.kitchen_motion
    to: "off"
    for:
      minutes: 10
    id: motion_off_long

condition: []

action:
  - choose:

    
      # SCENARIO 3 – NIGHT MODE (Highest Priority)
     
      - conditions:
          - condition: trigger
            id: motion_on
          - condition: state
            entity_id: input_boolean.night_mode
            state: "on"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.main_kitchen_lights
            data:
              brightness_pct: 5

 
      # SCENARIO 2 – WATCHING TV
       
      - conditions:
          - condition: trigger
            id: motion_on
          - condition: state
            entity_id: input_boolean.watching_tv
            state: "on"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.main_kitchen_lights
            data:
              brightness_pct: 50


      # SCENARIO 1 – HOME MODE + LUX LOGIC

      - conditions:
          - condition: trigger
            id: motion_on
          - condition: state
            entity_id: input_boolean.home_mode
            state: "on"
          - condition: state
            entity_id: input_boolean.watching_tv
            state: "off"
        sequence:
          - choose:

              # Lux < 200 β†’ Main 100% + Island
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.kitchen_lux
                    below: 200
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: light.main_kitchen_lights
                    data:
                      brightness_pct: 100
                  - service: switch.turn_on
                    target:
                      entity_id: switch.island_light

              # Lux 200–300 β†’ Island only
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.kitchen_lux
                    above: 200
                    below: 300
                sequence:
                  - service: switch.turn_on
                    target:
                      entity_id: switch.island_light

              # Lux > 300 β†’ Do nothing
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.kitchen_lux
                    above: 300
                sequence: []

      
      # TURN OFF LOGIC – NIGHT + TV (5 min)
     
      - conditions:
          - condition: trigger
            id: motion_off_short
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.main_kitchen_lights
          - service: switch.turn_off
            target:
              entity_id: switch.island_light

       
      # TURN OFF LOGIC – NORMAL MODE (10 min)
       
      - conditions:
          - condition: trigger
            id: motion_off_long
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.main_kitchen_lights
          - service: switch.turn_off
            target:
              entity_id: switch.island_light

1 Like

Thanks. As I mentioned, I’m a HA newbie. Do I copy this, create a new automation, select β€œEdit in YAMl” and paste it there?

Also I guess I should change the entity IDs to match what I have.

Yes you can do just that, paste as YAML. Replace the devices with your actual ones as well.

1 Like

yes and then change your entities. you can switch back to visiual mode to that as it may be easier

Thank you. This is working just fine. (I’m going to copy and adjust it for other lighting in my home).

Question; will the lights turn off after no motion when the lights are turned on manually - not by motion? I’m coming from Apple Home and its motion activated automations will only turn off the light if motion turned it on.

How does the automation know which turn off logic to use?

With your current automation, the lights will turn off after no motion even if they were turned on manually.

1 Like

The automation has trigger ids. So when the automation runs it is asking itself:

Which trigger fired me?

Then it looks at the condition.

Looking back at it, I refined it a bit:

alias: Kitchen Motion Lighting (All Modes)
mode: restart

trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_motion
    to: "on"
    id: motion_on

  - platform: state
    entity_id: binary_sensor.kitchen_motion
    to: "off"
    for:
      minutes: 5
    id: motion_off_5

condition: []

action:
  - choose:

      # SCENARIO 3 – NIGHT MODE 
      - conditions:
          - condition: trigger
            id: motion_on
          - condition: state
            entity_id: input_boolean.night_mode
            state: "on"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.main_kitchen_lights
            data:
              brightness_pct: 5

      # SCENARIO 2 – WATCHING TV
      - conditions:
          - condition: trigger
            id: motion_on
          - condition: state
            entity_id: input_boolean.watching_tv
            state: "on"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.main_kitchen_lights
            data:
              brightness_pct: 50

      # SCENARIO 1 – HOME MODE + LUX LOGIC
      - conditions:
          - condition: trigger
            id: motion_on
          - condition: state
            entity_id: input_boolean.home_mode
            state: "on"
          - condition: state
            entity_id: input_boolean.watching_tv
            state: "off"
          - condition: state
            entity_id: input_boolean.night_mode
            state: "off"
        sequence:
          - choose:

              # Lux < 200 - Main 100% + Island
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.kitchen_lux
                    below: 200
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: light.main_kitchen_lights
                    data:
                      brightness_pct: 100
                  - service: switch.turn_on
                    target:
                      entity_id: switch.island_light

              # Lux 200 - 300 -Island only
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.kitchen_lux
                    above: 199
                    below: 301
                sequence:
                  - service: switch.turn_on
                    target:
                      entity_id: switch.island_light

              # Lux > 300 - Do nothing
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.kitchen_lux
                    above: 300
                sequence: []

      # TURN OFF LOGIC 
      - conditions:
          - condition: trigger
            id: motion_off_5
        sequence:
          # If night mode OR watching TV - off after 5 min
          - choose:
              - conditions:
                  - condition: or
                    conditions:
                      - condition: state
                        entity_id: input_boolean.night_mode
                        state: "on"
                      - condition: state
                        entity_id: input_boolean.watching_tv
                        state: "on"
                sequence:
                  - service: light.turn_off
                    target:
                      entity_id: light.main_kitchen_lights
                  - service: switch.turn_off
                    target:
                      entity_id: switch.island_light

            # Otherwise - wait extra 5 min (total 10) then off
            default:
              - delay: "00:05:00"
              # Safety: only turn off if motion is still off after the extra delay
              - condition: state
                entity_id: binary_sensor.kitchen_motion
                state: "off"
              - service: light.turn_off
                target:
                  entity_id: light.main_kitchen_lights
              - service: switch.turn_off
                target:
                  entity_id: switch.island_light
1 Like