Max time door was open in day

Hi, I am new to Home assistant.

I did this:

sensor:
  - platform: history_stats
    name: Door open today
    entity_id: door.my_door
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

That give me total time of door open, if I change time to count I also get number of time door was opened.

How do I get max time door was opened in day before it was closed?

Can I get top 5 door opened time?

Regards, Ales

The following Trigger-based Template Sensor monitors a binary_sensor (like a door) and reports the longest time it was left on its longest attribute (in seconds). The 5 highest values are reported in its openings attribute including the time when it was turned on and off.

The sensor’s state value simply reports the most recent time when the binary_sensor was turned off.

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.front_door
        from: 'on'
        to: 'off'
      - platform: time
        at: "00:00:00"
    sensor:
      - name: Door Openings
        unique_id: template_door_openings
        state: "{{ now().timestamp() | timestamp_custom() }}"
        attributes:
          longest: >
            {% if trigger.platform == 'state' %}
              {% set current = this.attributes.get('openings', []) | map(attribute='duration') | list %}
              {% set new = [(now() - trigger.from_state.last_changed).total_seconds() | int(0)] %}
              {{ ((new + current) | sort(reverse=true))[0] }}
            {% else %}
              unknown
            {% endif %}
          openings: >
            {% if trigger.platform == 'state' %}
              {% set current = this.attributes.get('openings', []) %}
              {% set new = [{
                "opened": (trigger.from_state.last_changed | as_local).isoformat(),
                "closed": now().isoformat(),
                "duration": (now() - trigger.from_state.last_changed).total_seconds() | int(0) }] %}
              {{ ((new + current) | sort(reverse=true, attribute='duration'))[:5] }}
            {% else %}
              {{ [] }}
            {% endif %}

Screenshot


EDIT 1

I overlooked to mention that the values in the openings attribute are sorted by duration in reverse order (longest duration value is first).

EDIT 2

Modification. The openings attribute rejects entries that are older than today.

EDIT 3

Redesigned. Uses a Time Trigger to reset all values at the start of each day.

I was also interested in a similar task.
Could you explain what does this expression mean?
Why using “get()”, can’t we use just “attributes.openings”?

It means:

Get the value of the openings attribute but if it doesn’t exist then return an empty list.

this.attributes is a dictionary so it has a get method.

Thanks, I will try this.

Regards, Ales

Hi, It works.

I need to limit longest result to current day, as now shows all time longest. I need longest for current day

Thanks.

Regards, Ales

Use the modified example posted above. It now employs a Time Trigger to reset all values at the start of each day.