How to reset sensor time created with history_stats?

Good morning I have created a sensor to know how long I am lying in bed, this sensor is made with “history_stats” but I only wanted this to tell me when I am awake and not the hours of sleep. I need you to help me to know how to set this value back to 0. Thanks in advance.

This is my current sensor code:

  - platform: history_stats
    name: Tiempo en la cama
    entity_id: binary_sensor.withings_in_bed_jonathan
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

It cant tell when you are awake or asleep. Only when you are in or out of bed. If you want the hours out of bed use this:

  - platform: history_stats
    name: Tiempo en la cama
    entity_id: binary_sensor.withings_in_bed_jonathan
    state: "off"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

Hi @tom_l , thanks for your reply, maybe I’m explaining it wrong (I’m writing with translator) the sensor as you indicate marks if I’m in bed or not, that’s correct and actually calculates when I’m in bed for 24 hours. What happens is that I want to know when I am in bed during the day. My approach would be, I have a routine in the morning when I wake up and I want this to be the trigger for history_stats to set the time to 0 and thus tell me how long I was in bed but not counting my sleeping hours. I know how to activate it but I don’t know how to set the value to 0. (image)

image

You can’t reset the history_stats integration.

What you could do is this:

  1. Set an input boolean to ‘on’ in your morning routine automation.

  2. Write another automation that turns this input boolean ‘off’ when you get out of bed (trigger =
    binary_sensor.withings_in_bed_jonathan to ‘off’ ).

  3. Use the history stats integration to determine how long the input boolean has been on today.

Good morning,

It wasn’t exactly what I was looking for but it gave me the idea to do it. Thank you.

Regards.