Switch lights of if there is no movement OR it's bright enough

Hi there.

I’m bit strugling to make one of my automations work.
I have LED light that switches on if my Aqara motion sensor senses movement and it dark enough. This part works perfectly.

But second part is switching it off. Idea is to switch LED off if there is no motion in the room OR brighnes is above certain treshold. I’ve done it using Conditions but doesn’t seems to work.

Does anyone have any ideas whats wrong?

alias: Livingroom LED light togle on motion and presence
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.motion_sensor_2_occupancy
    to: "on"
conditions:
  - condition: numeric_state
    entity_id: sensor.motion_sensor_2_illuminance
    below: 35
actions:
  - action: light.turn_on
    metadata: {}
    data:
      kelvin: 4141
      brightness_pct: 100
    target:
      entity_id: light.h6076
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.everything_presence_lite_8fc9d4_occupancy
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 10
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.h6076
      - conditions: []
        sequence:
          - wait_for_trigger:
              - trigger: numeric_state
                entity_id:
                  - sensor.motion_sensor_2_illuminance
                above: 60
            continue_on_timeout: false
          - action: tts.edge_tts_say
            data:
              cache: true
              entity_id: media_player.tab1
              message: >-
                It seems that it's bright enough in the living room. Let me
                switch lights off for you.
          - delay:
              hours: 0
              minutes: 0
              seconds: 3
              milliseconds: 0
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.h6076
mode: single

I’d suggest to first think of it as two automations. Code it as such, you have the turn on automation, create a shut off light automation as you want it.

Then look into Trigger IDs.

You can have two different Triggers in one automation. User gives a name (trigger ID) to each trigger.

In the Actions (Then Do) section, add a Building Block CHOOSE. Each Choose option will operate like it’s own automation.

I found a walk through of the process, but it’s ad-laden. See if it helps to make sense of it:

I think Bob is giving good advice. I used to try to do things in these Uber-Automations and mostly just gave myself a headache. Once I separated things more it really helped. Sure, it’s a longer list of automations, but I think it’s easier to understand and troubleshoot. Here are my on and off automations for my dining room. On if the room becomes occupied and it’s dark enough. Off if the room becomes unoccupied or it gets bright enough you don’t need it.

One other thing. Don’t use device actions like I did. That was early me learning stuff, and now I know what a pain it is to update everything if you change out a device. Use the actual switch or light as the target. That way if you swap the device you can make sure the actual switch/light has the same name and won’t have to update the automations. I’m working through all my automations to fix that.

alias: Dining Room Lamp Off
description: ""
mode: restart
triggers:
  - entity_id:
      - binary_sensor.dining_room_occupied
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: "{{ states('input_number.dining_room_exit_delay')|int }}"
    from: "on"
    trigger: state
  - entity_id:
      - sensor.livingroom_lux
    for:
      hours: 0
      minutes: 0
      seconds: "{{ states('input_number.light_delay')|int }}"
    above: input_number.very_bright_threshold
    trigger: numeric_state
conditions: []
actions:
  - data: {}
    target:
      device_id: d97e7784c9e6301c58bca6d1d1a68c85
    action: light.turn_off

The ON one also has some logic so the dining room won’t come on in the middle of the night. The cats triggered it periodically otherwise, and the dining room light shined right into the bedroom.

alias: Dining Room Lamp On
description: ""
mode: restart
triggers:
  - entity_id:
      - binary_sensor.dining_room_occupied
    to: "on"
    from: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 0
    trigger: state
  - at: input_datetime.wake_time_early
    trigger: time
  - entity_id:
      - sensor.livingroom_lux
    below: input_number.bright_threshold
    for:
      hours: 0
      minutes: 0
      seconds: "{{ states('input_number.light_delay')|int }}"
    trigger: numeric_state
