Elapsed Time That a Sensor/Device is in a certain state?

As the title says, is there any way in HomeAssistant to report a total elapsed time that a device and/or sensor is in a state for a period of…say 24 hours?

More in depth, I’d like to specifically know the total time that my thermostats are in a “cool” or heat" state during the day.

Not sure if this is something that can be used in HomeAssistant or something that could potentially be a feature in later builds.

2 Likes

Take a look at this

4 Likes

For reporting you could also use Jupyter Notebooks.

Fantastic, @danichispa! This is exactly what I was looking for! Thank you!

Edit: So, for examples (and transparency), I want to see how long my AC system runs for the day. Code should be as follows in sensor.yaml:

platform: histroy-stats
name: AC On Today
entity_id: sensor.thermostat_operation
state: 'cool'
type: time
end:  '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
duration:
  hours: 24
1 Like

So when i would like to track the time of a switch being on, and start the timing again when it switches off and on again, how would i do that, the time period on is variable… so i do not think duration is a good option.

*history-stats :wink:

Look for the same thing.
Want to create a custom sensor that tracks how long a switch/light is turned on or a user is at home or another zone. i dont care about the last 24 hours but only the time since the state changed to “on”.

Sorry to pump old threat!

I want to track how long one sensor is in the state of “False”. I am doing this with history_stats sensor but my problem is that I am not interested for example 24 hours. What I would like to is that when the “False” state is changed to “True” the recording will end and when the state changes back to “False” the recording would start from the beginning. And I am unable to do this.

What I have tried is that I tried to set it so:

start: {{ states.sensor.machine.last_changed }}
end: {{ now() }}

but this will just give me 0.0 as a result and it stays on that. The last_changed value is correctly formed timestamp.

Any ideas how can I only start from 0 when the state changes back to “False”?

I tested this and the following worked for me:

  - platform: history_stats
    name: Vasketid
    entity_id: input_select.status_vaskemaskin
    state: 'Vasker'
    type: time
    start: '{{ as_timestamp(states.input_select.status_vaskemaskin.last_changed) }}'
    #start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

It now counts up since the last change.

2 Likes

I’m also struggling with the same topic. I would like to visualise how long a sensor or switch was in a specific state - but then how long each time, not calculated together. History-stats does only part of the job, but counts everything together for a day.

E.g. I want to know and see a graph that a specific window or door was open for 20 minutes at 10 AM and 30 minutes at 4PM, and so on, over multiple days/weeks even. But I can’t seem to get this right. what am I missing?

1 Like

Did you ever figure this out? I like having the sensor but would love to see it over time like you’re suggesting.