I thought I would share a solution to setting home/away states for devices on Home Asssistant. My problem is that I do not currently have a fibre or adsl broadband connection and am having to make do with a 5G router. This works well on the whole, but I cannot use Duckdns with it (for remote connection to my system I use Tailscale). However, HA always shows the family devices as being ‘home’ since I don’t have a reliable external url to use when away and the system is unable to detect when we are, in fact, away.
Of course, it is possible to set the device state to away using Developer Tools > States. However, this is laborious as a regular practice, and so I wanted to automate the process and use a button which would perform the change of state.
My solution was to set up a virtual button (I may add a physical button by the front door in due course) using Devices & services > Helpers > Create helper.
I then installed the Python module found here. Remember that it requires ‘python_script:’ (not scripts) to be enabled in you configuration yaml. The code goes into config/python_scripts.
Finally, I needed to create the automation. I find that the GUI for creating automations in HA, while powerful and impressive, is also quite confusing when adding things like calling a service, because it is easy to head down a wrong path. Therefore I used the YAML editor and after some tinkering around I managed to get the automation working.
This is the YAML code if anybody is following along this path:
alias: Home_away change
description: ""
triggers:
- trigger: state
entity_id:
- input_button.home_away_button
conditions: []
actions:
- action: notify.mobile_app_iphone_1
metadata: {}
data:
message: Changed status to 'away'
- data:
entity_id: device_tracker.iphone_1
state: away
action: python_script.set_state
The code also sends a notification to my phone confirming that the status has changed to ‘away’.
This has to be used either when I am leaving the house or set remotely using Tailscale. I have not bothered with changing the status back to ‘home’ because the system does that automatically when I am home and launch the app (note that there is danger this could happen after setting the state and before your device is out of wifi range!).
It is simple, but it took me quite a long time to get it working, and I thought that maybe someone else trying to do something similar could benefit by saving a bit on the learning curve!
