Setting up a motion and presence sensors

Hi all,

i have a presence senor and a motion sensor setup in my living room so that when im sitting in my living room the presence sensor will keep the light on but when im walking through i can turn the light on and off as i come and go
i setup a automation but the light either goes on and straight back off, or it doesn’t turn off at all.

this is the automation i have setup

Can anyone help ?

Please follow the Community Question Guidelines by posting your automation’s YAML configuration… it makes it a lot easier for us to see any possible issues that may be hard to spot in a screenshot of the UI Automation Editor.

Based on a cursory glance of the provided screenshot, it looks like you have misunderstood how conditions work.

That’s because your automation starts with an action to turn the light on and ends with an action to turn the light off. When it doesn’t turn off at all it’s because the branched logic leads to the Stop action.

ahh sorry heres the yaml,

alias: Motion
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.ms_motion
    to:
      - "on"
  - trigger: state
    entity_id:
      - binary_sensor.presense_occupancy
    to:
      - "on"
    for:
      hours: 0
      minutes: 0
      seconds: 5
conditions:
  - condition: time
    after: "20:00:00"
    before: "01:00:00"
    weekday:
      - sun
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
actions:
  - action: light.turn_on
    metadata: {}
    target:
      entity_id: light.living_room_light
    data:
      brightness_pct: 100
  - if:
      - condition: state
        entity_id: binary_sensor.presense_occupancy
        state:
          - "on"
        for:
          hours: 0
          minutes: 0
          seconds: 10
    then:
      - stop: Light already on
    else:
      - if:
          - condition: state
            entity_id: binary_sensor.presense_occupancy
            state:
              - "off"
            for:
              hours: 0
              minutes: 0
              seconds: 0
          - condition: and
            conditions:
              - condition: state
                entity_id: binary_sensor.ms_motion
                state:
                  - "off"
        then:
          - action: light.turn_off
            metadata: {}
            target:
              entity_id: light.living_room_light
            data: {}
  - action: light.turn_off
    metadata: {}
    target:
      entity_id: light.living_room_light
    data: {}
    enabled: true
mode: single

So the first thing to understand is that Conditions are not Waits. They are tests of the current state or situation.

What that means for your automation:

  1. The first If will only ever pass when the Motion Sensor triggers the automation. The Presence sensor-based trigger fires when the sensor has been “on” stably for 5 seconds. The condition of the first If requires that it has been “on” stably for at least 10 second… 5 seconds plus a few microseconds will never be greater than or equal to 10 seconds.
  2. The second If will practically never pass. It requires both motion and presence to be “off”, but one of them had to be “on” just a few microseconds earlier in order for one of the triggers to fire.

As I posted previously, the automation starts with a “turn on” action and ends with a “turn off” action, both of which are outside any control logic other than the general Time condition. That means that most of the times this automation runs, the likely outcome is that the light turns on, then turns off a second or two later.

Generally speaking, my recommendation is to separate the “turn on” logic and the “turn off” logic. That can be done either using multiple automations or in a single automation with branching logic. If you can describe, in detail, what you want to happen we can help you get there.

i originally had them set up as 2 seprate automations.

1st for the motion

alias: Motion
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.ms_motion
    to:
      - "on"
conditions:
  - condition: time
    after: "20:00:00"
    before: "01:00:00"
    weekday:
      - sun
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
actions:
  - action: light.turn_on
    metadata: {}
    target:
      entity_id: light.living_room_light
    data:
      brightness_pct: 100
  - wait_for_trigger:
      - trigger: state
        entity_id:
          - binary_sensor.ms_motion
        to:
          - "off"
        from:
          - "on"
  - action: light.turn_off
    metadata: {}
    target:
      entity_id: light.living_room_light
    data: {}
mode: single

2nd was for the presence

alias: Presence
description: ""
triggers:
  - trigger: state
    entity_id:
      - automation.presence
    to:
      - "on"
    for:
      hours: 0
      minutes: 0
      seconds: 5
conditions:
  - condition: time
    after: "20:00:00"
    before: "23:59:59"
actions:
  - action: light.turn_on
    metadata: {}
    target:
      entity_id: light.living_room_light
    data: {}
mode: restart
max_exceeded: silent

which somewhat worked but every now and then the light would just turn off once the motion detector cleared. so i tried to combine them into 1 automation with separate if statements which didnt work.

but more or less because i dont always sit in my living room i wanted it to be
if im just passing through the living room between the certain time, then the light will come on
but
if im in the living room and remain for more than 5secondds then keep the light on until both motion and presence have cleared.

It’s usually best to avoid Waits and delays, so think about the two automations from the perspective of one to turn on the lights and one to turn them off.

alias: Motion and Presence Light On at Night
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.ms_motion
      - binary_sensor.presence_occupancy
    to: "on"
    from: "off"
conditions:
  - condition: time
    after: "20:00:00"
    before: "01:00:00"
  - condition: state
    entity_id: light.living_room_light
    state: "off"
actions:
  - action: light.turn_on
    metadata: {}
    target:
      entity_id: light.living_room_light
    data:
      brightness_pct: 100
mode: single
alias: Motion and Presence Light Off at Night
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.ms_motion
      - binary_sensor.presence_occupancy
    to: "off"
    from: "on"
    for: "00:00:05"
conditions:
  - condition: time
    after: "20:00:00"
    before: "01:00:00"
  - condition: state
    entity_id: light.living_room_light
    state: "on"
  - alias: Check that both presence and motion sensors have been off for at least 5 seconds
    condition: state
    entity_id:
      - binary_sensor.ms_motion
      - binary_sensor.presence_occupancy
    state: "off"
    for: "00:00:05"
actions:
  - action: light.turn_off
    metadata: {}
    target:
      entity_id: light.living_room_light
mode: restart

this part of the code in the 2nd snippet is giving error 500

alias: >-
  Check that both presence and motion sensors have been off for at least 5
  seconds
trigger: state
entity_id:
  - binary_sensor.ms_motion
  - binary_sensor.presence_occupancy
state: "off"
for: "00:00:05"

Sorry about that… copy/paste error. I have fixed it in the post above.