Turn on the light only if anyone is at home

Hello, I created an automation that turns on some lights just before sunset, I also added the condition that at least one of the 4 people who live there must be at home, but the lights turn on even if there is no one at home. Can someone help me understand where I went wrong? I created the automation with the visual editor, below is the yaml, thanks.

alias: Lights at sunset if somebody is at home
description: ""
triggers:
  - trigger: sun
    event: sunset
    offset: "-00:10:00"
conditions:
  - condition: or
    conditions:
      - condition: state
        entity_id: person.john
        state: home
      - condition: state
        entity_id: person.phil
        state: home
      - condition: state
        entity_id: person.mel
        state: home
      - condition: state
        entity_id: person.jack
        state: home
actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      device_id:
        - 74b406069cfb6f37b8d48b737f4e9b8a
        - fd767feb5e23625a7dfa3b60210b5dd8
        - 7c804d32e2225e5b92041edd0e6ed480
mode: single

check to make sure all of the person entities actually update when they leave/enter the home zone. is one of them stuck at always home? Also for future, you can use a numeric state condition to see if zone.home state is greater than 0. that indicates someone is home.

All person entities all up to date. when leaving home the state return 0.

group:
  family:
    name: Family
    entities:
      - person.john
      - person.phil
      - person.mel
      - person.jack
alias: Lights at sunset if somebody is at home
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "-00:10:00"
condition:
  - condition: state
    entity_id: group.family
    state: home
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.living_room
        - light.kitchen
        - light.hallway
mode: single

When that happens, check the automation’s trace.

Troubleshooting automations

ok i created the Family group, but the group takes the value not_home only when all the members of the group are not_home. even if one of them is = home the group has the value = home so the condition i’m interested in is not satisfied. i need the lights to turn on when AT LEAST one person is at home

How are those different?

You need to take a look at why your presence detection is failing, as recommend by Taras previously. There wasn’t anything wrong with your original automation, and you have been given a number of alternate methods to achieve the goal… but none of those matter if presence detection isn’t working.

One common cause for this is being logged into a device that is left at home. If anyone in the household has devices that regularly stay home, those devices should not use the same user login as the person entity.

1 Like

In your original example, the four logically ORed State Conditions are designed to do that. At least one of the four State Conditions must be fulfilled in order for the automation to execute its action.

That’s why I suggested you check the automation’s trace to see what it reports when it’s automatically triggered at 10 minutes before sunset.


NOTE

Be advised that if you are testing the automation by using its Run Actions menu command, it skips the automation’s triggers and conditions and simply executes actions. The Run Actions command is not an effective way to test this automation.

And you are right, to know if a person is at home i use ping (ICMP) and i assigned person.xxx, which did not change its status, it remained “home” even when i disconnected the device. using device_tracker.xxx_xxx_xxx_xxx (ip) the status changes from home to not_home so now it works. thanks.

alias: Lights at sunset if somebody is at home
triggers:
  - trigger: sun
    event: sunset
    offset: "-00:10:00"
conditions:
  - condition: or
    conditions:
      - condition: state
        entity_id: device_tracker.192_168_1_103
        state: home
      - condition: state
        entity_id: device_tracker.192_168_1_55
        state: home
      - condition: state
        entity_id: device_tracker.192_168_1_14
        state: home
      - condition: state
        entity_id: device_tracker.192_168_1_91
        state: home
actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      device_id:
        - 74b406069cfb6f37b8d48b737f4e9b8a
        - fd767feb5e23625a7dfa3b60210b5dd8
        - 7c804d32e2225e5b92041edd0e6ed480
mode: single

my mistake, there’s no difference, creating a group works but it is not essential to solve the issue I had. my syntax was correct but the problem was the value of the entities.
you are all right, the noob is me :slight_smile: thanks to all.

Did you discover the person entities were stuck on home after you examined the trace?

If not via the trace, how did you discover it? Perhaps you checked a person entity’s history and saw it almost never indicated it was not_home?

i’ve discovered it going to enities and check if person.john change its state from home to not_home while disconnecting the device, it remain home.

For future reference, the automation’s trace would have also shown you that one or more of the person entities was reporting home when you thought they all should have been not_home. That’s why I recommended you check it because it’s a very effective tool for debugging an automation.

3 Likes

I’ll refer to post 2…

1 Like