Using History Stats to Catch a Door Sensor That Misses Open Events
A missed open event is a measurement problem first
A door sensor that occasionally fails to report open is one of the harder faults to pin down in a residential install. The failure is intermittent, the door still works, and by the time anyone notices, the event is gone. Replacing the sensor on a hunch is expensive and often wrong. The more useful first step is to make the measurement trustworthy: build a repeatable count of how many times the door reported open in a defined window, then compare that number against the raw state timeline. Only when the two disagree do you have a case for looking at the sensor itself.
Home Assistant’s History Stats integration is a reasonable instrument for that job, provided you understand what it actually counts. It does not detect missed events. It counts states within a period, and the difference matters.
What History Stats can and cannot tell you
The History Stats integration provides statistics about another entity using data from the history integration. It can track how long an entity has been in a specific state over a custom time period, and it supports three sensor types: time, ratio, and count.
The count type is the one that matters here, and its documentation is explicit about a subtlety that trips people up:
This will count states (for example, how many times a light was in the on state during the time period), as opposed to counting state transitions (for example, how many times a light was turned on). The difference is if the entity was already in the desired state at the start of the time period, that scenario will be counted with this sensor type.
So a count sensor on a door binary sensor’s on state answers “how many times was this door open during the window,” not “how many times did it open.” If the door was already open at the start of the window, that counts as one. If the door opened and closed three times, that counts as three. If the door opened and stayed open, that counts as one. The number is a state count, not an event count.
That distinction is why History Stats cannot directly detect a missed open event. It has no concept of an expected event. What it can do is give you a stable, repeatable number you can compare against the raw history timeline and against your own knowledge of how often the door was actually used. A mismatch between those is the diagnostic signal.
Build the diagnostic sensor
A binary sensor with the door device class reports on when open and off when closed, per the binary sensor documentation. The History Stats sensor should target that entity’s on state.
You need to provide exactly two of start, end, and duration. The documentation states this requirement plainly: “You have to provide exactly 2 of start, end and duration.” A rolling window is the most useful shape for a recurring diagnostic because it does not depend on the calendar day boundary:
sensor:
- platform: history_stats
name: Front door open count 7d
entity_id: binary_sensor.front_door
state: "on"
type: count
end: "{{ now() }}"
duration:
days: 7
Two practical notes on this configuration. First, the History Stats sensor updates when the source entity changes or once per minute if there is no source change, so the count is not instantaneous but it is close enough for a weekly diagnostic. Second, the duration must stay comfortably inside the recorder’s retention window. The documentation warns that if the duration exceeds the recorder’s purge_keep_days, the sensor “will not have all the information it needs to look at the entire duration.”
Keep the window inside the recorder’s retention
The recorder integration writes every entity state change to the database, and the History panel reads from that database. The default purge_keep_days is 10. A 7-day History Stats window leaves three days of margin, which is the right shape: the window should end well before the oldest retained data, not at the edge of it.
There is a known class of misleading values when a History Stats window reaches past stored history. In a GitHub issue thread on incorrect time-type values, a contributor offered an explanation: when History Stats finds no recorder data in the range it is searching, it extrapolates the oldest datapoint backwards to the start of the window. The thread contains disagreement about whether that explanation covers every reported anomaly, and the issue was closed without a confirmed root cause in the retrieved text. Treat the extrapolation behavior as a plausible mechanism and a reason to keep margin, not as a settled account of every spike.
The practical discipline is the same either way: set the History Stats window shorter than purge_keep_days, and if you need a longer diagnostic window, raise purge_keep_days first. That is a retention change, not a sensor fix, and it costs disk. On an SD-card install the recorder documentation already recommends raising commit_interval or excluding entities to reduce wear, so extending retention is a tradeoff to make deliberately.
Cross-check against the raw history
The count sensor is only half the instrument. The other half is the History panel for the same entity over the same window. Open the door sensor’s history, set the range to match the History Stats window, and count the on segments by eye or by scrolling. Then compare that number to the sensor’s value.
Three outcomes are possible, and they mean different things:
- The numbers match. The measurement is trustworthy for this window. If the door still seems to miss events, the problem is upstream of Home Assistant: the sensor is not sending the event, or the integration is not receiving it. That is a sensor-side investigation, not a History Stats one.
- The count sensor is lower than the raw timeline. This is the interesting case. It can mean the History Stats window is misconfigured, the entity was
unavailableorunknownfor part of the window, or the recorder was not running for a stretch. It does not by itself prove the sensor is faulty. - The count sensor is higher than the raw timeline. Usually a window or retention problem, or a state that was already
onat the start of the window and got counted. Check the window boundaries before drawing any conclusion.
A binary sensor can also be unavailable or unknown, and those states are not the same as off. If the sensor dropped off the network and came back, the History Stats count for on will not include the unavailable period, but the raw timeline will show a gap. That gap is itself diagnostic information, and it is the reason the cross-check step cannot be skipped.
Interpret carefully before escalating
A low count is a prompt to investigate, not a diagnosis. Before pointing at the sensor, rule out the alternatives:
- The door genuinely was not opened as often as you assumed. A week with a count of four may be correct if the household was away for three days.
- The entity was unavailable for part of the window. Check the raw history for gaps and for
unavailabletransitions. - The window was misconfigured. Verify that
start,end, anddurationresolve to the period you intended. The History Stats documentation recommends the template developer tools page for checking that the period is right, and notes that the sensor’sfromandtoattributes show the resolved window. - The recorder was not running or was purged. A restart, a database repair, or a purge can leave a hole in the data that looks like a missed event.
Only after those are excluded does the sensor itself become the suspect. At that point the investigation moves to the integration layer: whether the sensor is reporting at all, whether its events are reaching Home Assistant, and whether the pairing or placement is marginal. Those are separate diagnostics, and the retrieved sources for this article do not cover integration-specific behavior for Zigbee2MQTT, Z-Wave JS, ESPHome, or MQTT door sensors. Do not assume a History Stats count tells you anything about radio quality or battery state; it does not.
A repeatable procedure
The value of this approach is that it can be run the same way every week. The procedure:
- Confirm
purge_keep_daysin the recorder configuration. The default is 10. - Create a
count-type History Stats sensor on the door binary sensor’sonstate with a window shorter thanpurge_keep_days. A 7-day window against a 10-day retention is a reasonable starting point. - Record the sensor’s value and the resolved
fromandtoattributes. - Open the same entity’s History panel for the same window and count the
onsegments. - Compare. If they match, the measurement is trustworthy and any remaining concern is sensor-side. If they do not match, investigate the window, the availability gaps, and the recorder before touching the sensor.
- Keep a short log of the window, the count, and the raw timeline count. A pattern across several weeks is more informative than a single reading.
This is deliberately unglamorous. It does not produce an alert, and it does not tell you the sensor is broken. It produces a number you can defend, which is the prerequisite for any hardware decision.
Graceful degradation and the local-first case
Everything in this procedure runs on local recorder data. The History Stats sensor reads from the same database the History panel reads from, and the recorder writes state changes locally regardless of whether a vendor cloud is reachable. That matters for a diagnostic you intend to run for weeks: if the vendor’s cloud goes down, the door sensor may still report locally through its integration, and the History Stats count will keep accumulating. If the integration itself depends on a cloud connection, the sensor will go unavailable, and the raw history will show that gap. Either way, the diagnostic does not silently stop working; it either keeps counting or records the outage.
That is the argument for building the instrument on recorder data rather than on a vendor dashboard. The vendor dashboard may show a cleaner graph, but it will not tell you what Home Assistant saw, and it will not survive the cloud going away.
When a simpler approach is better
Not every door needs this. If the door is used a few times a day and a missed event is a minor annoyance, a plain automation with a state trigger is enough, and the trigger documentation is clear that a door-open trigger fires on the transition from closed to open and does not fire again while the door stays open. If you want a notification when the door opens, that trigger is the right tool. History Stats is for the case where you suspect the trigger is not firing and you need evidence.
If the door is critical, a mechanical switch or a wired contact may be the smarter choice regardless of what the diagnostic shows. A battery-powered radio sensor is a convenience device, and no amount of counting will change its failure modes. The diagnostic tells you whether the sensor is missing events; it does not make the sensor more reliable.
FAQ
Can History Stats tell me directly that a door sensor missed an event?
No. It counts states within a window. The diagnostic is the comparison between that count and the raw history timeline, not the count alone.
Why does my count sensor show one more than I expected?
If the door was already open at the start of the window, that state is counted. The documentation states this explicitly for the count type.
What happens if the window is longer than the recorder’s retention?
The sensor will not have all the information it needs for the entire duration. Keep the window shorter than purge_keep_days, or raise retention deliberately and accept the disk cost.
Does the sensor update in real time?
It updates when the source entity changes or once per minute if there is no source change. For a weekly diagnostic, that is close enough.
Should I raise purge_keep_days to fix missed events?
No. Retention is a storage setting, not a sensor diagnostic. Raising it gives you a longer window to measure, but it does not make the sensor report more reliably.
If you are auditing several small systems at once, the same discipline applies: measure first, then decide. A broader checklist for that kind of audit is in How to Audit the Small Systems That Quietly Run Your Week.