A motion sensor in Home Assistant reports on or off. For my dashboard I also wanted to see when each detector last reported motion, and the sensor does not keep that: its last_changed moves with every change, including the change back to off.
The PIR detectors already on the wall are enough for this. I store the time of their most recent change from off to on in one input_datetime helper per detector, written by a short automation. I use it for the seven KNX detectors in the corridors, on the stairs and in the garage area.
Update, November 2026: The automations below originally triggered on to: 'on' alone. That also matches the change from unavailable to on, so after a restart or a reconnect to the KNX interface a detector that reports motion at that moment can set a new timestamp without a new movement. On my old instance one helper showed the exact second of a KNX reload. The examples now include from: 'off'. On my new instance the seven automations have also become one, which I describe in my post on the KNX motion sensors.
Why input_datetime instead of last_changed?
Fair objection: every sensor already has a last_changed attribute. The problem is that last_changed flips on every state change — including the switch from on back to off. So you get the time of the last change, not the last motion. And it doesn't survive a Home Assistant restart cleanly: after a reboot the sensor sits freshly at off, last_changed is the boot time, and your history is gone.
An input_datetime helper fixes both. We write the timestamp only on the transition to on, the value is persistent (it survives restarts), and it's a real, queryable entity you can use in templates, conditions and on the dashboard just like any other sensor.
Step 1 — create one helper per room
Under Settings → Devices & Services → Helpers, create an input_datetime for each room with both date and time enabled. I keep the naming strictly consistent — letzte_bewegung_<room> (last_motion_<room>) — because that makes later scaling trivial: new entity, same schema. If you prefer YAML, the same thing goes into configuration.yaml.
input_datetime:
letzte_bewegung_gang:
name: Letzte Bewegung Gang
has_date: true
has_time: true
letzte_bewegung_garage:
name: Letzte Bewegung Garage
has_date: true
has_time: trueThe only thing that matters is that both flags are set — without has_time you lose the time of day and the whole pattern goes blunt.
Step 2 — write the timestamp on every motion
When the motion sensor changes from off to on, the automation writes the current time into the matching helper via input_datetime.set_datetime. I trigger on from: 'off' to 'on' — the off event doesn't interest us, and the from state keeps a detector that comes back from unavailable from counting as motion.
- id: '1711200000001'
alias: Letzte Bewegung Gang
triggers:
- trigger: state
entity_id: binary_sensor.bewegungsmelder_gang
from: 'off'
to: 'on'
actions:
- action: input_datetime.set_datetime
target:
entity_id: input_datetime.letzte_bewegung_gang
data:
datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
mode: singleThe now().strftime(...) formats the local time into exactly the format set_datetime expects. mode: single is plenty: the action takes milliseconds, parallel runs are irrelevant.
Step 3 — the same pattern across rooms
The real charm is repeatability. For every additional room you copy the automation and swap only two things: the entity_id of the motion sensor and the entity_id of the helper. Same shape, different room.
- id: '1711200000007'
alias: Letzte Bewegung Garage
triggers:
- trigger: state
entity_id: binary_sensor.bewegungsmelder_garage
from: 'off'
to: 'on'
actions:
- action: input_datetime.set_datetime
target:
entity_id: input_datetime.letzte_bewegung_garage
data:
datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
mode: singleOn my old instance there were seven of these automations, one per detector. The more compact form is a single automation with multiple triggers and a template over trigger.entity_id; that is what I use on my new instance, because the seven copies only differed in two entity names.
Step 4 — relative time on the dashboard
The raw timestamp "2026-07-17 14:32:10" is awkward on a dashboard — what's interesting is "12 minutes ago". For that Home Assistant has the template function relative_time(), which turns a datetime into a human-readable span. In a Markdown card it looks like this:
type: markdown
content: >
## Last motion
- **Hallway:** {{ relative_time(states('input_datetime.letzte_bewegung_gang') | as_datetime) }} ago
- **Garage:** {{ relative_time(states('input_datetime.letzte_bewegung_garage') | as_datetime) }} ago
- **Living room:** {{ relative_time(states('input_datetime.letzte_bewegung_wohnzimmer') | as_datetime) }} agoThe as_datetime converts the string state into a real datetime object that relative_time can process. The result is a compact overview — "Hallway: 3 minutes ago, Garage: 6 hours ago" — that tells you at a glance where there was life most recently.
What the data is good for
Once the timestamps exist, the interesting automations open up. A "house empty" heuristic: if the most recent of all letzte_bewegung_* times is older than, say, 30 minutes, the house counts as unoccupied — cheap presence detection without an mmWave sensor. Or room-scoped triggers: only ramp up the heating in a room if there's been motion there in the last hour. If you like the Modbus/sensor-data way of thinking, you'll find the same "data first, logic second" stance in my post on PV string fault detection.
Frequently asked questions
Why not just use the sensor's last_changed attribute?
Because last_changed flips on every state change — including the switch from on to off — and doesn't survive a restart: after a reboot the sensor sits freshly at off and last_changed is the boot time. The input_datetime helper, by contrast, stores only the last on-moment, is persistent, and survives restarts.
Does the value survive a Home Assistant restart?
Yes. input_datetime helpers are persistent — their last set value is stored and restored after a reboot. That's one of the main advantages over raw sensor attributes, which start from scratch after a restart.
Do I need a separate automation per room?
No. One automation per room is easy to read in the UI. If the entity names follow a pattern, a single automation with several triggers can derive the helper from trigger.entity_id, which is what I use now.
Does this work with any motion sensor?
With any that shows up in Home Assistant as a binary_sensor with on/off states — Zigbee PIR, KNX presence detector or Wi-Fi sensor alike. The automation reacts to the change from off to on. My KNX detectors switch the lights themselves on the bus, and the timestamp automation runs alongside that.


