Track sensor last update

Hi,

I have an espdevice which tracks the power consumption and everything seems to work fine, except that the battery fails unexpectedly. Since I can not add a monitor the battery I wanted to track the last update of the sensor.

Is there a way to set a timestamp of the last update es value to use in an automation ?

Thanks

Add the esphome status sensor and trigger when this binary sensor changes to off:

Thanks for the suggestion, however I am thinking of using deep sleep in the future which would mean I get an offline every time the sensor goes to sleep. Therefor I thought I could somehow access the information when the data was last updated, e.g. as
in the history.

That’s going to have the same issue. The sensor is not going to update while the ESP is asleep, it will change to unknown and stay that way.

You could do a for state trigger just longer than your max sleep time if the sleep period is timed. e.g. if you sleep for 5 minutes:

trigger:
  - platform: state
    entity_id: binary_sensor.energieverbrauch_status
    to: 'off'
    for: '0:05:10' # sleep time + time to wake and connect
1 Like

You can access that information,
states.<sensor_name>.last_updated, states.<sensor_name>.last_changed

Be aware though if you use it to create a sensor or a trigger, it wont update on its own, you might need to create an automation to trigger its updates. As a condition it works fine. You should also use defaults as using the states entries, they arent well filtered so can cause non-expected data types.

People probably wont like this solution, but I use it for exactly what you want and I dont believe there are other good options.

e.g.

      button_bedroom1_bedside_lost_contact:
        friendly_name: 'Button Bedroom1 Bedside Lost Contact'
        value_template: "{{ (states('sensor.now_timestamp_hour') | int(default=0) - as_timestamp(state_attr('sensor.button_bedroom1_bedside_battery','last_seen'),0) | int(default=0)) > 345600 }}"

Which is not going to change if the device is asleep. Also it is far easier to use for than doing math with datetime objects.