Post

Making Home Assistant's Presence Detection not so Binary (Node-RED version)

My special thanks to Phil Hawthorne for letting me use the title of his post here.

In early 2018 I found Phil’s excellent blog post “Making Home Assistant’s Presence Detection not so Binary” and began using the setup suggested by him almost immediately.

A few months later I started migrating my automations to Node-RED and posted the initial results in the comments section of Phil’s post.

This works great, but the caveat is that you have to have one sequence for each device tracked.

I started looking for a way to have only one sequence for all the tracked devices, after some time (with some hits and many, many misses) this is what I came out with.

Template Sequence

The JSON code of this sequence is available here.


Now all I needed was to set up an input_select with the same name of a device_tracker in Home Assistant.

For reference, in known_devices.yaml.

1
2
3
4
5
6
7
homer:
  hide_if_away: false
  icon:
  mac: 00:00:00:00:00:00
  name: Homer
  picture: /local/homer.png
  track: true

The known_devices.yaml file was removed from Home Assistant in version 0.94. It’s suggested to use the person integration instead.


Then I added this to configuration.yaml.

1
2
3
4
5
6
7
8
input_select:
  homer:
    options:
      - Home
      - Just Arrived
      - Just Left
      - Away
      - Extended Away


And in Node-RED an events: state node that should be connected to the change node in the beginning of the sequence.


Homer


This must be repeated for each person being tracked, just replacing the names where needed.

For example:

known_devices.yaml

1
2
3
4
5
6
7
marge:
  hide_if_away: false
  icon:
  mac: 11:11:11:11:11:11
  name: Marge
  picture: /local/marge.png
  track: true


configuration.yaml

1
2
3
4
5
6
7
8
input_select:
  marge:
    options:
      - Home
      - Just Arrived
      - Just Left
      - Away
      - Extended Away


Marge


The final result should be something like this:


Node-RED


Home Assistant


Update - Dec 25, 2018

If you use node-red-contrib-home-assistant-websocket (default in Node-RED Community Hass.io Add-on), the sequence can be even smaller.

Since its version 0.3.0:

Call Service and Fire Event nodes are now able to render mustache templates in the data property.

This means that we can remove almost all of the template nodes used in the original sequence and move their content to the call service nodes right after them.

Template Sequence

The JSON code of this sequence is available here.


Update - Nov 04, 2019

Here’s the most recent version of the sequence. Now I’m using the person integration to track people. If you’re using device_tracker, remember to change the first and third nodes according to your needs.

Template Sequence

The JSON code of this sequence is available here.


Update - May 01, 2020

A reader made me aware of an issue in the sequence when using the zone integration in Home Assistant. The tracked person’s state would change to “Just Left” again when entering or exiting a zone.

Here’s the sequence with a fix for this issue.

Template Sequence

The JSON code of this sequence is available here.

Thank you, CamelY0, for finding, reporting, and helping to fix the issue.


Update - Jun 17, 2020

This sequence went through a diet. Now we no longer need the “rbe” and “template” nodes in it, nor do we need to use the “change” nodes to reset the timers.

Template Sequence

The JSON code of this sequence is available here.


Update - Jun 17, 2020 (2)

Always the zones! 🤦🏼‍♂️

The issue mentioned on May 01, 2020 (see above) found its way in again. Here’s the sequence with the fix.

Template Sequence

The JSON code of this sequence is available here.

My thanks to Kermit Jason Zachow for the JSONata tips.


Update - Apr 05, 2022

I was thinking the other day, “What if I go on vacation and set my status to Extended Away right after I leave home? This way I would force the house into Vacation Mode”.

When I tried doing this, I soon noticed a problem. If I change my status to Extended Away less than 10 minutes after leaving home, my status will change to Away because the current flow does not account for manual status changes.

To fix that, I created this new flow (or set of flows).

Template Sequence

The JSON code of this sequence is available here.

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.