Uptime sensor in hours or days?

Hi, I put the uptime sensor in all my ESPhome configuration, works great but has a value in seconds, which is difficult to read (for me). I rather have it in hours and/or days.

Is it possible to change it in hours/days with ESPhome code, if yes, how? Thanks

I implemented that in HA for each device (template code inspired by several different users on this forum):

sensor:
  - platform: template
    sensors:
      esp_01_uptime_readable:
        friendly_name: "ESP 01 Uptime"
        value_template: >-
          {% set uptime = states.sensor.esp_01_uptime.state | int %}
          {% set days = (uptime / 86400) | int %}
          {%- if days > 0 -%}
            {{ days }} days, {{ (uptime - (days * 86400)) | int | timestamp_custom('%H:%M:%S', false) }}
          {%- else -%}
            {{ uptime | int | timestamp_custom('%H:%M:%S', false) }}
          {%- endif -%}
2 Likes

It’s all explained here in details Sensor Component — ESPHome :wink: Ask if you need more details or if it doesn’t answer your problem !

Thanks but I wish to have it in ESPhome code, and not create new sensors in HASS

Thanks, I red it but I don’t understand how to transform from seconds to hours …?

Maybe multiply per 3600?

sensor:
  - platform: uptime
    name: "BLE Bridge Uptime"
    filters:
        - multiply: 3600

Yep you can just divide sensor value by 3600 to get it in hours, or 86 400 in days,etc…

Yes sorry divide not multiply :smile:

divide is not accepted, gives error

Trying this,

  - platform: uptime
    name: "POW Uptime"
    filters:
      - lambda: return x / 3600;
    unit_of_measurement: "h"

Yes it works!, you can have only one uptime sensopr though

1 Like

Why only one ? just gives them some different names to distinguish them no ? :wink:

I did, it didn’t work: it showed only one

To be sure to understand well the problem, you want to report multiple uptime from same physical device (which doesn’t have much sense) or from differents ESPs ? If from different ESPs they’ll show up as different variables for sure in HA so not sure what’s your problem there :confused:

@vincen @Klagio

Thanks for this! I’m much happier having my ESP do the work instead of making more sensors in HA. I also learned I can control the number of decimal places:

  - platform: uptime
    name: "Sensor Uptime"
    filters:
      - lambda: return x / 3600.0;
    unit_of_measurement: "hours"
    accuracy_decimals: 2
4 Likes

yes I love esphome because so many things can be done without touching configuration of HASS.

My next goal is to put most of the automation inside ESPhome too.

Is there a reason why you put 3600.0 and not 3600?

No, actually at first I wondered if putting 3600.0 would make it add the decimal place, but then I figured out it didn’t make any difference - I needed the accuracy_decimals statement. But I forgot to change it back to just 3600 :blush:

I’m learning new things every day! :grinning:

Me tto, these are also some nice additions

switch:
  - platform: restart
    name: "power plug restart"

binary_sensor:
  - platform: status
    name: "power plug status"

sensor:
  - platform: wifi_signal
    name: "POW WiFi Signal"
    update_interval: 60s
2 Likes

Like the OP here, I wanted the Uptime Sensor in ESPHome to do the formatting and not have to have Home Assistant do the work. So, I offer this code snippet to @Klagio (and everyone else).

esphome code:

sensor:
  - platform: uptime
    name: "Uptime"
    id: uptime_s
    update_interval: 15s

text_sensor:
  - platform: template
    name: "Uptime (formatted)"
    lambda: |-
      uint32_t dur = id(uptime_s).state;
      int dys = 0;
      int hrs = 0;
      int mnts = 0;
      if (dur > 86399) {
        dys = trunc(dur / 86400);
        dur = dur - (dys * 86400);
      }
      if (dur > 3599) {
        hrs = trunc(dur / 3600);
        dur = dur - (hrs * 3600);
      }
      if (dur > 59) {
        mnts = trunc(dur / 60);
        dur = dur - (mnts * 60);
      }
      char buffer[17];
      sprintf(buffer, "%ud %02uh %02um %02us", dys, hrs, mnts, dur);
      return {buffer};
    icon: mdi:clock-start
    update_interval: 15s

17 Likes

Any idea why I am getting below error?

src\main.cpp:562:21: error: could not convert ‘{buffer}’ from ‘’ to ‘esphome::optional’
return {buffer};
^
src\main.cpp:563:3: warning: control reaches end of non-void function [-Wreturn-type]
});
^
*** [.pioenvs\bw_shp6_powerplug_02\src\main.cpp.o] Error 1

Fantastic, works great

hi all,

i was just looking thorough to find a method to do just this and after reading the above worked out that i can do this …

Copy to clipboard

 - platform: uptime
    name: "Emergency Light uptime"
      filters:
        - lambda: return (x/60)/60;
      unit_of_measurement: "Hrs"

to get a readout in hours… eg… 1 hrs. or 1.5hrs

hope it helps

Found myself. I put everything under sensor and not text_sensor:(