Adding Presence Sensor to my light automation... HELP!

I have a basement where I want lights to turn on automatically when I enter (by opening a door → door sensor) or if movement is detected (by the use of multiple PIR movement sensors: this is because sometimes the door is kept open, so lights in this case will turn on because movement is detected). I also want the lights to turn of after 3 minutes of “no movement”

So far I have solved by using three automations that work this way:
#1: when door sensor turns to open or any movement sensor detectes movement, start a 3 minutes timer
#2: when timer goes from idle to active, turn on the lights
#3: when timer goes from active back to idle, turn the lights off

Since my movement sensors have a timeout of about 90 seconds, if a person moves after 90 seconds the sensor should be triggered again and start the timer again keeping the lights on.

It was not perfect but worked most of the times. Now I’ve bought a Sonoff Presence Sensor (I think it works using mwaves) and I would like to add this to the picture, so that lights would stay on when someone is in the basement, even if not moving.

The problem is that I have no idea how to add the presence sensor to my automations, nor how to write a completely new one that serves my purpose.

Any help very much appreciated. Thanks!!

What entities did the Sonoff Presence Sensor add when you installed it?

It sounds like you are going about this the wrong way.

I would use a single automation since you are controlling a single entity(ies), in this case your lights.

The lights coming on is either triggered by the contact sensor opening or the movement sensor changing to detected.

The lights switching off is triggered by the movement sensor changing to clear +3 minutes.

You therefore don’t need the timer.

In respect of the presence sensor, I have the Aqara FP300 and it offers PIR movement detection and mmwave presence. At the moment, I am testing it but I have the following triggers:

The first turns the lights on and the second turns them off. Adding in the contact sensor, you would end up with something like this:

The triggers have IDs and I use those to determine which action to perform in the ‘Then do’ section.

These are the entities of this device.

I see your point, but (correct me if I’m wrong) your automations won’t cover this scenario: I open the door and leave it open.

Sometimes we do it so that the cat can go sniffing around down in the basement. In this case I would like the lights to turn off after a few minutes (the presence sensor “should” not detect the cat although I still have to test the sensitivity levels to achieve this) but with your automation I don’t think they would.

Am I wrong?

EDIT, I tried your suggestion and I can’t understand what I’m doing wrong but the lights will still go off after two minutes even if the presence sensors still detect presence. Here is the YAML, if you are willing to give it a look:

alias: TEST Automazione Scale NEW
description: Automazione luce scale con sensori di presenza
triggers:
  - type: opened
    device_id: 61243c7c72dae4ff74f7f3008b330486
    entity_id: binary_sensor.porta_scale_contact
    domain: binary_sensor
    trigger: device
    id: open
  - type: opened
    device_id: fca6d02ec1541af6330ec1e62ff6b7e9
    entity_id: binary_sensor.porta_cantina_contact
    domain: binary_sensor
    trigger: device
    id: open
  - type: occupied
    device_id: 805310d8f22a10c7847a7acf03dbbeb3
    entity_id: 21a43385f7657a08cccc1b50cbdda581
    domain: binary_sensor
    trigger: device
    id: presence
  - type: occupied
    device_id: 9b7d6908e8765428fba32707e1dfd6c8
    entity_id: c3777917d4679419b55da430e675392e
    domain: binary_sensor
    trigger: device
    id: presence
  - type: occupied
    device_id: ca55781f3570885ecda2eeb19c42d97a
    entity_id: 486e1d01ebab86cf6fd8c53554b7e554
    domain: binary_sensor
    trigger: device
    id: presence
  - type: occupied
    device_id: f4430cbc2c9ef4f306564d9f1fa77d20
    entity_id: 6fb9377bdb58c3b492daad9232500420
    domain: binary_sensor
    trigger: device
    id: presence
  - type: occupied
    device_id: 6987fa74acc2749bae514550c889b9ff
    entity_id: 354f7b5d35009f6e8df83fb338fbb53d
    domain: binary_sensor
    trigger: device
    id: presence
  - type: not_occupied
    device_id: 805310d8f22a10c7847a7acf03dbbeb3
    entity_id: 21a43385f7657a08cccc1b50cbdda581
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: absence
  - type: not_occupied
    device_id: 9b7d6908e8765428fba32707e1dfd6c8
    entity_id: c3777917d4679419b55da430e675392e
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: absence
  - type: not_occupied
    device_id: ca55781f3570885ecda2eeb19c42d97a
    entity_id: 486e1d01ebab86cf6fd8c53554b7e554
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: absence
  - type: not_occupied
    device_id: f4430cbc2c9ef4f306564d9f1fa77d20
    entity_id: 6fb9377bdb58c3b492daad9232500420
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: absence
  - type: occupied
    device_id: 6987fa74acc2749bae514550c889b9ff
    entity_id: 354f7b5d35009f6e8df83fb338fbb53d
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: absence
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - open
              - presence
        sequence:
          - action: light.turn_on
            metadata: {}
            target:
              entity_id: light.luci
            data: {}
      - conditions:
          - condition: trigger
            id:
              - absence
        sequence:
          - action: light.turn_off
            metadata: {}
            target:
              entity_id: light.luci
            data: {}
