Count hours state false

Hi,
I need help, how can I get from the entity_id time how long has it false state and when it comes again it false state count from the beginning.

The counter needs to work even if the HA is reset.

Not sure about this though:

The history stats sensor relies on the recorder to generate the stats. So if it is not restored after a restart (and it probably will be) then the next time the state of the monitored sensor changes the history stats sensor should update.

I try to use history_stats, but I don’t know how to properly configure it to stop measuring when it is no longer state false and when it comes again it false state count from the beginning.

It would help me if I can use history_stats so that it resets every 72hours, is with this configuration it resets me every 24h:

  • platform: history_stats
    name: name_x
    entity_id: sensor.name_x
    state: “False”
    type: time
    start: “{{ now().replace(hour=0, minute=0, second=0) }}”
    duration:
    days: 3

This uses data starting from midnight 3 days ago until the current time.

  - platform: history_stats
    name: name_x
    entity_id: sensor.name_x
    state: 'False'
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=3) }}'
    end: '{{ now() }}'

Are you sure the sensor you are using reports its state as True and False? Go to Developer Tools > States, find the sensor and see what it reports as its current state.

Thanks, that’s what I was looking for. I have tried and it works, that it is True and False.

Thanks for your help.

You’re welcome! Glad to hear to hear it works now.

Please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title which serves to indicate this topic has an accepted solution. This helps other users find answers to similar questions.

Sorry, this is not working as I expected. Has an interval of 3 days but at 00:00:00 decreases by 24h.

Ideally, it can display a history of 72h or more and always starts counting from the beginning when the condition switches state to false.

It does not have to be a configuration via history_stats.

The counter is ok but it’s not good for me because if I reset the HA the counter is also reset.

It’s a sliding window of 3 days that “slides” every 24 hours. If that’s not what you wanted, please elaborate how you would prefer to see it work.

Maybe not but there’s very little else that can come close to analyzing time-based without having its calculations impacted by a restart.

Your suggestion regarding timedelta helped me, I went to study and found that I needed a timestamp.

I managed to solve it:

sensor:
  - platform: history_stats
    name: name_x
    entity_id: sensor.name_x
    state: "False"
    type: time
    start: >
      {{ as_timestamp(states('input_datetime.x')) }}
    end: '{{ now() }}'

At helper created: Date and/or time name input_datetime.x

scripts:
 reset_run_time:
  alias: 'Reset time - x '
  sequence:
  - service: input_datetime.set_datetime
    data:
      entity_id: input_datetime.x
      timestamp: '{{ as_timestamp(now()) }}'
  - service: history_stats.reload
    data: {}
  mode: single

When the sensor again comes to False through automation I run the script Reset time - x to reset the sensor time and start measuring again.

Can you explain how that works like what you asked for in your second post?

so that it resets every 72hours

Anyone reading your first and second posts and then your own Solution post, is likely to be confused as to how it is achieved.

This is to monitor the water in the humidifier, to monitor the water for 72h and after that, the automation sends a notification to change the water.

How could automation work and sends the notification, I had to monitor the sensor for 72 hours in one part.

There was no way I could find a solution to constantly monitor the condition of the sensor in my case 72h always with my previous configurations it was reset after 24h or the value decreased by 24h, and this didn’t a real situation.