In Home Assistant and many other home automation controllers, your home status is either Home or Away. There’s no in-between.
The Problem
So, you’ve got your home automation started, and it’s time to add some cool automations, like turning off everything when you leave, or playing some music when you get home. These can be pretty easy to do with presence detection, but sometimes you need more.
In my Home Assistant setup I’m using Bluetooth presence detection. Every ten seconds my Synology NAS running Home Assistant checks to see what Bluetooth devices it can ‘see’. If it detects my phone, then it knows I’m home.
When I get home, and it’s between a certain time, my house will start playing my Chillout playlist on Spotify through my Sonos speakers. It will only do that, if I arrive home alone, or my fiancée isn’t home. When everyone leaves, Home Assistant will shut everything down – including music, Philips Hue Lights, the TV, heating/cooling etc.
I’m in a two bedroom apartment, and the Bluetooth presence detection works across the entire apartment. There seems to be a blackspot when I’m at my desk with my phone. I’ll be working away, writing my next awesome automation or piece of code, only for Home Assistant to cut the power to my computer monitor and pause my music. A massive first world problem that needs to be solved.
I could increase the timeout which would reduce this from happening (which I’ve done) or even use multiple presence detection options like WiFi and Bluetooth. However this doesn’t solve the problem of my house doing things too quickly.
I’ll wake my phone, place it on the desk and wait for Home Assistant to detect it again. But here comes the problem. After waiting what seems like a lifetime for my phone to be detected again, everything starts as if I have been away for hours. My music is changed to my Chillout playlist again (or starts from the beginning), lights that I don’t need might come back on, I get messages that the dishwasher is done etc. At the moment, Home Assistant doesn’t know that I was home one minute ago, but maybe there’s a way we don’t need to start from scratch each time.
There’s also some times when we might be away from home. Maybe a weekend in the country, or away for work. The house should be able to tell that we’re more than just away for the day, so maybe it shouldn’t send us alerts that a battery needs to be replaced. Or, if I’m away for a long time, but my fiancée is home, maybe send those alerts to her instead.
Adding New States To Home Assistant
In my home, I want to track the following states that a person could be.
Just Arrived | when someone has just arrived home after being away |
---|---|
HOME | The classic Home state |
Just Left | When someone has just left the house |
AWAY | The classic Away or not_home state |
Extended Away | If someone is away for more than 24 hours |
I’m using the Home Assistant input select component to track the states of my dishwasher and washing machine. I can re-use the same logic for that here.
1 2 3 4 5 6 7 8 9 10 11 12 |
input_select: phil_status_dropdown: name: Phil options: - Home - Just Arrived - Just Left - Away - Extended Away initial: Home |
Making Them Look Nice
Just like I did for my washing machine and dishwasher, I’m going to use a template sensor that will read the value of the drop-down. I can then make it look like a normal device tracker by adding our photos to the sensor.
1 2 3 4 5 6 7 8 9 10 11 |
sensor: - platform: template sensors: phil_status: value_template: '{{ states.input_select.phil_status_dropdown.state }}' friendly_name: 'Phil' helen_status: value_template: '{{ states.input_select.helen_status_dropdown.state }}' friendly_name: 'Helen' |
1 2 3 4 5 6 7 |
people_status: name: People Status entities: - sensor.phil_status - sensor.helen_status |
1 2 3 4 5 6 |
sensor.phil_status: entity_picture: #URL to picture sensor.helen_status: entity_picture: #URL to picture |
Automating Transitions Between States
In Home Assistant we can see when someone is home or away only. But we now have some more complex states. Like I did for my washing machines, I’m going to use a for: condition. So as soon as someone is marked as away from their device tracker, they’ll be set to “Just Left”. If they are then in the “Just Left” status for five minutes, they’ll be marked as “Away”. The opposite is true when getting home.
However one problem I mentioned was music jumping back to my Chillout playlist. Let’s make sure that if someone is marked as “Just Left” and then their device tracker ‘sees’ them again, instead of marking them as Just Arrived, mark them as Home. This will stop any automations for when you get home for the first time firing off again.
Here’s some code which will do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
- alias: Mark Phil as just arrived trigger: - platform: state entity_id: device_tracker.phil from: 'not_home' to: 'home' action: - service: input_select.select_option data: entity_id: input_select.phil_status_dropdown option: Just Arrived - alias: Mark Phil as home trigger: - platform: state entity_id: input_select.phil_status_dropdown to: 'Just Arrived' for: minutes: 10 - platform: state entity_id: input_select.phil_status_dropdown from: 'Just Left' to: 'Just Arrived' condition: action: - service: input_select.select_option data: entity_id: input_select.phil_status_dropdown option: Home - alias: Mark Phil as just left trigger: - platform: state entity_id: device_tracker.phil from: 'home' to: 'not_home' action: - service: input_select.select_option data: entity_id: input_select.phil_status_dropdown option: Just Left - alias: Mark Phil as away trigger: - platform: state entity_id: input_select.phil_status_dropdown to: 'Just Left' for: minutes: 10 action: - service: input_select.select_option data: entity_id: input_select.phil_status_dropdown option: Away |
We also want to add the special case “Extended Away”. This is useful if someone is away for the house for more than a whole day, like a vacation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- alias: Mark Phil as extended away trigger: - platform: state entity_id: input_select.phil_status_dropdown to: 'Away' for: hours: 24 action: - service: input_select.select_option data_template: entity_id: input_select.phil_status_dropdown option: Extended Away |
The Latest In Your Inbox
Enter your email address below to receive my latest blog posts and videos about Home Automation in your Inbox
Using Templates for Multiple People
With the code blocks above, there’s five automations just to go through those states. What if you have two people in your house? That becomes ten automations you need to maintain.
Let’s use Home Assistant templates to make the automations more dynamic. We can use the trigger variable to see which device caused the state to change, and then update the drop-down for the right person. Using templates, our whole automation for today becomes the following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
- alias: Mark person as just arrived trigger: - platform: state entity_id: device_tracker.phil from: 'not_home' to: 'home' - platform: state entity_id: group.helen from: 'not_home' to: 'home' action: - service: input_select.select_option data_template: entity_id: > {% if trigger.entity_id == 'device_tracker.phil' %} input_select.phil_status_dropdown {% else %} input_select.helen_status_dropdown {% endif %} option: Just Arrived - alias: Mark person as home trigger: - platform: state entity_id: input_select.phil_status_dropdown to: 'Just Arrived' for: minutes: 10 - platform: state entity_id: input_select.helen_status_dropdown to: 'Just Arrived' for: minutes: 10 - platform: state entity_id: input_select.phil_status_dropdown from: 'Just Left' to: 'Just Arrived' - platform: state entity_id: input_select.helen_status_dropdown from: 'Just Left' to: 'Just Arrived' action: - service: input_select.select_option data_template: entity_id: > {% if trigger.entity_id == 'input_select.phil_status_dropdown' %} input_select.phil_status_dropdown {% else %} input_select.helen_status_dropdown {% endif %} option: Home - alias: Mark person as just left trigger: - platform: state entity_id: device_tracker.phil from: 'home' to: 'not_home' - platform: state entity_id: group.helen from: 'home' to: 'not_home' action: - service: input_select.select_option data_template: entity_id: > {% if trigger.entity_id == 'device_tracker.phil' %} input_select.phil_status_dropdown {% else %} input_select.helen_status_dropdown {% endif %} option: Just Left - alias: Mark person as away trigger: - platform: state entity_id: input_select.phil_status_dropdown to: 'Just Left' for: minutes: 10 - platform: state entity_id: input_select.helen_status_dropdown to: 'Just Left' for: minutes: 10 action: - service: input_select.select_option data_template: entity_id: > {% if trigger.entity_id == 'input_select.phil_status_dropdown' %} input_select.phil_status_dropdown {% else %} input_select.helen_status_dropdown {% endif %} option: Away - alias: Mark person as extended away trigger: - platform: state entity_id: input_select.phil_status_dropdown to: 'Away' for: hours: 24 - platform: state entity_id: input_select.helen_status_dropdown to: 'Away' for: hours: 24 action: - service: input_select.select_option data_template: entity_id: > {% if trigger.entity_id == 'input_select.phil_status_dropdown' %} input_select.phil_status_dropdown {% else %} input_select.helen_status_dropdown {% endif %} option: Extended Away |
Note: I’m using bluetooth and WiFi presence detection for my fiancée’s iPhone, which I have in a Home Assistant group. When her iPhone is detected on Bluetooth or WiFi the group Helen will be marked as home.
Stopping the Just Arrived State
Update January 2018: Thanks to u/barqers on Reddit for pointing this out. With the code block above, it is possible for you to be marked as Just Left -> Just Arrived -> Home in the matter of a second if your phone disconnects and reconnects quickly. This means any automations you have setup that you want triggered when you enter the Just Arrived state could be triggered when you don’t want them.
You could use a for in your automation to account for this, however this may increase the time for Home Assistant to detect you as Home, which we don’t want.
To cover this edge-case, I’ve made the following changes to my automation, using templates to first check what my previous state was. If my previous state was Just Left, then instead of marking me as Just Arrived it will instead mark me as Home, skipping the Just Arrived state altogether.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
- alias: Mark person as just arrived trigger: - platform: state entity_id: device_tracker.qopyeeku_phil from: 'not_home' to: 'home' - platform: state entity_id: group.helen from: 'not_home' to: 'home' action: - service: input_select.select_option data_template: entity_id: > {% if trigger.entity_id == 'device_tracker.qopyeeku_phil' %} input_select.phil_status_dropdown {% else %} input_select.helen_status_dropdown {% endif %} option: > {% if trigger.entity_id == 'device_tracker.qopyeeku_phil' %} {% if states.input_select.phil_status_dropdown.state == 'Just Left' %} Home {% else %} Just Arrived {% endif %} {% else %} {% if states.input_select.helen_status_dropdown.state == 'Just Left' %} Home {% else %} Just Arrived {% endif %} {% endif %} |
Some Automations We Can Now Do
Now that we’ve got our new states setup, here’s some examples of automations we can do:
- Play music when someone is marked as Just Arrived
- Send dishwasher/washing machine alerts to someone when they are marked as Just Arrived, so the notification doesn’t get sent every time their device is marked as home
- If everyone is marked as Extended Away, activate vacation mode to automatically turn off/on lights to make it look like someone is home
- Don’t send alerts/messages to anyone marked as Extended Away for day-to-day operations of the house, like the washing being done or a battery needing to be replaced.
- If you are marked as Home after being marked as Extended Away, send an email to you with the status of your smart home. What sensors need their battery replaced etc.
- When someone is marked as Just Arrived and there are people marked as Home, announce who just got home over Text-to-Speech. Great for an announcement to let kids know when a parent is home from work for the day.
Additional States
At the moment, these five states work well for me, but i may want to change them in the future. Remember, they may not be the best fit for you but you can tailor them to suit your own home.
I don’t use this feature, but Home Assistant can be used with GPS to identify ‘zones’, such as work or school. These zones are used by Home Assistant instead of “home” or “not home”.
You could also do something similar with the drop-downs. One example would be to add a work option to the drop-down. If you have Home Assistant exposed to the world, you could use Tasker on Android to change the state of the drop-down to work when you connect to your work’s WiFi. If you have something like an Eight Sleep tracker, you could also mark someone as “asleep” when they’re laying in bed.
Wrapping Up
By adding additional states for presence, your home gets more information about what is happening. This means your smart home can tell the difference between when you’ve arrived home from a vacation and when you’ve just picked up milk from the shops.
Hopefully this inspires you to think outside the binary constraints of your controller software, to help you build some really smart automations.
-
pattyland
-
Phil Hawthorne
-
-
Stephen Arbon
-
Kyle Gordon
-
Phil Hawthorne
-
teachingbirds
-
Kyle Gordon
-
Kyle Gordon
-
-
-
David Pickel
-
David Pickel
-
Phil Hawthorne
-
David Pickel
-
-
-
Jimmy Berglund
-
Phil Hawthorne
-
Jimmy Berglund
-
Jimmy Berglund
-
Phil Hawthorne
-
Jimmy Berglund
-
Phil Hawthorne
-
Jimmy Berglund
-
Phil Hawthorne
-
Jimmy Berglund
-
Phil Hawthorne
-
-
-
-
-
doune
-
Phil Hawthorne
-
Glenn Delmotte
-
Phil Hawthorne
-
Glenn Delmotte
-
-
-
-
-
Jorge Assunção
-
Phil Hawthorne
-
Jorge Assunção
-
Phil Hawthorne
-
Jorge Assunção
-
-
-
-
-
Andrey Laptev
-
Phil Hawthorne
-
Andrey Laptev
-
Phil Hawthorne
-
-
-
-
– wιlғred
-
Phil Hawthorne
-
-
Mark Rennie
-
sangve
-
Mark Reyes
-
Phil Hawthorne
-
Mark Reyes
-
Phil Hawthorne
-
Mark Reyes
-
Mark Rennie
-
Phil Hawthorne
-
-
-
-
-
Eduard Antonyan
-
Phil Hawthorne
-
Vince M
-
Jakob Tewes
-
Vince M
-
-
-
-
Vince M
-
Eduard Antonyan
-
Vince M
-
-
-
-
Phil Hawthorne
-
Phil Hawthorne
-
Vince M
-
teachingbirds
-
wobbles
-
-
-
-
Mauricio Bonani
-
Phil Hawthorne
-
Mauricio Bonani
-
Mauricio Bonani
-
Hans
-
Mauricio Bonani
-
Grega
-
Mauricio Bonani
-
Phil Hawthorne
-
Grega
-
-
-
-
-
Nicolaas Schoute
-
Fernando Tapia
-
Phil Hawthorne
-
-
Nikiya
-
Nikiya
-
Phil Hawthorne
-
Nikiya
-
bisschopsr
-
Phil Hawthorne
-
bisschopsr
-
-
-
-
-
Jakob Tewes
-
Chad Derksen
-
-
Sylvia Wei
-
Bradley
-
teachingbirds
-
-
Marvyn
-
Phil Hawthorne
-
-
Runar Asly
-
Phil Hawthorne
-
Runar Asly
-
-
-
Dave
-
Phil Hawthorne
-
Dave
-
Phil Hawthorne
-
-
-
-
Asaf Davidof
-
Phil Hawthorne
-
Asaf Davidof
-
Phil Hawthorne
-
-
-
-
Sprint
-
Phil Hawthorne
-
Sprint
-
Phil Hawthorne
-
Sprint
-
-
-
-
-
Perc3val
-
Mark Ghali
-
Trevor Watt
-
Benjamin Piraro
-
Phil Hawthorne
-
Benjamin Piraro
-
-
-
O.M. G.
-
Martijn van der Pol
-
Dick Pluim
-
Cowboysdude
-
Mudd
-
Phil Hawthorne
-
bartoszx
-
Mudd
-
bartoszx
-
Mudd
-
-
-
-
-
Cowboysdude
-
Sergey
-
Phil Hawthorne
-