Turn on/off lights with door open sensor AND motion sensors

I’m struggling with this one and would appreciate a pointer!

I have 4 zigbee bulbs under my porch in a group. There are 2 ikea tradfri motion sensors either end of the porch. These sensors detect motion, and stay in a “detected” state for 3 minutes after activation. he sensors are also in a group

I currently have an automation to turn on the light group when either sensor detects motion (between dusk and dawn):

alias: Turn porch lights white on motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.porch_sensors
    from: "off"
    to: "on"
condition:
  - condition: sun
    before: sunrise
    after: sunset
action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.porch_lights
mode: single

A second automation turns off the lights when the sensor group switches to off. This means that the lights stay on for 3 minutes after the last motion is detected and then switch off after 3 minutes. All good:

alias: Turn off porch lights when no motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.porch_sensors
    from: "on"
    to: "off"
condition: []
action:
  - service: light.turn_off
    data:
      transition: 2
    target:
      entity_id: light.porch_lights
mode: single

The motion sensors point outwards, so when leaving the house in the dark the lights don’t turn on until I am halfway down the path. I’d like to also activate the lights when the front door sensor changes to open (between dusk and dawn). That’s easy - but then I cannot work out the best way to automate switching them off afterwards? It is possible that someone could open the door and activate the lights, but not leave and set off the motion sensors. So the current “off” automation would not fire after 3 minutes as the sensor state wouldn’t change.

Is there any way to modify the “off” automation to incorporate the door sensor? A simple timer won’t work as I might want the lights to stay on manually sometimes…

So are you saying that the off automation might not want to fire if the outside lights are already on ?

If so you could do this in one of two ways:-

Merge the on and off automations into a single one, then capture the state of the lights using a temporary scene. When you get to the off part of the automation, just set the lights to the temporary scene again ( which could be to keep them on, or turn them off).

Or… if you don’t want to merge the on and off automations, use a toggle helper. Set it to on when the lights are turned on via the automation (not manually), and use the value of the toggle helper to determine if the off automation should turn the lights off or not.

It’s not that the automation won’t fire if the lights are on… more that the lights won’t turn off if triggered by the door sensor, as the timing component is intrinsic to the two Ikea motion sensors (they stay active for 3 minutes). It’s an annoying consequence of the Ikea sensors, but I need to find a way of adding a timing component for the door sensor without upsetting the automation for the motion sensors…

Similar problem I think then.

Merge the two automations for both on and off triggers, and use trigger id’s to work out whats been triggered.
Then use an if or a choose condition to decide on what to do. If the trigger is the door sensor, then just make it wait manually for three minutes using a simple wait. If its triggered by the motion sensors, then just do a wait on trigger for them to be ‘off’

Once either have finished ‘waiting’ turn off the lights.

Also, set the automation type to single, so that if either the door or motion sensors trigger the automation, it won’t fire again.

Thanks - that’s really helpful. I’ll post full code back here if I can get it working.

Think I have this sorted - here is what I ended up with:

alias: Porch light control
description: Activate/deactivate proch lights by motion or fornt door
trigger:
  - platform: state
    id: motion
    entity_id:
      - binary_sensor.porch_sensors
    from: "off"
    to: "on"
  - platform: state
    id: door
    entity_id:
      - binary_sensor.front_door
    from: "off"
    to: "on"
condition:
  - condition: sun
    after: sunset
    before: sunrise
action:
  - choose:
      - conditions:
          - "{{ trigger.id == 'motion' }}"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.porch_lights
          - wait_for_trigger:
              - platform: state
                entity_id: binary_sensor.porch_sensors
                to: "off"
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.porch_lights
      - conditions:
          - "{{ trigger.id == 'door' }}"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.porch_lights
          - delay:
              minutes: 3
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.porch_lights
mode: single

1 Like

Looks good to me. You can save a tiny bit of code by putting the light turn off after and outside the choose, so you only have to do it once. Regardless of which choose is triggered, you’re only going to get to the off state after either has met its ‘wait’ condition.