conditions:
  - condition: time
    after: input_datetime.wake_time_early
    before: input_datetime.bed_time
  - condition: state
    entity_id: binary_sensor.dining_room_occupied
    state: "on"
  - condition: numeric_state
    entity_id: sensor.livingroom_lux
    below: input_number.bright_threshold
  - condition: state
    entity_id: binary_sensor.great_room_occupied
    state: "on"
actions:
  - data: {}
    target:
      device_id: d97e7784c9e6301c58bca6d1d1a68c85
    action: light.turn_on

If you’re just starting out, I second @pkscout 's suggestion.

Once you’re a little more comfortable with things - and you want to cut back on the sheer number of automations - you can combine automations like these by assigning a ‘Trigger ID’.

Then you pick up the ‘Trigger ID’ again for choosing which action to take, incl. potential additional conditions.

This single automation turns two different air purifiers on and off depending on the air quality their sensors report, i.e. four automations in one.

alias: Levoit Purifier On/Off by Air Value
description: ""
triggers:
  - entity_id:
      - sensor.levoit_air_purifier_diro_pm2_5
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: DiRo On
    above: 30
    trigger: numeric_state
  - entity_id:
      - sensor.levoit_air_purifier_mabero_pm2_5
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: MaBeRo On
    above: 30
    trigger: numeric_state
  - entity_id:
      - sensor.levoit_air_purifier_diro_pm2_5
    for:
      hours: 0
      minutes: 5
      seconds: 0
    below: 20
    id: DiRo Off
    trigger: numeric_state
  - entity_id:
      - sensor.levoit_air_purifier_mabero_pm2_5
    for:
      hours: 0
      minutes: 5
      seconds: 0
    below: 20
    id: MaBeRo Off
    trigger: numeric_state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - DiRo On
          - condition: state
            entity_id: input_boolean.vacationmode
            state: "off"
        sequence:
          - data:
              preset_mode: auto
            target:
              entity_id: fan.levoit_air_purifier_diro
            action: fan.set_preset_mode
      - conditions:
          - condition: trigger
            id:
              - DiRo Off
        sequence:
          - target:
              entity_id: fan.levoit_air_purifier_diro
            data: {}
            action: fan.turn_off
      - conditions:
          - condition: trigger
            id:
              - MaBeRo On
          - condition: state
            entity_id: humidifier.levoit_humidifier
            state: "off"
          - condition: state
            entity_id: input_boolean.vacationmode
            state: "off"
        sequence:
          - data:
              preset_mode: auto
            target:
              entity_id: fan.levoit_air_purifier_mabero
            action: fan.set_preset_mode
      - conditions:
          - condition: trigger
            id:
              - MaBeRo Off
        sequence:
          - target:
              entity_id: fan.levoit_air_purifier_mabero
            data: {}
            action: fan.turn_off
mode: single

The previous automation was just to brag :stuck_out_tongue_winking_eye: - and because it was fresh on my mind because I was just working on it yesterday.

This one might actually be more what you’re looking for:

alias: Switch Laundy Indicator Light - On & Off
description: ""
mode: single
triggers:
  - entity_id:
      - binary_sensor.snzb_03_01_occupancy
    from: "off"
    to: "on"
    id: Switch On
    for:
      hours: 0
      minutes: 0
      seconds: 0
    trigger: state
  - entity_id:
      - binary_sensor.snzb_03_01_occupancy
    from: "on"
    to: "off"
    id: Switch Off
    for:
      hours: 0
      minutes: 0
      seconds: 5
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Switch On
        sequence:
          - data_template:
              color_name: "{{ states('sensor.laundry_indicator') }}"
            target:
              entity_id: light.laundrytimer
            action: light.turn_on
      - conditions:
          - condition: trigger
            id:
              - Switch Off
        sequence:
          - target:
              entity_id: light.laundrytimer
            data: {}
            action: light.turn_off

Thank you all for amazing advices. My automation is now sorted using triger ID. Everything works like a charm!

1 Like