Switch components off when nobody is at home, switch them on to their previous state when at least one person is at home

Hello all,

I am looking to achieve the following.

  • We have two people living in our home. When there is nobody at home (presence tracking is already in place via my router), switch off a list of entities.
  • When at least one person is back home, switch them back on to their previous state.
  • If possible this automation rule should respect the other automations in place. For example, I have entities that turn on at sunset. So if a person comes back home before sunset, it shouldn’t switch them on. This part of the automation is the one that is probably going to complicate things. If this is the case, I am happy to repeat my other time-based automations into this automation again.

I don’t know how to set up an automation that is based on zone:home occupancy.

Thank you.

1 Like

This will be very challenging. You’ll need a way to store states of items and native HA does not have this ability without jumping through hoops. Is this going to be just lights?

You can do that with automations. Store the states with something like light store. (Currently only entities of the switch, light or group domains are supported)

Yes they are just lights. Storing the states of the entities is a nice to have, but not a must. To make things simpler, what is the automation rule that triggers if nobody is at home and if at least one person is at home?

Well those are mutually exclusive. You can’t have both nobody home and 1 person home. It’s just not possible. The automations are done typically with using device trackers. Is that what you have implemented? What sensors do you have that say ‘home’ or ‘not_home’?

You could also use presence based lights, it is build in

1 Like

Oh no you misunderstood me. I know they are exclusive :slight_smile: I just don’t know what the automation trigger rules are for “when nobody is at home” AND “at least one person is at home”. Let me simplify it.

  • How can I turn off one light (call it light.living_room) when device_tracker.phone_1 AND device_tracker.phone_2 is NOT at zone.home?
  • How can I turn on one light (call it light.living_room) when device_tracker.phone_1 OR device_tracker.phone_2 IS at zone.home?

Sorry if I couldn’t make it more clear earlier @Pippyn @petro

Edit: I am assuming I will need to define the devices at some point in the automation. So I updated the above to include a device.

Edit 2: If it helps, we can add device_tracker.phone_1 and device_tracker.phone_2 into a group called group.home_devices. Just mentioning it.

Wow I didn’t know such component existed. Thank you for sharing it. I will give this one a go as well. I am not sure how to apply this component into an automation though. Can you show me an example?

That component isn’t an automation. It just works after you configure it. The example is at the bottom of that page. You just need to create a group with your lights and a group with your device trackers.

Pretty easy in something like nodered or appdaemon, in straight up HA it’s going to wake some work.

1 Like

group section

      - device_tracker.phone_1
      - device_tracker.phone_2

      - light.living_room

presence based light

  light_group: group.my_presence_lights
  device_group: group.my_device_trackers
1 Like

This is useful. Thank you.

Is there an alternative so that it is not based on sunset or sunrise? I want the light to turn on or off anytime of the day when someone is at home or not.

Gotta do the automation the ‘normal way’

- alias: turn my lights off when i leave
    - platfrom: state
      entity_id: group.my_device_trackers
      from: home
      to: not_home
   - service: homeassistant.turn_off
       entity_id: group.my_presence_lights

I suggest you brush up on automations, all of this is covered in the docs.

Awesome! So group.my_device_trackers will be not_home unless all devices in the group are in zone.home. Am I right?

Opposite. If one device is home the group will be home.

1 Like

Great! Will give this one a go! Thanks a million!

1 Like

One more question:

You are saying if at least one of the devices in group.my_device_trackers is at zone.home, then the state of the group is home. Great, but how about if I want an automation to trigger only when ALL devices in the group.my_device_trackers to be at zone.home? Is there such option in HA?

Edit: Found my answer:

Group behavior

By default when any member of a group is on then the group will also be on. Similarly with a device tracker, when any member of the group is home then the group is home. If you set the all option to true though, this behavior is inverted and all members of the group have to be on for the group to turn on as well.