Uptime sensor not showing minutes anymore

Hi,

I had an uptime sensor counting minutes since last restart of HA, but it was broken in an update some time ago. Cant remember which one.

I have the following code:

  - platform: uptime
    name: "HA runtime in minutes"
    unit_of_measurement: minutes

#time_date_sensor
  - platform: time_date
    display_options:
      - 'time'

And it shows like this:

and this as an entity card:

image

I have tried searching the forum, but couldnt comprehend the posts I found.

Any ideas to how I can get this back working?

It changed in the most recent version, 2020.12.0, and is described in the Breaking Changes section of its release notes.

Hereā€™s an excerpt:

to adhere to Home Assistant architectural design rules, the sensor is now changed to a timestamp.

As a side effect of this change, the unit_of_measurement option of this integration is now deprecated and can be removed from your configuration if you used that.

It means the sensor no longer reports the elapsed time since startup. It reports the time of startup.

If you want to know the elapsed time, you will need to calculate it (subtract the Uptime Sensorā€™s value from the current time). Let me know if you need help creating the template.

Thank you.
I think I for now will see if I can manage without it.

There are multiple solutions posted
What bit are you confused by ?

sensor:
  - platform: template
    sensors:
      s_ha_uptime:
        friendly_name: HA Current Uptime
        value_template: >
          {% set secs = as_timestamp(states.sensor.time.last_updated) - as_timestamp(states('sensor.uptime')) %}
          {% set days = (secs / (24 * 60 * 60)) | int %}
          {% set plural = ' days ' if days > 1 else ' day ' %}
          {% set hrmn = secs | timestamp_custom('%H:%M', false) %}
          {{ days ~ plural ~ hrmn if days > 0 else hrmn }}
1 Like

I have this in sensors.yaml but the card on the UI shows ā€œunavailableā€

- platform: template
  sensors:
    uptime_parsed:
      friendly_name: Uptime
      value_template: >
        {% set secs = as_timestamp(states.sensor.time.last_updated) - as_timestamp(states('sensor.ha_uptime')) %}
        {% set days = (secs / (24 * 60 * 60)) | int %}
        {% set plural = ' days ' if days > 1 else ' day ' %}
        {% set hrmn = secs | timestamp_custom('%H:%M', false) %}
        {{ days ~ plural ~ hrmn if days > 0 else hrmn }}

Just a wild stab in the dark here but Iā€™m betting you donā€™t have the standard time / date sensors set up on your system.

Edit: What does : -

{{ states('sensor.time'} }}
{{ states('sensor.ha_uptime') }} 

Produce in your template editor ?

{{ states('sensor.time') }}: 17:45
{{ states('sensor.uptime') }}: 2020-12-28T17:02:12.439038-08:00

Where did the colons come from (after the curley brackets) ? Or did you just add those to make it pretty ?

So what does the whole template produce in the editor ?

I assume you donā€™t have packages as you are using sensors.yaml

I assume you didnā€™t write sensors: into your sensors.yaml ?

Make sure you only have the one - platform: template ?

Edit : Just Re read yours you have the first two lines swapped and You shouldnā€™t have sensors: in you sensors.yaml file Total crap, ignore this

Yes, I added the colons. What do you mean by the whole template? What should I try in the template section of dev tools exactly?
I have all my sensors there. Also, I have ā€˜sensorsā€™ under ā€˜platform: templateā€™ inside sensors.yaml and they are working just fine. I think you have to have it because it complains w/o it.
Again, to reiterate, this is the (only) template section of my sensors.yaml

- platform: template
  sensors:
    master_bedroom_temp:
      friendly_name: "Temperature"
      unit_of_measurement: 'Ā°F'
      value_template: "{{ states('sensor.master_bedroom_temperature') | round(1) }}"
      device_class: "temperature"
    uptime_parsed:
      friendly_name: Uptime
      value_template: >
        {% set secs = as_timestamp(states.sensor.time.last_updated) - as_timestamp(states('sensor.ha_uptime')) %}
        {% set days = (secs / (24 * 60 * 60)) | int %}
        {% set plural = ' days ' if days > 1 else ' day ' %}
        {% set hrmn = secs | timestamp_custom('%H:%M', false) %}
        {{ days ~ plural ~ hrmn if days > 0 else hrmn }}

I forgot to replace the sensor name in your code with mine: sensor.ha_uptime to sensor.uptime. All is well now. Thank you.

Thank you all. At first I just gave up, but since you kept the discussion going I tried as well, and it works for me as well.

The solution based of @Mutt and @backslashv posts. Creds to @123 as well.

  - platform: uptime
    name: "ha_uptime"
