Missing something obvious when it comes to unit conversion - any ideas?

Hello, I have a Withings sleep sensor and am attempting to plot the data of each night.

The data is output in seconds. I have created template sensors to convert that data into minutes and then hours. There are several problems.

1: Strangely this works for some of the data but not others. See REM-Hours versus Deep Hours.

  1. At some point the seconds started being automatically converted to hours and minutes. See REM sleep in the chart (photo 1) - I definitely did not change that reading to hours. When I click on the entity in settings it’s still showing an output in seconds (photo 2).

  2. This data is being tracked cumulatively rather than an a series. It’s showing me the TOTAL time I have slept over the course of the week rather than reporting data about EACH day of the week. I Light Sleep-ed for about 5 hours last night and Friday night and want to chart that as a line graph - but this is adding both nights together. Edit: I have changed the sensors from measurement to total_increasing. That should change them to report the sleep data to last night only instead of all the sleep.

  #Adjusting sleep numbers

  - sensor:
      - name: "REM minutes last night"
        unique_id: "REM"
        state_class: measurement
        unit_of_measurement: "Minutes"
        state: >
          {% set REM = states('sensor.withings_sleep_rem_duration_seconds_name') | float(0) %}
          {{ REM / 60 }}

  - sensor:
      - name: "REM Hours last night"
        unique_id: "REMh"
        state_class: measurement
        unit_of_measurement: "Hours"
        state: >
          {% set REMh = states('sensor.rem') | float(0) %}
          {{ REMh / 60 }}

  - sensor:
      - name: "Deep Sleep minutes last night"
        unique_id: "Deep"
        state_class: measurement
        unit_of_measurement: "Minutes"
        state: >
          {% set deep = states('sensor.withings_sleep_deep_duration_seconds_name') | float(0) %}
          {{ deep / 60 }}
          
  - sensor:
      - name: "Deep Sleep Hours last night"
        unique_id: "Deeph"
        state_class: measurement
        unit_of_measurement: "Hours"
        state: >
          {% set deeph = states('sensor.deep') | float(0) %}
          {{ deeph / 60 }}
          
  - sensor:
      - name: "Light Sleep minutes last night"
        unique_id: "Light"
        state_class: measurement
        unit_of_measurement: "Minutes"
        state: >
          {% set light = states('sensor.withings_sleep_light_duration_seconds_name') | float(0) %}
          {{ light / 60 }}
  - sensor:
      - name: "Light Sleep Hours last night"
        unique_id: "Lighth"
        state_class: measurement
        unit_of_measurement: "Hours"
        state: >
          {% set lighth = states('sensor.light') | float(0) %}
          {{ lighth / 60 }}

This is most like due to the underlying integration having assigned a duration device_class to the entity. Device classes will change the way an entity’s state appears in the front end.

Are you sure you are using the correct entity_id for your source? The normal way an entity ID is set is by the name of the sensor, not the unique ID.

Hi Drew,

Where do I go to view the device class for the sensor provided by the Withings device? That information doesn’t come up in systems - devices - entities. In Dev Tools - states I see “duration” as you suspected. I am not sure when and why the duration device class chooses to spontaneously change the format of the reported data?

Regarding entity_id, I gave the sensor a friendly name for viewing purposes but its ID is still listed as sensor.withings_sleep_rem_duration_seconds_name. Below is a photo of me clicking on the REM seconds entity and seeing it has the proper entity_id. Is that how you mean?

My comment was more for about the following:

  - sensor:
      - name: "REM Hours last night"
        unique_id: "REMh"
        state_class: measurement
        unit_of_measurement: "Hours"
        state: >
          {% set REMh = states('sensor.rem') | float(0) %}
          {{ REMh / 60 }}

Unless you performed the workaround to use the unique ID to determine the entity ID or manually changed it, “sensor.rem” would most likely be “sensor.rem_minutes_last_night”. You can double check whether “sensor.rem” is correct/exists in Settings > Devices & Services > Entities, or in the Developer Tools > States tool.

It looks to me like the entity_id is correct at sensor.rem.

Also, this sensor is the one that’s working properly - the seconds to minutes to hours conversion is being reported correctly. Although the chart is still converting the seconds to minutes without my input, the manually-created REM-hours sensor number is still correct.

It’s the Light and Deep ones that should be configured the same way that mysteriously aren’t putting out the correct number of hours.

Then there’s also the problem of understanding why this is adding all of the nights together rather than separating the data into something you can chart for comparison between the different nights.

I have changed the sensors from measurement to total_increasing. That should change them to report the sleep data to last night only instead of all the sleep.