Multiple devices presence detection

Allright. I hope you guys can help me out. I’m already half way there, but I can’t figure out what I’m doing wrong in my multiple devices presence detection

What I want is the following:

  • Check if I’m at home
  • Check if there are guests at home

If one of those two are home; someone’s inn. If nobody’s here, there’s nobody =))

In my cofiguration I’ve got the following booleans:

input_boolean:
  rick_thuis:
    name: Rick thuis
    initial: on
    icon: mdi:cellphone-iphone
  gast_thuis:
    name: Gasten thuis
    initial: on
    icon: mdi:cellphone-iphone
  iemand_in_huis_aanwezig:
    name: Iemand in huis aanwezig
    initial: on
    icon: mdi:cellphone-iphone

Next I’ve got those two automations:

- id: '8171367139013' # deze kijkt of Rick weg is / weg gaat
  alias: Rick niet thuis
  condition:
    condition: state
    entity_id: input_boolean.rick_thuis    
    state: 'on'
# Will only trigger if all are 'not_home'
  trigger:
      platform: template
      value_template: "{% if (is_state('device_tracker.409c2859d29b','not_home')) and (is_state('device_tracker.f018980744b4','not_home')) %}true{% endif %}"
  action:
      service: homeassistant.turn_off
      entity_id: input_boolean.rick_thuis

- id: '8247236230232' # deze kijkt of rick thuis is / komt
  alias: Rick thuis
  condition:
    condition: state
    entity_id: input_boolean.rick_thuis
    state: 'off'
# Will trigger if any of the below are 'home'
  trigger:
    - entity_id: device_tracker.409c2859d29b
      from: not_home
      platform: state
      to: home
    - entity_id: device_tracker.f018980744b4
      from: not_home
      platform: state
      to: home
  action:
      service: homeassistant.turn_on
      entity_id: input_boolean.rick_thuis

Below some of the devices from guests I know

- id: '8771613894273' # deze kijkt of gasten weg zijn / weg gaan
  alias: Gasten niet in huis
  condition:
    condition: state
    entity_id: input_boolean.gast_thuis    
    state: 'on'
# Will only trigger if all are 'not_home'
  trigger:
      platform: template
      value_template: "{% if (is_state('device_tracker.b0702d072161','not_home')) and (is_state('device_tracker.881196349ff3','not_home')) %}true{% endif %}"
  action:
      service: homeassistant.turn_off
      entity_id: input_boolean.gast_thuis

- id: '9809802320202' # deze kijkt of gasten thuis zijn / komen
  alias: Gasten in thuis
  condition:
    condition: state
    entity_id: input_boolean.gast_thuis
    state: 'off'
# Will trigger if any of the below are 'home'
  trigger:
    - entity_id: device_tracker.b0702d072161
      from: not_home
      platform: state
      to: home
    - entity_id: device_tracker.881196349ff3
      from: not_home
      platform: state
      to: home
  action:
      service: homeassistant.turn_on
      entity_id: input_boolean.gast_thuis

What I want to do is make a final check to see if one of those is inn or not:

- id: '8817155151211' # deze kijkt of Rick of gasten weg zijn / weg gaan
  alias: Overall niemand thuis
  condition:
    condition: state
    entity_id: input_boolean.iemand_in_huis_aanwezig    
    state: 'on'
# Will only trigger if all are 'not_home'
  trigger:
      platform: template
      value_template: "{% if (is_state('input_boolean.rick_thuis','off')) and (is_state('input_boolean.gast_thuis','off')) %}true{% endif %}"
  action:
      service: homeassistant.turn_off
      entity_id: input_boolean.iemand_in_huis_aanwezig


- id: '8765419182112' # deze kijkt of Rick of gasten thuis zijn / komen
  alias: Overall iemand in thuis
  condition:
    condition: state
    entity_id: input_boolean.iemand_in_huis_aanwezig
    state: 'off'
# Will trigger if any of the below are 'home'
  trigger:
    - entity_id: input_boolean.rick_thuis
      from: 'off'
      platform: state
      to: 'on'
    - entity_id: input_boolean.gast_thuis
      from: 'off'
      platform: state
      to: 'on'
  action:
      service: homeassistant.turn_on
      entity_id: input_boolean.iemand_in_huis_aanwezig

However when I switch both guests or me to on or off, the final boolean doesn’t respond… Does anyone know why?

think it would be easier to group your input_booleans and use this instead. If at least one of your input_booleans is on, the group will be on too…
Let me know if you need more details

That sounds logic… How do I do that… ? I’ve got them in groups already:

wie_is_thuis:
  name: Aan- en afwezig
  icon: mdi:information 
  control: hidden
  entities:
    - device_tracker.409c2859d29b
    - input_boolean.rick_thuis
    - input_boolean.gast_thuis
    - input_boolean.iemand_in_huis_aanwezig

group.wie_is_thuis would be the entity you can use in your condition, e.g.

  condition:
    condition: state
    entity_id: group.wie_is_thuis
    state: 'on'
2 Likes

Awesome. That did the trick =)! Thanks

