How to trigger device_tracker state change only when state is changed?

I want to setup an automation that notifies me every time a device state is changing (owntracks mqtt)
At the moment I use a state trigger without a from and a to. But that results in a notification every time a device is reporting. Not only when the ‘from state’ differs from the ‘to state’. Anyone got an idea tot solve this?

Thanks in advance!

There is an example in the docs. I assume this is what you’re looking for:

Thank you for your reply.
No, it’s not what i’m looking for. The trigger in this example only hits when it’s going from a specific state to an other specific state.
I just want a trigger when state is changed from any state to any other state. With the condition: old state <> new state.

I’m 95% certain that the following only triggers when the state changes and not on updates:

    platform: state
    entity_id: device_tracker.adam

Pretty sure I have automatons that use the shorthand without a ‘from’ or ‘to’ and only fire on changes. I may be wrong though. Double check your owntracks log. Either way, this would ensure that it only fires on any state change:

    platform: state
    entity_id: device_tracker.adam
    platform: template
    value_template: '{{ trigger.to_state.state <> trigger.from_state.state }}'


Yes I also had that problem and had to put a condition just like that:

    - condition: template
      value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"

Never had time to investigate in details why it triggers, but it does.

Regards PH


Thanks for your suggestions. i’ll try this when i’m back home.


From the docs:

Automation Trigger - Home Assistant
Triggers when the state of tracked entities change. If only entity_id given will match all state changes, even if only state attributes change.

According to the docs you’re right.
I’m using the mytracks mqtt component. I think it triggers because mqtt receives every n minutes an update of the location of the device with small updates to lat/long. Even when the current state.state is equal to the new state.state. I’m almost sure the condition template will do the trick!

See the docs, i thinks other attributes than state.state are changed. According to the docs it will cause a state changed event.

1 Like

I want something similar - but not exactly like this.

The problem with adding a condition for old_state <> new_state is that a lot of the time, old and new states will be ‘not_home’.

I want to send location notifications every time a persons location has changed by 500m or more since the last time I sent a notification. Does anyone have any idea how I could accomplish that?

I would think that my notification script would need to be triggered from every device_tracker update, and it would need to keep track of the latitude/longitude for each device_tracker entity so it can be compared to the “current” latitude/longitude. If the distance between then 2 sets of co-ordinates is more than 500m, it should send a new notification and update its saved co-ordinates. If the distance is less than 500m, it should just ignore that state_changed event and wait for the next one.

Is there any way I can accomplish that using some sort of persistent variables - or would I need a custom sensor component to store those attributes? Or is there a better way to accomplish this?

(I don’t want to adjust the frequency with which Owntracks sends updates to home assistant - just the frequency with which I send out location notifications….)

1 Like

Just to say that I’ve followed your method to get totals of my energy consumption per day.

Few errors on my side, but got it working. So thank you from the Netherlands!

1 Like

Netherlands second!

came across this topic based on google search. Just wanted to highlight a new functionality related to this discussion is now available as of 2021.12 release: a way to trigger on any state change, regardless of the state, without triggering on attribute changes:

1 Like