Time of window open in last 24h

Hello

I have some contact sensors. There exists the attribute “last_changed” which is an interesting value. But I want more. I want to display the time how long the window is open in the last 24 hours.
I am thinking about sql sensor and trying to do a query. Is this a good idea?

Has somebody an idea how to do this?

Thanks
Gilbert

You may wish to consider using a History Statistics Sensor.

I use it to report how long our furnace was heating (for the current day).

  - platform: history_stats
    name: Heating Hours Today
    entity_id: climate.thermostat
    state: 'heat'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
2 Likes

Is yours still working? I moved my database to Maria10 on Synology NAS and around that time, my pool ‘time-on’ sensor stopped working. I get 0m now.

  - platform: template
    sensors:
      pool_pump_status:
        value_template: '{% if is_state("switch.pool_pump", "on") %}Running{% else %}Stopped{% endif %}'
        friendly_name: 'Pool Pump Status'

  - platform: history_stats
    name: Pool Pump running today
    entity_id: sensor.pool_pump_status
    state: 'Running'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

  - platform: template
    sensors:
      pool_pump_time_on:
        value_template: '{{ states.sensor.pool_pump_running_today.attributes.value }}'
        friendly_name: 'Pool Pump Hours Running'

Hello xbmcnut, 123
For me the solution is working perfect. Thanks very much!
Gilbert

Something is wonky with my system since recent updates. The pool pump state is currently ‘Stopped’ but my template sensor to count the time is still counting up. Argh…:anguished: