Using Person plugin with HomeKit driven input_booleans?

I have two input_booleans which are driven by HomeKit based on user presence.

Is there a way to hook these up to the Persons plugin so that whether they are home or not is driven by these input_booleans?

Kind of. There is no template device tracker yet…but we can kind of spoof it.

device_tracker.see will create a device tracker for us if we give a dev_id that doesn’t yet exist.

So, under your person, add 2 more ‘device trackers’. These wont exist yet. Name them the same as the input_boolean (i.e. if it’s input_boolean.homekit1, call it device_tracker.homekit1).

Then set an automation to follow the input booleans.

- alias: HomeKit Device Tracker
  trigger:
    - platform: state
      entity_id: input_boolean.homekit1
    - platform: state
      entity_id: input_boolean.homekit2
  action:
    - service: device_tracker.see
      data_template:
        # Grab the object id of the trigger. 
        # i.e. if this was input_boolean.homekit1, trigger.to_state.object_id will be 'homekit1'
        dev_id: "{{ trigger.to_state.object_id }}"
        location_name: "{{ 'home' if trigger.to_state.state == 'on' else 'not_home' }}"
2 Likes

Awesome! Works really great! Only on reboot this automation gives the following error:

Logger: homeassistant.components.automation.homekit_device_tracker
Source: helpers/service.py:140 
Integration: Automation (documentation, issues) 
First occurred: 14:32:06 (2 occurrences) 
Last logged: 14:32:06

Homekit naar device tracker: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'
While executing automation automation.homekit_device_tracker
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 421, in async_render
    render_result = compiled.render(kwargs)
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 407, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'to_state'

Got an idea what’s the cause?

Weird. It’s a state trigger…it’s got to have a to_state.

Though, maybe it’s only an attribute change. Since we didn’t give a state in the trigger, an attribute will still fire an event.

Just add this condition:

condition:
  - "{{ trigger.to_state is not None }}"

Also, change “data_template” to just “data”. Both of these changes assume you’re on version 0.115 or greater.

1 Like

Thanks again! The suggested condition gives me an error.

Should it be like this?

condition: "{{ trigger.to_state is not None }}"

What version of home assistant are you using?

If you arent on 0.115 or greater, do this.

condition:
  condition: template
  value_template: "{{ trigger.to_state is not None }}"

I’m on the latest release 0.117.5

I know this pos its old but i make this work with this automation


alias: 'Homekit tracker '
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.alexis
  - platform: state
    entity_id: input_boolean.stefany
condition: []
action:
  - service: device_tracker.see
    data:
      dev_id: '{{ trigger.to_state.object_id }}'
      location_name: '{{ ''home'' if trigger.to_state.state == ''on'' else ''not_home'' }}'
mode: single

1 Like