Turn off lights when defined people are not at home

This simple automation turns off all lights when all the persons defined have left home. In order for this to work all persons must have a device associated which sends its position. Setting up presence detection - Home Assistant . Specific lights can be excluded from this automation.

Blueprint Code

Click the badge to import this Blueprint:
Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.
Or import this Blueprint by using the forum topic URL:

blueprint:
  name: Turn off lights when defined people are not at home.
  domain: automation
  description:
    This simple automation turns off all lights when all the persons defined
    have left home. In order for this to work all persons must have a device associated
    which sends its position. https://www.home-assistant.io/getting-started/presence-detection/
    . Specific lights can be excluded from this automation.
  input:
    persons:
      name: Persons
      description:
        When all of the selected Persons left the home zone the lights
        will turn off
      selector:
        entity:
          multiple: true
          filter:
            - domain:
                - person
    light_exeptions:
      name: Excluded_Lights
      description:
        The following lights will be excluded from this automation. They
        will not be turned off.
      default: []
      selector:
        entity:
          multiple: true
          filter:
            - domain:
                - light
    post_triggers:
      name: Automations
      description: The following Automations will start after the lights have turned off.
      default: []
      selector:
        entity:
          multiple: true
          filter:
            - domain:
                - automation
  source_url: https://community.home-assistant.io/t/turn-off-lights-when-defined-people-are-not-at-home/600462
variables:
  exclude: !input light_exeptions
trigger:
  - platform: state
    entity_id: !input persons
    to: not_home
action:
  - service: group.set
    data:
      name: turn_off_when_persons_not_home
      entities: !input persons
      object_id: turn_off_when_persons_not_home
  - choose:
      - conditions:
          - condition: state
            entity_id: group.turn_off_when_persons_not_home
            state: not_home
        sequence:
          - service: light.turn_off
            target:
              entity_id:
                "{{ states.light | rejectattr('entity_id', 'in', exclude)\n  |
                selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list }}"
          - service: automation.trigger
            target:
              entity_id: !input post_triggers
mode: single

1 Like

All of this:

        {% set domains = ['light'] %}
        {%- for device in states|selectattr('domain','in', domains)|rejectattr('entity_id','in',exclude)|selectattr('state','in','on') %}
          {%- if loop.first %}{%- else %},{% endif %}
          {{device.entity_id }}
          {%- if loop.last %}{% endif %}
        {%- endfor  %}

can be reduced to this:

        {{ states.light
          | rejectattr('entity_id', 'in', exclude)
          | selectattr('state', 'eq', 'on')
          | map(attribute='entity_id') | list }}

The service call can be configured like this:

  - service: light.turn_off
    target:
      entity_id: >-
        {{ states.light | rejectattr('entity_id', 'in', exclude)
          | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list }}

If your intention was to allow the user to select multiple domains (i.e. not just the light domain) then your service call’s template doesn’t implement that functionality. It defines the Jinja2 variable domains as containing a single list item which is light.

If you select anything other than all persons, this blueprint will act unpredictable. Lights will only go off when every one is away, but it will only trigger when one of the selected persons leave. Depending on who leaves first, lights may then not turn off.

If the blueprint is only supposed to work when all persons leave home, the trigger can just be that zone.home state goes to 0. Then you do not need to name the persons.

If the blueprint is also supposed to work when a smaller number of people leave home, the zone.home test needs to be replaced by something more complex. In that case, creating a group and checking if the group is home will be way easier.

Thank you for the input. I will change to the more elegant template sugested and use a persons group for the condition check :slight_smile:

I just imported this Blueprint and quickly I’m posting the following suggestion so I don’t forget.

It would be nice if the blueprint could either call another automation “away lights on” or allow to turn on specific lights with different intensity (dimmed?) just to make it look like there be someone home. It could also mean to turn on the TV and / or play some music but at first I would do the lights.

Now that I wrote the above, it’s probably possible to trigger an automation when the automation from this Blueprint ran. I’m positive the Blueprint could call another automation but I’m not clear how to define the trigger based on another automation that successfully ran. Comments are welcome, I will sure dig into this as well.

I will try to make the changes myself and repost here but I’ve never worked with Blueprint and still learning HA.

Look at Presence Simulation on the hacs store, it replays the state of your house (only for the entities you select) X number of days ago.

1 Like

Oh I had that too somewhere in my mind, thanx for the reminder. This makes a lot more sense, already looking to add this. Thank you!

1 Like

While looking at one of the video that explains how to setup the presence_simulation, when nobody is home, instead of using a trigger that is based on a list of people, the clever way is to trigger when there’s 0 people at home. As simple as this:

 trigger:
   - platform: state
     entity_id:
       - zone.home
     to: "0"
     for:
       hours: 0
       minutes: 0
       seconds: 0

I set this up just earlier, it should trigger tomorrow. If there’s an issue I’ll edit this post otherwise consider the above works. Should the Blueprint be updated or not? I’ll leave that up to the author.

That’s how I trigger mine, with a condition of a few input_booleans to stop it running when for example, I have workmen in, or I have a guest that is not part of Home Assistant.

Ok. I setup a selector in the blueprint that can now optionally trigger an automation after the lights have been turned off. You can reimport the blueprint and create a new automation to use this.

1 Like