#    unit_of_measurement: minutes

  - platform: template
    sensors:
      uptime_parsed:
        friendly_name: Uptime
        value_template: >
          {% set secs = as_timestamp(states.sensor.time.last_updated) - as_timestamp(states('sensor.ha_uptime')) %}
          {% set days = (secs / (24 * 60 * 60)) | int %}
          {% set plural = ' days ' if days > 1 else ' day ' %}
          {% set hrmn = secs | timestamp_custom('%H:%M', false) %}
          {{ days ~ plural ~ hrmn if days > 0 else hrmn }}

#time_date_sensor
  - platform: time_date
    display_options:
      - 'time'

image

This is ā€œthe whole templateā€
Your version : -

Should NOT have the middle ā€œsensors:ā€ line (Iā€™m amazed it didnā€™t complain loudly at that).
By definition ā€œsensors.yamlā€ only contains sensors so this would be a repeat and is under - platform: template, which is a type of sensor. Nope thatā€™s fine

Re: changes to offered code: When cutting and pasting stuff, get it working first, then customise it to your preferences then, so you know ā€˜whatever you last didā€™ was what broke it. Iā€™ve learned most of my templating from backing out of mistakes

Itā€™s considered very bad form to mark your own post as the solution
Every post on the forum would be solved by the original poster
Read the forum sticky (by Tinkerer) at the top of the forum (then read it again its a very good document)
Usually itā€™s the person who contributed most to your solution.
Sometimes thatā€™s the person who gives you the solution, but mostly itā€™s the person who points you at a part of the documentation or corrects your syntax, or even just gives you an unrelated clue that turns out to be the solution.

Thanks for your guidance on this. I thought about it the wrong way. I did not intend in any way to take credit for the solution, simply to make it easier for whoever in the future with a similar problem to find a complete working code. But I see the point now.

[quote=ā€œbackslashv, post:7, topic:260497ā€]

        {% set secs = as_timestamp(states.sensor.time.last_updated) - as_timestamp(states('sensor.ha_uptime')) %}
    {% set days = (secs / (24 * 60 * 60)) | int %}
    {% set plural = ' days ' if days > 1 else ' day ' %}
    {% set hrmn = secs | timestamp_custom('%H:%M', false) %}
    {{ days ~ plural ~ hrmn if days > 0 else hrmn }}

[/quote]

This is ā€œthe whole templateā€
Your version : -

[quote=ā€œbackslashv, post:7, topic:260497ā€]

- platform: template

sensors:
uptime_parsed:

[/quote]

Should NOT have the middle ā€œsensors:ā€ line (Iā€™m amazed it didnā€™t complain loudly at that).
By definition ā€œsensors.yamlā€ only contains sensors so this would be a repeat and is under - platform: template, which is a type of sensor.

When cutting and pasting stuff, get it working first, then customise it to your preferences then, so you know ā€˜whatever you last didā€™ was what broke it. Iā€™ve learned most of my templating from backing out of mistakes

Does this relate to my code as well?

  - platform: template
    sensors:
      uptime_parsed:
        friendly_name: Uptime
        value_template: >
          {% set secs = as_timestamp(states.sensor.time.last_updated) - as_timestamp(states('sensor.ha_uptime')) %}
          {% set days = (secs / (24 * 60 * 60)) | int %}
          {% set plural = ' days ' if days > 1 else ' day ' %}
          {% set hrmn = secs | timestamp_custom('%H:%M', false) %}
          {{ days ~ plural ~ hrmn if days > 0 else hrmn }}

It seems to be working, and the other ā€œvariantsā€ I have tried ā€œcomplains loudly at thatā€.

Donā€™t know, where does this sensor live ?
In your configuration.yaml, in sensors.yaml or in a package ?

I have it in sensors.yaml :slight_smile:

If anyone is interested, hereā€™s a one-line Template Sensor to report ā€˜up timeā€™.

  - platform: template
    sensors:
      up_time:
        friendly_name: Up Time
        value_template: >
          {{ ((now() - strptime(states('sensor.uptime'), '%Y-%m-%dT%H:%M:%S.%f%z')) | string)[:-10] }}

If the up time is less than a day, it is reported in this format (HH:MM)

20:46

When it exceeds a day, itā€™s in this format (N days, HH:MM):

2 days, 20:46

The template produces a Timedelta object so thatā€™s what governs the outputā€™s format. The templateā€™s [:-10] simply slices off the Timedeltaā€™s seconds and microseconds.

2 Likes

This post was wrong, you can view the edit to see just how wrong
:sob:
:stuck_out_tongue_winking_eye:
:rofl: