Switch lights off when last person leaves

Hello,

I’ve searched the forums and am just getting confused, I’d really appreciate a clear simple answer.

All I want to do is switch off all lights when the last person leaves the house.

I used to have this set up perfectly using state change group.all_devices from home to not_home. After an update quite a while ago, it stopped working.

We have person A and person B each with a phone using the HA Companion app for presence detection.

The nearest topic I could find was https://community.home-assistant.io/t/howto-use-multiple-device-tracker-to-turn-off-item/93623

but it doesn’t seem to work (the syntax which i copy exactly from the above topic is getting mangled by the UI configurator (it keeps changing the quotations marks in the template even when use the Edit in YAML function (See below for what it changes it to). At the bottom of that topic they suggest using groups (as I used to) but I literally cannot find how to create groups anymore using the UI.

alias: Turn off the lights when I leave home
description: ‘’
trigger:

  • platform: zone
    entity_id: person.A
    zone: zone.home
    event: leave
  • platform: zone
    entity_id: person.B
    zone: zone.home
    event: leave
    condition:
  • condition: and
    conditions:
    • condition: template
      value_template: ‘{{ states(’‘person.A’’) != ‘‘home’’ }}’
    • condition: template
      value_template: ‘{{ states(’‘person.B’’) != ‘‘home’’ }}’
      action:
  • domain: light
    entity_id: light.living_room_main
    device_id: 144d336355384ce2a13e57a5c0d1fbd6
    type: turn_off
    mode: single

Any help much appreciated.

You can’t create groups from the UI, you need to add them to condiguration.yaml see here.

Create a group containing the two persons and then trigger when this group changes from home to not_home.

Please also read the forum rules here and format your code properly.

The only way I know of to create groups is through editing the YAML files. In this case, you either have to have something in the configuration.yaml like this:

group:
  family:
    name: family
    entities:
      - person.me
      - person.her

or you could put the part starting with “family:” into a separate file (eg: groups.yaml) and put

group: !include groups.yaml

into your configuration.yaml
The group will have status of “home” when anyone is home or “not_home” when everyone has left (which is what you’ll be using for your automation). I find that, depending on the gps radius of our phones, I can be a some blocks away before HA says I’m gone, but the home/not home functionality has never failed to track.

Aside: Those code blocks in this reply are set between three back quote characters “```”. (The back-quote is the one in the upper left of the keyboard between ESC and TAB). That preserves indentation and even highlights some things.

2 Likes

Thank you both very much. I have figured it out using groups, its very simple.

My main issue was that I didn’t realise there are some things you can do in the UI and some things you can’t. Turning all lights off when last person leaves seems like such an obvious thing that most people would want to automate, yet you cant do it in the UI.

Duly noted for the back quotes too, will do in future.

3 Likes