Device tracker from script?

I’d check out mqtt device tracker

I think you would want to create a template sensor for that.

This means I’d have to deploy a MQTT server just for this? I was hoping to avoid that

I have looked at it a little but I haven’t seen any examples that would be device trackers. Any help is appreciated, thanks!

well unfortunately, there is no ‘template device tracker’ So you have to get the information into HA using one of the various device_tracker components.

You should be able to just use the device_tracker.see service call in your script. The first time you do, it will create the entity by adding it to known_devices.yaml. From then on, it will update that device entry. At the minimum, you need the dev_id and the location_name. The dev_id is what the entity will be called in Home Assistant. So if you execute the service call with:

{"dev_id":"thisis", "location_name":"home"}

It will create an entity called device_tracker.thisis with a status of home. Future service calls with that dev_id will update the entity.

Here’s a full list of the supported parameters for device_tracker.see:

Hope this helps! This service call is not really documented in the Home Assistant documentation, but you can use it for just about any custom presence detection you want. I didn’t like the built in options and already had a customized presence setup using Tasker on my Android phone, from when I was using SmartThings. So when I switched to Home Assistant, I used this functionality to adapt my existing setup.

11 Likes

Amazing! Thank you so much

FYI for anyone else creating/managing device_trackers using only the device_tracker.see service call. There was a slight breaking change introduced to this workflow starting in 0.89. As far as I can tell, it was caused by PR #20933. The issue is that, when Home Assistant restarts, device_trackers now go stale and will be marked as away if they don’t get an update within a minute of the restart. Unless your custom device_tracker is set up to do this, this is likely not happening. It’s not a huge deal but it is a little bit of a pain when you are updating config, restart HASS, and suddenly all the lights in the house go out. :upside_down_face:

Since this is custom functionality and a relatively minor issue, I’m not anticipating a fix to the core code. However, I’ve worked around this with an automation. If you’re using YAML automations, you could use the Home Assistant trigger to see when HASS restarts and call the device_tracker.see service to update any affected trackers to whatever their state already is.

I’m using an AppDaemon app that simply has an initialize function. When HASS restarts, the app initializes and in that function it calls device_tracker.see to refresh my trackers with their existing state. The state still does persist after the restart, so if you were already home, it would still say home until it goes stale and gets marked as away. By making the service call, the tracker doesn’t go stale. Here’s my app:

import appdaemon.plugins.hass.hassapi as hass

#Calls device_tracker.see after a HASS restart. Needed after .89 updating handling of stale trackers on restart

class Restart(hass.Hass):

    def initialize(self):
        self.call_service("device_tracker/see", dev_id = "a", location_name = self.get_tracker_state("device_tracker.a"))
        self.call_service("device_tracker/see", dev_id = "b", location_name = self.get_tracker_state("device_tracker.b"))

Note that the impetus for the PR that caused this breaking change is situations where a tracker goes from home and away in between when Home Assistant stops and when it’s started again. If you’re concerned about this and want to address it in some way, you’d have to have an automation push a message to your phone somehow (for example, using Join with Tasker) and then have your phone act on that message to immediately send a service call back to the HASS API to update the tracker.

2 Likes

I don’t actually need this anymore however I wanted to thank you for responding to this thread with this information

Hi apop,

I was using your solution and it was working fine. Now with the last update 0.94.0 things change with tracking component and its not working anymore, showing API error on NodeRed. Does this mean that the service device_tracker.see was removed?
image

Did you find a solution for this?

I have not yet upgraded to 0.94. I know there were some changes made to the device_tracker component but all of the discussions I read indicated that device_tracker.see would not be removed due to custom integrations depending on it. I’ll check on this as soon as I get a chance to upgrade to 0.94, probably this weekend.

Hi Apop,

Thanks for the quick reply. I reboot the system several time and now its working again, I really dont know what happend.

1 Like

It seems like that always tends to be a good thing to try first when running into issues after an upgrade. Glad it’s working for you again!

It looks like device_tracker.see is broken in HA version 0.94
The error is: “Service not found for call_service at pos 1: Unable to find service device_tracker/see”
Downgrade to version 0.93.2 and HA is working again…

Per the post above, did you try restarting Home Assistant? That service call is not getting deprecated.

I did restart HA and also the Raspberry pi more then once, with no success…
They say there are changes in device tracking in this new release, but the notes on that are unclear.

Okay, thanks for that data point. I didn’t get a chance to upgrade to 0.94 this past weekend, and it’ll be a couple days likely before I get time, but I’ll investigate this further once I’m able to upgrade.

I was able to upgrade to 0.94.2 tonight, and device_tracker.see worked fine for me.

@mdereuver how are you currently trying to call it?

In my automation I have:

- id: '1542626376554'
  alias: Me absent
  trigger:
  - below: '10'
    entity_id: sensor.me_presence
    platform: numeric_state
  action:
  - data:
      dev_id: me
      location_name: not_home
    service: device_tracker.see
  hide_entity: true

The sensor gets its value by a script.
Works in HA version 0.93.2 and with version 0.94.2 I get a “Service not found…”

Strange. If you go to the services dev tool, is device_tracker.see still listed in the dropdown there? (If you don’t mind upgrading again to check that)

Also, are you using any other device_tracker components, other than ones directly updated via the service call?