1 Like

maybe a silly question but since I’ve been struggling with this (when to use input_boolean, template switch or binary_sensor template) for a while too, it might help:

why would one use input_booleans for this, if your not aiming to set it manually (or let it be set by some automation). Thats what they are for, to be set by manual or automated input…

Being home or not_home is simply the state of the device_trackers, so you might just as well use the device_trackers as triggers.

If you must, you could use binary_sensor template based on the device_trackers, which makes it easier to only trigger on on/off (phones device_trackers see state changes also on attribute changes).

Put all of these device_trackers in the group @lolouk44 suggests and your off.

But remember, the group is home if 1 of the group members is home, and not_home if all members are not_home.

That is a wonderful thing for trackers for one person (enhancing the possibility of a correct state check) but a more complex thing to think about when using groups for more than 1 person , lets say your family …

btw, another thing that might improve the system logic and ease of use is let the input_booleans be read by recorder and use restore state. Leave out the initial: on and the system always will be isync with the real state of the booleans, and more over, remember the state they were in before reboot. You’ll find having them always on inconvenient in the end.
Restore state is a great thing, especially so for automations, so you could also set that up accordingly.

Cheers,
Marius

Do you have an example of how your code(s) look like? I don’t need to use inputs, but that’s just because I saw some examples of presence detection with them…

Here is my config if that helps

sure:

  - alias: People home
    id: 'People home'
    #initial_state: 'on'
    trigger:
      platform: state
      entity_id: group.grand_family
      to: 'home'
    condition: []
    action:
      - service: script.arrive_home
      - condition: state
        entity_id: input_boolean.notify_presence
        state: 'on'
      - service: notify.notify
        data_template:
          message: '{{as_timestamp(now()) | timestamp_custom("%X") }}: People entered Home'

  - alias: People not home
    id: 'People not home'
    #initial_state: 'on'
    trigger:
      platform: state
      entity_id: group.grand_family
      to: 'not_home'
      for:
        minutes: 10
    condition: []
    #     entity_id: input_boolean.guest_mode
    #     state: 'off'
    action:
      - service: script.leave_home
      - condition: state
        entity_id: input_boolean.notify_presence
        state: 'on'
      - service: notify.notify
        data_template:
          message: >-
            {{as_timestamp(now()) | timestamp_custom("%X") }}: 
            Emtpy House, securing the premises.

(note the id. no cryptic numerals, simply copy the alias)

  - alias: Presence Tracking
    trigger:
      - platform: state
        entity_id:
#          - device_tracker.naam1
#          - device_tracker.naam2
#          - device_tracker.naam3
#          - device_tracker.naam4
          - device_tracker.iphone
          - device_tracker.telefoon
    condition:
      - condition: template
        value_template: >
          {{ trigger.to_state.state is not none and
             trigger.from_state.state is not none and
             trigger.to_state.state != trigger.from_state.state }}
      - condition: state
        entity_id: input_boolean.notify_presence
        state: 'on'
    action:
      - service: notify.m
        data_template:
          title: 'Presence Tracking:'
          message: >-
            {%- if trigger.to_state.state == "not_home" -%}
              {{ trigger.to_state.attributes.friendly_name }} left {{trigger.from_state.state}}
            {%- elif trigger.from_state.state == "not_home" -%}
              {{ trigger.to_state.attributes.friendly_name }} arrived at {{trigger.to_state.state}}
            {%- else -%}
              {{ trigger.to_state.attributes.friendly_name }} left {{trigger.from_state.state}} and arrived at {{trigger.to_state.state}}
            {%- endif -%}

binary_sensor:
  - platform: template
    sensors:
      people_home:
        friendly_name: "People home"
        value_template: >
          {{ is_state('device_tracker.naam1', 'home')
             or is_state('device_tracker.naam2', 'home')
             or is_state('device_tracker.iphone', 'home')
             or is_state('device_tracker.telefoon', 'home')
             or is_state('device_tracker.naam3', 'home')
             or is_state('device_tracker.naam4', 'home')
             or is_state('device_tracker.naam5', 'home') 
             or is_state('device_tracker.naam6', 'home') }}

another option is to use zoning, if you have a device that supports that, like the iPhone.

  - alias: Naam1 arrived Home
    #initial_state: 'on'
    id: 'Naam1 arrived Home'
    trigger:
      - platform: zone
        event: enter
        zone: zone.home
        entity_id: device_tracker.telefoon
      - platform: state
        entity_id: device_tracker.naam1
        to: 'home'
    condition:
      condition: state
      entity_id: input_boolean.notify_presence
      state: 'on'
    action:
      - service: notify.notify
        data:
          message: Naam1 arrived home
      - condition: state
        entity_id: input_boolean.notify_announcement
        state: 'on'
      - service: notify.ios_naam1
        data_template:
          title: 'Arrived Home at 
                {{as_timestamp(now()) | timestamp_custom("%X") }}'
          message: "Je bent thuis, het script werkt."
          data:
            push:
              sound: "US-EN-Alexa-Welcome-Home.wav"
2 Likes