Not able to calculate time sensor spends in a state

I am trying to calc time the sensor state stays wet before it changes to dry. It has two states “Wet” and “Dry”

  - platform: history_stats
    name: Water_Sensor_WetStatus
    entity_id: binary_sensor.water_leak_detector_water_alarm_water_leak_detected
    state: "wet"
    type: time
    start: "{{ now() }}"
    end: "{{ now().replace(hour=17, minute=0, second=0) }}"


The above script never records time.
water time

Change this:

    state: "wet"

to this:

    state: "on"

A binary_sensor’s state is on/off (you can confirm that by inspecting the state of binary_sensor.water_leak_detector_water_alarm_water_leak_detected in Developer Tools → States). The reason you see it reported as Wet/Dry in the dashboard UI is because the binary_sensor’s device_class is moisture.

1 Like

Also the period is weird. At 8:00, will it try to look from now into the future? And what will the sensor report at 18:00? Does the sensor support a reverse time period? Normally you’d look from the start of the day until now, or 24 hours back.

Yea my time could be wrong. I want to the recording to start asap and record until it stays “on” not sure if


is correct or

now().replace(hour=0, minute=0, second=0).


   start: "{{ now().replace(hour=0,minute=0,second=0) }}"
   end: "{{ now() }}"

That records from midnight to right now

(This is exactly what I use to record the length of time my heating has run today)

If you want only the length of the last occurrence then I think you need an input helper or other sensor to store the time it went on for that. The last changed value can can only be used while the sensor is on. If you use that directly, the value will always be 0 if there is no moisture. (Unless that is what you want of course). But then you do not heed a history sensor, and you could just as easily use a template sensor.

If you replace the time with 0 you will get the sum for today. That will always be 0 at 0:00, even if the wet started before midnight though.

So it all depends: if it was wet but now is dry, do you want the length to stay or go to 0. And if you have multiple wet periods on a day, do you want them to sum op or do you only want the length of the last time, no matter if it started before today?

I would like the time to stay and average of multiple time it was wet during the day.
I just can’t figure out how to do that- just starting my beginner journey.

This reports a running total of the time when water was detected for the current day (from midnight until the current time).

  - platform: history_stats
    name: Water_Sensor_WetStatus
    entity_id: binary_sensor.water_leak_detector_water_alarm_water_leak_detected
    state: "on"
    type: time
    start: "{{ today_at() }}"
    end: "{{ now()}}"

I use a nearly identical configuration for History Stats sensors that report total heating/cooling time for the current day.

Thank you, it has been working.
How do you hold the previous value as when the sensor state changes it goes back to blank or perhaps take the average and then show that on the history graph?

Nevermind I got it using the graph object inside the Lovelace UI.

I use the Utility Meter integration to create four sensors that track daily, weekly, monthly, and yearly statistics for a History Stats sensor. By looking at each of the four sensor’s history you can see its previous values.

Here’s how you would configure four Utility Meter sensors to track your wetstatus sensor’s daily, weekly, monthly, and yearly statistics.

    source: sensor.water_sensor_wetstatus
    name: Wetstatus Daily
    cycle: daily
    source: sensor.water_sensor_wetstatus
    name: Wetstatus Weekly
    cycle: weekly
    source: sensor.water_sensor_wetstatus
    name: Wetstatus Monthly
    cycle: monthly
    source: sensor.water_sensor_wetstatus
    name: Wetstatus Yearly
    cycle: yearly