mode: single

Use a timer helper. Set it for two minutes.

Use triggers only for open, motion, timer finished.

When open or motion, start timer (this will restart it if already running) and turn on light.

When timer finished, turn off the light.

Oh, I just read that is what you were doing before. Why not keep doing it but use the presence sensor?

When the timer finishes, check for presence. If presence, start the timer again. If not, shut off the lights.

ETA: You should use group helper for your sensors and only trigger on the helper. Then it would only trigger when ALL presence is off.

Door opens=Lights on.

Presence sensor stops detecting=Lights off.

I don’t think it has to be any more complicated than that. There’s no need for a timer in this situation, the only addition is you could keep 1 motion sensor to trigger the light on and position it for optimal results, then have the presence sensor keep the light on until it’s not detecting.

There are a number of Blueprints that do this and address a bunch of edge cases. I use the one from Blacky with great success. But there are others.

This is the one that I use: 💡 Sensor Light - Motion Sensor - Door Sensor - Sun Elevation - LUX Value - Scenes - Time - Light Control - Device Tracker - Night Lights

The requirement for the light to stay on for two minutes requires some type of timer. The for in the trigger will not survive a restart.

I always code to survive a restart. So, I don’t use for and instead use a timer.

With multiple PIR’s and a Presence sensor I would be tempted to ask if a simpler solution might work, specifically:

Do you even need the door sensor in the automation any more, could you just add all the sensors to a Binary Sensor group such that:

  • If any sensor is detecting movement or presence - turn on the light.
  • If all sensors are clear (none are currently detecting) turn off the light.

I have found that the presence sensors are not perfect so I use a small delay (the sensors have to be off for a while before the light is turned off) but otherwise I just do as I described above (all my sensors are Sonoff - both IR and Radar).

1 Like

Yes. It was stated they will open the door for the cat. They want the light on for a few minutes. But, the presence sensors will be set to not detect the cat.

While I read quickly, its seems to me that the sensor hasn’t sent a new response to trigger the timer reset.

Using timers is the best way to do presence, as it’s easy to add new devices. You can do it all in one automation with trigger IDs and using queueing.

To debug, look at the log and see what triggers are missing. Perhaps you are not resetting the timer correctly.

Timer is easiest because it’s the only entity to control the light. Note how complicated the “simpler” methods suggested above are.

Also, the best design is to think about presence, not light control. Create an entity called Basement Presence. Make the light respond to basement presence buy the rules you want.

I would handle this as a special case.

I used to have (now replaced) an intermittent IR sensor - occasionally it would not send an event (both detected and clear events). I created (an admitted convoluted) set of binary template sensors based on the real sensor.

I think something similar would work for the the door, i.e. your “template door” would register as open if (and only if):

  • The real door is open. (and)
  • The real door has been is open for less than 3 minutes.

You could then add the “template door” to the same binary sensor group, so the light would now be on if, either:

  • The door was just opened - in the last 3 minutes. (or)
  • Any of the sensors detect presence.

It would, because the ‘off’ trigger is based on presence only and not the door sensor being closed.

It looks like you are using devices for triggers whereas you should be using entities. Here is my YAML for my triggers:

