Turn off light when last person lives home

Greetings!

Want to try this automation:

If door sensor turns from off to on, and
Motion sensor is on, and
there is only one person at home, then:
turn off light.

It is pretty easy, I just don’t know how to do this “and there is only one person at home, then”

Turning on when there is no motion at home seems useful one, hope this one will be as nice

What are you using for detecting people at home? Do you have some device_tracker entities? Is there one per person?

If, for example, you have one device_tracker per person, then you can use this template to get a count of how many of them are home:

{{ states.device_tracker|selectattr('state','eq','home')|list|count }}

If you’re doing something more complicated, then please describe.

Sorry, forgot to write
I am using Nmap
I have 3 known devices and Nmap is checking for their IP (they are static)

So, here is it for now

- id: '1530900095838'
  alias: Living light off
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d0001dcb0e3
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    state: 'on'
  action:
  - data:
      entity_id: switch.a12_key
    service: switch.turn_off

Should I try this?

- id: '1530900095838'
  alias: Living light off
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d0001dcb0e3
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    state: 'on'
    wait_template: {{ states.device_tracker|selectattr('state','eq','home')|list|count }}
  action:
  - data:
      entity_id: switch.a12_key
    service: switch.turn_off

UPD:
no,

Error loading /config/configuration.yaml: while parsing a flow mapping
  in "/config/automations.yaml", line 137, column 21
expected ',' or '}', but got '<scalar>'

Best practice is to put the template code in quotes. try this and see if it works?

wait_template: "{{ states.device_tracker|selectattr('state','eq','home')|list|count }}"

Wow, I’ve been having very bad luck getting at this site in the last hour or so. Here is a reply I typed up a while ago:

Close…

- id: '1530900095838'
  alias: Living light off
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d0001dcb0e3
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    state: 'on'
  - condition: template
    value_template: "{{ states.device_tracker|selectattr('state','eq','home')|list|count }}"
  action:
  - data:
      entity_id: switch.a12_key
    service: switch.turn_off

A few comments. First, for a binary_sensor state trigger, you don’t have to specify both from: and to:. Once you specify one, there’s only one possibility for the other, so the second option is redundant. For an action like you specified, you don’t really need to use data: - you can use entity_id: at the same level as service: as a shortcut. So:

- id: '1530900095838'
  alias: Living light off
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d0001dcb0e3
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    state: 'on'
  - condition: template
    value_template: "{{ states.device_tracker|selectattr('state','eq','home')|list|count }}"
  action:
  - entity_id: switch.a12_key
    service: switch.turn_off

will work, too.

2 Likes

I’m confused by your overall goal. Is it just to turn off switch.a12_key when the last person leaves?

No need to use door or motion for that if you have nmap for people. Just use a group for all device trackers (NMAP), when group goes to off, then turn off the light.

No need to count either. The group will stay ON as long as at least one person is home.

Thank you for comments, will make code a bit cleaner! And of course thanks for correct code

I would love to use nmap in automation more, but it is not working really good. When iphones are “asleep” - they are not visible for nmap, so I had to use home_interval: 15 so the light will turn off only after 15 minutes after group living home

Understood. I had issues with presence in the beginning. Never used NMAP for the reason you described. What about using the HA phone app along with NMAP?

I do like the automation you’ve done, it will probably work well, but getting a good reliable presence detection can really be very useful for future automations too.

Thank you for advice, will know in future!

Could you share a link where I can read more about ios app detection, because when I was reading this I did not find that. Should I just turn on locations for HA app?

Pretty basic. Load the app enable iOS in configuraton and you should be good to go.

There is some smarts that combine multiple trackers with the same MAC into one device in HA.

I recommend playing around and seeing what works best for you. I use ASUSWRT, the HA app, and I have an apple TV which I also use to turn on and off a input Boolean switch which I use for presence.

1 Like

Should I add smth to code? I can’t read and understand template actually and it doesn’t work now. Nothing in logs.
47

What is smith? You should not have to do anything more than add IOS to your configuration.yaml file.

Then install the app on your phone, and login into HA. The device trackers will be created automatically

I wrote it to “pnbruckner”
I have already ios app with enabled location and ios in config file, thank you

No worries, missed the reply. :slight_smile:

Now I have ios location too, but still want to try this algorithm:

If door sensor turns from off to on, and
Motion sensor is on, and
there is only one person at home, then:
turn off light.

Any ideas?