Setting a "last seen" entity for an ESPHome device

Hi have some ESPHome devices that deep sleep, and occasionally (once per ~24 hours) wake up, take measurements and go back to sleep.

The problem is that the measurements could be largely static and so it’s difficult to tell if the device really is waking up, or if the reading is stale.

Is there a way (on either HA or ESPHome) to set a entity on the last time the sensor values have been submitted?

Does {{ states.sensor.my_sensor.last_updated }} work?

(where sensor.my_sensor should equal the entity_id of your sensor)

Thanks @robertklep. I’ll experiment and report back.

Yep – that works, and has confirmed my concern. The bloody thing has been sleeping for a couple of weeks!

Thanks so much, @robertklep

I’m not entirely sure at what point last_updated gets updated (sic) though (I assume that it’s the timestamp of the last update HA received, since there’s also last_changed, but I don’t know if ESPHome will actually send an update if nothing has changed locally).

Yeah, that is my concern. I’m testing this with battery voltage, and it is completely reasonable to believe that the voltage hasn’t moved as the drain is minimal and the present voltage is right in the “flat spot” of a Lithium discharge curve.

I’m just not sure if last_updated means “different value” or just “refreshed”.

I just ran a simple test with a template sensor that always sends the same value. The value gets sent to HA:

[10:45:38][D][sensor:125]: 'Template Sensor': Sending state 1.00000  with 1 decimals of accuracy

But on the HA side, last_updated doesn’t change. So it means “different value”.

So you may have to add an additional sensor that gets updated periodically, like the uptime sensor, and use that sensor to determine if the board is still sending any updates.

I’ll give that a try. Thanks again for your help, @robertklep