Hm, that was 3 years ago, and I have completely changed my approach. That code is probably still going to work fine - I haven’t heard about deprecating the old template format.
What I am doing nowadays is to have a time
entity on the ESP device, then have a text_sensor
that exposes that value, and then set the device class to timestamp
inside Home Assistant. The major advantage of this approach is that the entity only updates once (as opposed to every 60 seconds) and thus reduces the load on the database, and it uses the built-in way to display a human-readable uptime like “2 days ago” or “Last month”.
text_sensor:
- platform: template
name: "${display_devicename} Last Restart Time"
id: last_restart_time
icon: mdi:clock
entity_category: diagnostic
# device_class: timestamp
time:
- platform: homeassistant
id: homeassistant_time
on_time_sync:
then:
# Update last restart time, but only once.
- if:
condition:
lambda: 'return id(last_restart_time).state == "";'
then:
- text_sensor.template.publish:
id: last_restart_time
state: !lambda 'return id(homeassistant_time).utcnow().strftime("%Y-%m-%dT%H:%M:%S.000000+00:00");'