Bed occupancy value template based on Withings Sleep Analyzer

I’ve tried to duplicate (and edit) my people_home-template to use with my Withings Sleep Analyzers, but I’m not getting it right. What do I have to change to get it right? The purpose is to have the number of people in bed, e.g. 0, 1 or 2.

  - platform: template
    sensors:
      people_home:
        friendly_name: How many are home?
        entity_id:
        - person.male
        - person.female
        value_template: >-
          {{states.person | map(attribute='state') | select('eq','home') | list | length }}
  
  - platform: template
    sensors:
      in_bed:
        friendly_name: How many in bed?
        entity_id:
        - binary_sensor.withings_in_bed_male
        - binary_sensor.withings_in_bed_female
        value_template: >-
          {{states.binary_sensor.withings | map(attribute='state') | select('eq','on') | list | length }}

Keep in mind, the number of people who can help is far greater that the number of people who have Withings.

( Include what the entities are)

The problem is that both mentioned binary sensors were created automatically. Do you mean that I have to mention every sensor.withings individually?

I have no clue what the state or attributes of the sensor(s) is.
That is very helpful information if I want to have any chance in helping you.

1 Like
- platform: template
    sensors:
      in_bed:
        friendly_name: number of occupied beds
        entity_id:
        - binary_sensor.withings_in_bed_male
        - binary_sensor.withings_in_bed_female
        value_template: >-
          {{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'occupancy') | list | count }}

Thanks to @Knottyboy’s topic I knew I had to make a filter on device class occupancy, instead of fiddling with states.binary_sensor

1 Like