How to see if anyone is home

I’m trying to set a sensor to see if either my wife or I are at home. I want the sensor to be “true” if either one of us is home. I have had this working in the past, but I recently moved to using the person component so I could use multiple device trackers to track a single person. Previously, I was just using the Life360 tracker.

Here is my current code:

Configuration.yaml

person:
  - name: Brad
    id: Brad
    device_trackers:
      - device_tracker.brads_droid_2
      - device_tracker.brad_phillips
  - name: Katie
    id: Katie
    device_trackers:
      - device_tracker.iphone
      - device_tracker.katie_phillips

  - platform: template
    sensors:
      someone_home:
        friendly_name: "Is someone home?"
        entity_id: group.presence
        value_template: '{% if states("group.presence") != "home" %}false{% else %}true{% endif %}'

Groups.yaml

presence:
  name: Where is everyone
  entities:
    - person.brad
    - person.katie

When this worked in the past, my groups.yaml looked like this and I did not have the “person” entry in my config. Note that these device trackers are using Life360 to see where we are at.

Old Groups.yaml

presence:
  name: Where is everyone
  entities:
    - device_tracker.brad_phillips
    - device_tracker.katie_phillips

With my current config, using the person tracker, the entity “Is someone home” becomes “true” only when BOTH of us are at home. Previously, using the old config, the entity “is someone home” became “true” when either of us got home.

How can I get this to be “true” if just one of us is at home?

2 Likes

Set up a template binary sensor. This sensor will resolve to true if either of you are ‘home’. Bonus icon template included.

binary_sensor:
  - platform: template
    sensors:
      someone_home:
        friendly_name: Someone Home
        icon_template: >-
          {% if is_state('binary_sensor.someone_home','on') %} mdi:home-account
          {% else %} mdi:home-outline
          {% endif %}
        value_template: "{{ is_state('person.brad','home') or  is_state('person.katie','home') }}"
2 Likes

Thanks for the code! But there looks to be an error in the icon_template section. I’m getting this error:

Invalid config for [binary_sensor.template]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'mdi') for dictionary value @ data['sensors']['someone_home']['icon_template']. Got "{% if is_state('binary_sensor.someone_home','on') mdi:home-account {% else %} mdi:home-outline". (See ?, line ?). Please check the docs at https://home-assistant.io/components/binary_sensor.template/

Couple of missing % I think.

- platform: template
  sensors:
    someone_home:
      friendly_name: Someone Home
      icon_template: >-
        {% if is_state('binary_sensor.someone_home','on') %}
          mdi:home-account
        {% else %}
          mdi:home-outline
        {% endif %}
      value_template: "{{ is_state('person.one','home') or  is_state('person.two','home') or is_state('person.three','home') }}"
1 Like

Yeah sorry I was at work, typed it out in a hurry.
@walaj posted the fix below

Just a couple… thx for stepping in, got sidetracked at work. Gotta get better at double checking…lol.

Thanks, guys! That worked!

Maybe this is being too picky, but is there an easy way for this to use the states “yes” and “no” instead of “on” and “off”?

I’m using device_class to set the icon in my binary_sensor.yaml file which may be a simple alternative to the icon_template:

- platform: template
  sensors:
    anyone_home:
      device_class: occupancy
      friendly_name: 'Anyone Home'
      value_template: "{{ states.person | selectattr('state','eq','home') | list | count > 0 }}"

There’s also a presence device class option which should be similar icon.

I am having an issue with my value_template not updating the binary sensor so I may use your explicit is_state to see if that works better.

FYI

8 Likes

VdkaShaker just posted a comment here so this brought this thread back to my attention. You didn’t reply to myself or @walaj you just posted another reply in your own thread so neither one of us realized you had another question!

You’ve probably figure this out by now but yes and now. You can’t do it with a binary sensor which only has states of on/off but you can accoplish this with a template sensor utilizing the value_template parameter.

I was looking for something like this. Thanks!

Hey, I’m fairly new and would like to set up this binary sensor, however when I post this into my config, I get the warning that “end of the stream or a document separator is expected (46:1)”. I think it may have something to do with the indentation but as this is only second time I am working in the config file, I do not really know how to solve this.

Maybe someone can help me. The part for which I get the error is this:

sensor:
    - platform: integration
      source: sensor.battery_power_charging
      name: "Total Batterie geladen"
      method: left
    - platform: integration
      source: sensor.battery_power_discharging
      name: "Total Batterie entladen"
      method: left
    - platform: integration
      source: sensor.power_photovoltaics
      name: "Total Photovoltaik"
      method: left
      
- platform: template
  sensors:
    someone_home:
      friendly_name: Someone Home
      icon_template: >-
        {% if is_state('binary_sensor.someone_home','on') %}
          mdi:home-account
        {% else %}
          mdi:home-outline
        {% endif %}
      value_template: "{{ is_state('person.one','home') or  is_state('person.two','home') or is_state('person.three','home') }}"```

The persons obviously have not been changed yet.

Thanks very much!

It should be under the binary_sensor: section. This code below should fix it.

binary_sensor:
  - platform: template
    sensors:
      someone_home:
        friendly_name: Someone Home
        icon_template: >-
          {% if is_state('binary_sensor.someone_home','on') %}
            mdi:home-account
          {% else %}
            mdi:home-outline
          {% endif %}
        value_template: "{{ is_state('person.one','home') or  is_state('person.two','home') or is_state('person.three','home') }}"
1 Like

Yep, working. Thanks!

So if I got this right, if I now want to add another binary sensor it must be under the binary_sensor “headline”, starting again with “- platform: template”, right?

Correct. Assuming you are doing another template binary sensor. Here is an example of my Binary Sensor section in my config.yaml with 3 different binary sensors.

binary_sensor:
#See if anyone is home - https://community.home-assistant.io/t/how-to-see-if-anyone-is-home/106340/4
  - platform: template
    sensors:
      someone_home:
        friendly_name: Is Someone Home
        icon_template: >-
          {% if is_state('binary_sensor.someone_home','on') %}
            mdi:thumb-up
          {% else %}
            mdi:thumb-down
          {% endif %}
        value_template: "{{ is_state('person.brad_phillips','home') or is_state('person.katie_phillips','home') }}"
#See if any radiant floor pump is running
  - platform: template
    sensors:
      radiant_heat_running:
        friendly_name: Is Radiant Heat Running
        value_template: "{{ is_state('binary_sensor.basement_garage_zone_pump','on') or is_state('binary_sensor.basement_zone_pump','on') or is_state('binary_sensor.garage_zone_pump','on') }}"
  - platform: trend
    sensors:
      air_line_pressure_drop:
        entity_id: sensor.air_line_pressure
        sample_duration: 11
        max_samples: 3
        min_gradient: -0.5
        friendly_name: Air Line Pressure Drop
1 Like

Can’t you just check for zone.home > 0?

2 Likes

I recently switched to using proximity;
It will not only return the distance from the person closest to home, but also the direction of movement (arrived,towards,away_from, stationary, unknown,not set)

proximity:
  proximity:
    devices:
      - device_tracker.kt_ip12
      - device_tracker.ace_ip12
      - device_tracker.aaa_ip8
    tolerance: 50
    unit_of_measurement: m

Ah okay, got it.

Thanks for explanation. :slight_smile:

@aceindy that is also a nice way. I’ll put that on the notes list to have a look at.

How about using the following to avoid having to use an OR for each person state?

value_template: {{ states.person | selectattr('state', 'eq', 'home') | list | count > 0 }}

1 Like