How to convert last_restart_time to uptime?

Hello everyone,

I have an esp32 which reports last_restart_time as 2022-05-23T01:00:03+00:00.
HA automatically shows this time as “2 days ago”.

I would like to plot (e.g. in ApexCharts or any other chart card) the duration of uptime, which of course resets when the next restart occurs. Similar to “energy today” which continuously increases until midnight and then starts over.

Does anybody know how this can be plotted (or whether I need to create a sensor that converts to the wanted target format)?

Thank you all :slight_smile:
Alex

EDIT:
The ESP32 is running Tasmota.

If you add this to your esphome device config it will report uptime in seconds:

sensor:
  - platform: uptime
    name: Uptime Sensor

Hello @tom_l
thank you for the suggestion. That is actually a pretty cool feature in ESPHome. Unfortunately I am using Tasmota on my ESP32.

Ok, the tasmota StatusSTS message has an uptime value but it is not formatted well either it would be just as easy to work with the value you have. Try this:

template:
  - sensor:
      - name: "Uptime"
        unit_of_measurement: "s"
        state: "{{ now().timestamp() - states('sensor.last_restart_time)|as_timestamp }}"
1 Like

Thank you @tom_l , this works perfectly!

For all who want to use tom_l’s code, please be aware that one ' is missing.

It should be

template:
  - sensor:
      - name: "Uptime"
        unit_of_measurement: "s"
        state: "{{ now().timestamp() - states('sensor.last_restart_time')|as_timestamp }}"

(of course you need to replace the sensor with the correct name, but the ' at the end was missing :slight_smile:

1 Like

Doh.

Sorry.

Nothing to be sorry for :slight_smile:
I would not even have mentioned it but I wanted to make sure other newbies like myself would not run into any issues :slight_smile:

1 Like

@tom_l
I have two small follow-up questions because I am seeing a lot of error messages in my log when the esp32 is unavailable.

2022-05-25 21:47:00 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'as_timestamp' got invalid input 'unavailable' when rendering template '{{ now().timestamp() - states('sensor.esp32_wroom_32d_devkitc_v4_1_last_restart_time')|as_timestamp }}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2022.6
  1. Is it possible to make HA ignore this error so it does not get logged. Unlike any other integration, template does seem to care a lot if the sensor is available or not.
  2. Why will this template fail to render in 2022.6?

If a template does not have defaults and fails when restarting then the new behaviour will be to not load it. So we need to fix this like so:

template:
  - sensor:
      - name: "Uptime"
        unit_of_measurement: "s"
        state: "{{ now().timestamp - states('sensor.last_restart_time')|as_timestamp(0) }}"
        availability: "{{ states('sensor.last_restart_time')|as_timestamp(0) != 0 }}"
1 Like

Hello tom,
it seems that something is not quite right yet.
With the new code the entity remains permanently unavailable despite the source entity having a value.

Is there maybe a small code error? the states in availability is also formatted red by the forum software. Maybe that hints towards the problem?

Use the developer tools template editor. What does this give:

{{ states('sensor.last_restart_time') }}
{{ states('sensor.last_restart_time')|as_timestamp(0) }}
{{ states('sensor.last_restart_time')|as_timestamp(0) != 0 }}

Hello tom,
I found the issue. There was a conflict that cause the sensor from not working properly. So your code was great and it was my oversight in a different code. I apologize for the additional work I created :frowning:

1 Like