Withings Sleep Sensor Data - Individual nights rather than cumulative totals

Hi all,

I am using the Withings Sleep mat - overall it seems to be working well. However, when add the entity sensors for the sleep metrics, it is adding each nights sleep data. I would rather just see last nights sleep data and log the rest in history. An example…

withings-sleep-sensors

I swear I did not sleep 16 hours last night! I did however sleep this long over 2 nights. Below is an example of a custom sensor entity where I convert the returned Withings sensor data from seconds to minutes and hours…

      sleep_deep:
        friendly_name: 'Deep Sleep'
        value_template: >-
          {% set dstime = states.sensor.withings_sleep_deep_duration_seconds_brad.state | int %}
          {% set seconds = dstime % 60 %}
          {% set minutes = ((dstime % 3600) / 60) | int %}
          {% set hours = ((dstime % 86400) / 3600) | int %}
          {% set days = (dstime / 86400) | int %}
          {%- if days > 0 -%}
            {%- if days == 1 -%}
              1 day
            {%- else -%}
              {{ days }} days
            {%- endif -%}
            {{ ', ' }}
          {%- endif -%}
          {%- if hours > 0 -%}
            {%- if hours == 1 -%}
              1 hour
            {%- else -%}
              {{ hours }} hours
            {%- endif -%}
            {{ ', ' }}
          {%- endif -%}
          {%- if minutes > 0 -%}
            {%- if minutes == 1 -%}
              1 minute
            {%- else -%}
              {{ minutes }} minutes
            {%- endif -%}
          {%- endif -%}
        icon_template: mdi:sleep

I am guessing I somehow need to add some logic to limit the sensor date between times (say 8pm to 9am or similar) - but I cannot for the life of me figure out how to do this. I am hoping someone will be kind enough to share their wisdom and point me in the right direction. Thanks in advance.

Getting closer (I think). I believe I need to use History State to return values between times. My sensor’s State returns a value in seconds - I am struggling with the below code “State” value…

  - platform: history_stats
    name: Last Night Light Sleep
    entity_id: sensor.withings_sleep_light_duration_seconds_brad
    state: '?'
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'

When I look at the state for sensor.withings_sleep_light_duration_seconds_brad - it returns a numerical value in seconds (as expected) - however I am stuck - how would I effectively say State is greater than 0 (as I cannot explicitly define the value as it always changes)? Or is there a better way?

What integration are you using to pull this data?

I am using the Withings integration (https://www.home-assistant.io/integrations/withings/)

Hi Brad,

Sorry to resurrect this two year old thread but I am exactly where you were at in your posts here. Did you (or anyone else) ever get this working as intended? Thanks in advance!