History stats with 2 entities?

Hello, I have 2 bed sensors (leg and head) , I want to see how long I slept so I did history stats and its work cool for 1 entity, but sometimes 1 of sensor get on if I put something on the bed so I want to do history stats with 2 entities , In fact I want it to show me history only that the two sensors are active , someone know if I can do it?

Thanks

You don’t say what sort of sensors they are. Assuming they are binary sensors…

Create a binary sensor group of your two sensors and set the all option to true. This way the group is on only when your two sensors are both on.

Feed your group to the history stats sensor.

Thanks!
I did it but its not working, this is history stats sensor

you can see the only sensor I changed to group not calculate the time.
this is the code not working:

- platform: history_stats
  name: Noy sleep time
  entity_id: group.sleep_time_noy
  state: 'on'
  type: time
  end: '{{ now().replace(hour=11, minute=0, second=0) }}'
  duration:
      hours: 15

and this code working good without group


- platform: history_stats
  name: Netta sleep time
  entity_id: binary_sensor.top_left
  state: 'on'
  type: time
  end: '{{ now().replace(hour=11, minute=0, second=0) }}'
  duration:
      hours: 15
- platform: history_stats
  name: Noy sleep time1
  entity_id: binary_sensor.bottom_right
  state: 'on'
  type: time
  end: '{{ now().replace(hour=11, minute=0, second=0) }}'
  duration:
      hours: 15
- platform: history_stats
  name: Netta sleep time1
  entity_id: binary_sensor.bottom_left
  state: 'on'
  type: time
  end: '{{ now().replace(hour=11, minute=0, second=0) }}'
  duration:
      hours: 15```

What you think the problem?
Thank you very much!

It’s not a group. It’s a binary sensor, if you used this:

It’s the group platform used to create a binary sensor.

Look in developer tools states.

ohhh, ok I made binary sensor group but its still show me “Unknown”.
this is the new code

binary_sensor:
    - platform: group
      name: Noy Sleep Time
      device_class: opening
      entities:
        - binary_sensor.top_right
        - binary_sensor.bottom_right
      all: true


- platform: history_stats
  name: Noy sleep time
  entity_id: binary_sensor.noy_sleep_time
  state: 'on'
  type: time
  end: '{{ now().replace(hour=11, minute=0, second=0) }}'
  duration:
      hours: 15

why its not working now? :slightly_frowning_face:

Test if the group changes state by triggering both binary sensors. This should also update the history stats.

I tested, its change the state group if i change the entities to on, so the group look right, but I still see unknown in history stats


Huh. That’s weird. Not sure why it is not working. It’s just a binary sensor like any other.

Oh its ok, I just had to wait a few hours for it to work.
Now its working good!
Thank you!!

1 Like