ha_history_get_state_history
Retrieve state-change history for Home Assistant entities. Use session mode to count and measure durations of specific states, or timeline mode for full transition logs.
Instructions
Gets the state-change history for any discrete-state entity in Home. Use for binary sensors (motion, door, window, presence), person/device_tracker (zone and location history), and any entity with named states.
Two modes: session mode when state_value is set, timeline mode when it is omitted. Source: HA recorder state history — limited to ~10 days by default. Returns a timeline of transitions, or session counts and durations when state_value is set.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end_time | No | Period end, same formats as start_time. Default: now. | |
| group_by | No | Session mode only. "day" returns one row per day (date | count | first | last | total active). Combine with day_start_hour=6 for overnight/cross-midnight grouping. | |
| entity_id | Yes | Exact entity_id resolved earlier, e.g. "binary_sensor.entrance_motion". Do not guess. | |
| comparison | No | Session mode only. Compare session count and duration to a reference period. | |
| start_time | No | Period start. Formats: relative (7d/30d/24h/2w/1y), named ("last month"/"last week"/"yesterday"/"last summer"/"Q1"), or ISO ("2026-04-01"). Default: 30d. Special: "overnight" = 22:00 yesterday → 06:00 today. | |
| max_results | No | Max rows to return. Default: 20. Pass "all" to list all. Timeline mode: caps transitions shown. Session mode: caps sessions or days. | |
| state_value | No | Target state to detect sessions for, case-insensitive. Binary sensors: "on" or "off". Person/device_tracker: a zone name e.g. "home", "not_home". Prefix ! for "any state except", e.g. "!home" = time spent outside Home. Omit to get a full timeline of all state transitions instead. | |
| day_start_hour | No | Only with group_by="day". Shifts the day boundary from midnight to this hour (0-23). E.g. 6 → post-midnight activity (01:30) is attributed to the previous evening's date. | |
| max_gap_minutes | No | Session mode only. Bridge inactive gaps shorter than this into one session (minutes). Default: 0 (no merging). E.g. 2-5 for motion sensors when a person pauses briefly. | |
| min_duration_minutes | No | Session mode only. Discard sessions shorter than this (minutes). Default: 0. Filters brief glitches, e.g. 1 for motion sensors. |