Presence not working in automations

Hey all,
I am going crazy trying to work out how to make this work.

I have 2 human presence sensors (ZG-204ZM) and I am trying to do an automation to turn the lights off, but not while its still detecting presence.

I have a motion sensor which is the main one while I am trying to get these working and I have an automation to turn off the lights in the room after motion is no longer detected 5 and 20mins.

I have added a step of and not having the presence of home.

However when the automation triggers, the presence sensors return null instead of the state.
Config as below, when looking at a trace both presence sensors are showing state “on” or null and the lights never turn off. Am i configuring something wrong?

Post the automation’s YAML configuration.

Here is the yaml, thanks


alias: "Lights: Turn off Living room - No motion"
description: Turns off the living room lights when there is no motion for 15 mins
trigger:
  - type: not_occupied
    platform: device
    device_id: 55638ecdbe48a0aebbf50b7ee18bca42
    entity_id: 1c6c76dbc4b58d0b8b63a9c30cf6b87c
    domain: binary_sensor
    id: 5min
  - type: not_occupied
    platform: device
    device_id: 55638ecdbe48a0aebbf50b7ee18bca42
    entity_id: 1c6c76dbc4b58d0b8b63a9c30cf6b87c
    domain: binary_sensor
    id: 20min
  - type: not_present
    platform: device
    device_id: 11d35ee3713321c4bed28660d781a73f
    entity_id: a53a35f5301bd80f7989a1f143c15a49
    domain: binary_sensor
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: 5min
    enabled: false
condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: light.living_room
        state: "off"
      - condition: numeric_state
        entity_id: sensor.entertainment_power
        above: 45
        enabled: false
      - condition: or
        conditions:
          - condition: state
            entity_id: binary_sensor.presence_dining_presence
            state: "on"
            enabled: true
          - condition: state
            entity_id: binary_sensor.presence_living_presence
            state: "on"
        enabled: true
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - 5min
              - condition: time
                after: "00:00:00"
                before: "08:00:00"
                enabled: false
        sequence:
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.living_room
      - conditions:
          - condition: trigger
            id:
              - 20min
        sequence:
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.living_room
    default: []
mode: single

I’m no guru with all this, yet. . .

I think the “Home” presence is a needless complication at the moment, but maybe I don’t understand your motives fully.

The automation below turns on & off the lights using a Timer Helper (Timer helper created first, with the Restore option selected).

# NOTE: referencing entities (not devices/Device IDs) makes it easier to follow and maintain

alias: presence.Bedroom
description: ""
# 3 Automation triggers to turn on or off lights, start or cancel the Timer
trigger:
  - platform: state
    entity_id:
      - binary_sensor.presence2_br_occupancy
    to: "on"
    id: occupancy-detected
  - platform: state
    entity_id:
      - binary_sensor.presence2_br_occupancy
    to: "off"
    id: occupancy-cleared
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.timer_bedroom
    id: timer-finished
condition: []
action:
#  3 Options to execute depending on Trigger above that was activated
  - choose:
      - conditions:
          - condition: trigger
            id:
              - occupancy-detected
   # Occupancy ON: turn on light, cancel timer if it was running.
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              brightness_pct: 50
              transition: 5
            target:
              entity_id: light.blue
          - service: timer.cancel
            metadata: {}
            data: {}
            target:
              entity_id: timer.timer_bedroom
      - conditions:
          - condition: trigger
            id:
              - occupancy-cleared
    # Occupancy Off:  Start Timer, set duration timer runs
          - condition: state
            entity_id: light.blue
            state: "on"
        sequence:
          - service: timer.start
            target:
              entity_id:
                - timer.timer_bedroom
            data:
              duration: "00:05:00"
      - conditions:
          - condition: trigger
            id:
              - timer-finished
    # Timer runs out:  Turn off Lights
        sequence:
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.blue
mode: single

More conditions can be added to the triggered Choices once the basics are working reliably.

I hope this is a more helpful start to finding your solution than it is a glaring display of my lack of understanding. :sunny:

So the problem I am trying to solve here is, the motion sensor can detect no motion for a period of time if we are sitting still and turn off the lights why we are in the room.

I want it to make sure there is not presence in the room before turning off the lights.
Which is made up of living room and dining presence (as the lights are the same room.

I couldnt work out a way to combine the 2 sensors and just do a turn off lights when there is no presence on either of them.
I seem to be missing something in the automations to make the presence work.

And when searching for it I just get a lot of results about the home presence based on the home assistant app instead.

I get the motive better now.

Your automation would be easier to interpret if you change it. In the Trigger section you entered Devices as triggers - if you change those to Entities and repost the YAML, I’ll follow up.

Bear with me here, my Presence sensor reports Occupancy On/Off, my Motion sensors report Occupancy or Motion On/Off depending on the application. I don’t know what your presence sensor reports.

To make it easier you can create a group helper for the two presence sensors set to act as one , with a “if either has motion, occupancy, or presence” setting (as opposed to "if all detect motion, occupancy, or presence.)

But first, we have to sort out what you’re using to get the sensors Occupancy or Presence state. It shouldn’t have anything to do with “Home”, it should just confirm whether the device itself detects Presence or Occupancy. Eliminating the use of Devices in your automation (using Entities instead) will help with that. If you’re having a hard time entering the right Entity for the presence sensor in the automation, if you type “binary_sensor.p” you’ll get the list of presence sensor entities (that you named starting with P).

1 Like

To add to this, please read up here:

What we need from you, is information on these presence sensor(s). We need t know, what state to look for. Please post the information aboout the presence sensor from developer tools > state.

2 Likes

Thank you for the help!

Ill try changing those to entities and see.

I did try to create a group previously and it was not changing at all, however I just tried it again (have had a few HA updates since then) and now it seems to be working!

The presence seems to report as home or away.
image

I think part of the key was moving away from using devices like you stated.

Here is the new one I have setup with the group to test and using the group presence as the trigger to simplify it.
Ill see if that works now and turns off correctly. Set to a short timeout for testing.

alias: Turn off livingroom
description: Turns off the living room lights when there is no motion for 15 mins
trigger:
  - platform: state
    entity_id:
      - binary_sensor.presence_living
    from: null
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 5
    id: 5min
condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: light.living_room
        state: "off"
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - 5min
        sequence:
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.living_room
      - conditions:
          - condition: trigger
            id:
              - 20min
        sequence:
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.living_room
    default: []
mode: single

EDIT: So far this config seems to be working a lot better, ill play with it more! Thanks for the direction

Good deal. I started edit your YAML, but there’s too much in flux now. When you’re finished, whether successful or not, give a shout.

I’M just happy that I was on the right track, due to starting to read YAML better. It was all just a sealed black box mere months ago. :wink:

Thanks guys, this seems to have gotten me sorted out and I can build on it from here :smiley: And definitely will use entities in the future after this education :smiley:

Excellent.

Every day is a school day, right? :innocent: