History Statistics Sensor

Hello, I’m trying to configure a history sensor.

I have device tracking configured and I wan’t to obtain the time since the device is in home but I can’t imagine how to configure de start time.

I have this:

  • platform: history_stats
    name: Time in Home
    entity_id: device_tracker.google_maps_1343434343340
    state: ‘home’
    type: time
    start: ???
    end: ‘{{ now() }}’

Thanks!

First, you should follow the instructions at the top of the page to format your YAML code.

Next, if a device_tracker entity is home, you can see how long it has been home with this template:

{{ now() - states.device_tracker.google_maps_1343434343340.last_changed }}

EDIT: Just be aware that when HA restarts, last_changed will be (about) when HA restarted. I.e., this method doesn’t work across HA restarts. Otherwise, it works fine.

1 Like

Great! I will test your solution. It’s very clear.