Great stuff guys! Here’s a version I did that only shows variables if they are positive. (i.e. doesn’t state “days” until it has been up for at least 1 day.)
Guys, I’m really liking the human readable uptime strings but have you noticed that just after you program the thing it gives a value that is MASSIVE? For example:
I also determined that the huge negative number was causing a boot loop on the ESP32 so now I have used this code to reflash all my ESPHome devices so they come up with more readable and accurate values.
nice find. Will update my nodes.I have had random reboots on several devices that eventually sort themselves out. I wonder if there’s some variation as to what order things are initialized upon boot. Hopefully this stops the overflow. For what it’s worth, I’ve noticed high values on HA sensor values as well before it actually connects to HA and refreshes the value. Is there a way, maybe with a clever filter, to have an initial value (like 0) for a sensor to have before the data comes in?
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”.
This code ends up sending the device_last_restart to HA every 60 seconds, even if update_internval = 6h or a filter for delta 0.01 is added… it keeps sending the same last restart data every minute.
That should actually work. I am currently on ESPHome version 2023.3.2 and can confirm that setting a longer update interval for the template text sensor works.
The other thing to remember is (at least that’s my understanding) that even if the last restart time is sent every minute, because the value has not changed this should not be registered as an update in HA and thus not write an entry into the database.
Compare that with an actual uptime sensor that changes its value every single time, this would result in a state change and a new entry in the database.