Template Sensor returns 1 hour too much when converting seconds to HH:SS

Hi All,

Hopefully one of you can help me further.

I have 2 Withings sleeping mats and these report time back in seconds and I would like to see the format HH:MM.

So I made a template sensor entity, only they all show 1 hour too much. So when something is 0 seconds and therefore 0 hours then I see 01:00. At 15.900 seconds it should be 04:25 but I see 05:25.

Of course I googled and searched the forum but I didn’t do this well enough because I didn’t find a suitable option.

Hopefully one of you can help me further.

Thnx up front.

BR,
Chad

sensor:
  - platform: template
    sensors:
      sleep_light_duration_seconds_chad:
        friendly_name: Lichte slaap Chad
        value_template: >
          {% set seconds = ( states('sensor.withings_sleep_light_duration_seconds_chad') | int ) | timestamp_local %}
          {{ strptime(seconds, '%Y-%m-%d %H:%M:%S').strftime('%H:%M') }}
  - platform: template
    sensors:
      sleep_light_duration_seconds_cindy:
        friendly_name: Lichte slaap Cindy
        value_template: >
          {% set seconds = ( states('sensor.withings_sleep_light_duration_seconds_cindy') | int ) | timestamp_local %}
          {{ strptime(seconds, '%Y-%m-%d %H:%M:%S').strftime('%H:%M') }}

You’re running into time zone issues because you’re using datetimes. And strptime takes a datestring and converts it to a datetime object. It does not accept seconds. If you don’t care about going over 24 hours, all you need is

{{ states('sensor.withings_sleep_light_duration_seconds_cindy') | int(0) | timestamp_custom('%H:%M', false) }}
1 Like

Dear Petro, 1st thanks for your quick reply. Secondly thanks for the solution offered. It works now and it’s also clear to me why it didn’t with my code.

1 Like