Time since last zone visit


Normally I can wrap my head around these things but I am struggling with this one. I can’t seem to work it out with all the moving parts.

Using a device tracker (in my case the icloud3 and beta app) how can I get a sensor that says how many days its been since I was last in a zone. I need the sensor to persist between reboots so it doesn’t reset. For this I am attepting to use MQTT with the retain flag.

No matter what I try I cant get the device tracker to fire the MQTT reliably or in the right time format. Also all my automations end up being so long to write as I am having to repeat the code.

Any pointing in the correct direction or sample code would be really helpful.



It would help if you could share what you’ve tried so far. If anything, it’ll help us provide you with a answer that is relevant to you.
here is an example that sends an MQTT message when a device_tracker’s state changes

- alias: location_to_mqtt
      - platform: state
        entity_id: device_tracker.device_tracker_name
    service: mqtt.publish
      topic: 'location/device_tracker_name'
      payload_template: '{{ trigger.to_state.state }}'
      retain: true

You probably want to add a condition to make sure the message is only fired when the state is one of the zones:

{{ states('device_tracker.device_tracker_name') in states.zone | lower }}