alias: FP300_test
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.aqara_presence_sensor_fp300_motion
    to:
      - "on"
    id: "on"
  - trigger: state
    entity_id:
      - binary_sensor.aqara_presence_sensor_fp300_occupancy
    to:
      - "off"
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: "off"

So, after re-reading through everything and noting that many of us missed/ignored requirements (especially me where I skipped the initial question and answered with what was originally being used), I stick with my original expanded answer.

  1. Use group helper for your presence sensors and only trigger on the helper.
  2. It looks like you have two door sensors too. Group them as well.
  3. Triggers for your open group, presence group, and timer finished.
  4. When open or presence, start timer (will restart if already running) and turn on light.
  5. When timer ends, check for presence. If presence, start the timer. If not, turn off light.

Why your current code is not working is what looks like five presence sensors. When one goes not_occupied, it turns off the light. But, other presence sensors can still show presence. The group will handle this.

And, stop using devices! :slight_smile:

First of all, thanks to all of you for the help and time you are dedicating to my issue.

I’ve read all your suggestions and I came to the conclusion that my initial approach using a timer is probably what would work best in my use cases.

I learned that I should (and I will) stop using devices and use entities instead. I will update my automations to reflect that.

The intended behavior of my original automations was that as long as anyone moved in the basement (and triggered a movement sensor) the timer would restart. This actually doesn’t happen and I find myself very often with the lights turning off on me and then back on after a few seconds, when the movement sensors catch a new movement.

So, keeping this in mind, my problem now is how to add the presence sensors to the mix. I’ll try to explain better: I have no idea how to have the timer restart each time movement is detected and/or presence is “on”, because I don’t know how to code something like “keep lights on AS LONG AS presence is detected”.

Or maybe this is a limit that’s would be solved by using entities? It’s late now and I don’t have the time to try …

I would love to have a “simpler” automation work but my scarce abilities in using HA don’t allow me to write an automation that’s sophisticated enough to cover all possibilities (especially the one where I open the door but no actual presence will be detected by sensors because no one will actually go down to the basement).

I only read this after having written my reply above this one… Thanks for the suggestions and I will try to implement them ASAP.
Just as a clarification, I have two door sensors, four PIR movement sensors and two (momentarily) presence sensors that will probably reduce to a single one as soon as I find out which one is more reliable, as one is battery operated while the second one uses a power supply.

Exactly, and I think this is why IMHO it wouldn’t work: if I open the door the lights will turn on triggered by the door sensor, but when no one (except the cat) actually goes down to the basement, no presence will be detected ever and therefore the lights would never be turned off. Am I wrong?

I see what you are saying and you are correct.

It sounds like the external timer is what you are looking for and, with so many sensors, creating grouped sensors, as suggested by others, would be the way forward.

You can still handle this in a single automation though.

Assuming that you are going to place all:

  • Door sensors in a group: basement_door
  • Motion (and presence) sensors in a group: basement_motion

I came up with the following automation:

triggers:
  - trigger: state
    entity_id:
      - binary_sensor.basement_door
      - binary_sensor.basement_motion
    from: "off"
    to: "on"
    id: turn_on
  - trigger: state
    entity_id:
      - binary_sensor.basement_door
    from: "off"
    to: "on"
    id: door_left_open
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - trigger: state
    entity_id:
      - binary_sensor.basement_door
      - binary_sensor.basement_motion
    from: "on"
    to: "off"
    id: turn_off
variables:
  motion_state: "{{ iif( is_state('binary_sensor.basement_motion', 'on') , 'on', 'off') }}"
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - turn_on
        sequence:
          - action: light.turn_on
            target:
              entity_id: light.basement
      - conditions:
          - condition: trigger
            id:
              - door_left_open
              - turn_off
        sequence:
          - action: light.turn_{{ motion_state }}
            target:
              entity_id: light.basement

There are three triggers:

  • turn_on - You open a door or motion is detected.
  • door_left_open - One or more doors have been open for 3 minutes.
  • turn_off - All doors are closed or All motion/presence detectors clear.

The turn_on condition is simple - turn the light on.

In all other circumstances it checks the current status of the motion detectors and sets the light status accordingly.

If you open the door for the cat - since the motion detectors won’t pick it up the 3 minute timer will fire (or if you close the door) and the light will go out.

Otherwise if someone is in the basement the motion detectors will take priority.