Setting home/away states with an automation and set_state.py

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!

Yeah, this is not the correct approach. You shouldn’t change states in the state machine. You should be using the device tracker see action.

Ever thought of adding the device tracker domain to the template integration petro?

It’s not possible, the upstream platform would need a complete rework

1 Like

Yes, it’s not an elegant solution, but as I explained, I don’t seem to have any other good options. I wish the device tracker relied on geolocation rather than on network addresses, but I guess it is what it is.

I already stated what you should use, which supports that. Device Tracker See action.

Use petro’s suggestion.

device_tracker.see

The device_tracker.see action can be used to manually update the state of a device tracker

Thanks, Petro. I appreciate your taking the time to reply to me and for your suggestion.

I have now had a close look at Device Tracker, and have tried to get it to work.

The problem is that my router, as I explained, is a non-standard one. It is branded Vodaphone, but is , I think, manufactured by ZTE. I haven’t been able to find an integration for it, and so I can’t get the configuration.yaml code for Device Tracker to work. Is there any other integration which I should be exploring?

I am sure I am not the only Home Assistant user who is having to make do with sub-optimal internet access. If I had a proper cable connection, then I could use Duckdns or some other dynamic dns service and the problem would go away.

Thanks again.

No, you just need to stop using the python set script and switch to device tracker see action.

I’m not using fake words here and you’re glossing over them, Device tracker see action.

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:
      dev_id: iphone_1
      location_name: not_home
    action: device_tracker.see

or set the gps location

Thanks. That’s very helpful. I thought device_tracker.see depended on having Device Tracker initiated in configuration.yaml.

I appreciate your help. I can now get rid of the additional python script, which makes everything much tidier and less prone to future deprecations.