Loading...
E.G.

KNX Motion Sensors in Home Assistant: What HA Does With Them

· updated
Part 1 of 6Series: KNX in Home AssistantAll parts
  1. 01KNX Motion Sensors in Home Assistant: What HA Does With Them
  2. 02Home Assistant Thermostat on KNX: Base vs. Active Setpoint
  3. 03KNX Thermostat Shows Standby After a Home Assistant Restart
  4. 04Window Open, Heating Off: Auto-Switch Each KNX Thermostat to Frost Protection in Home Assistant
  5. 05Decoupling a KNX Alarm from the JUNG Panel in Home Assistant
  6. 06Move KNX Switches From the Home Assistant UI to knx.yaml
Table of Contents

The house has seven KNX motion detectors, in the corridors, on the stairs and in the garage area. When I took stock of the lighting for my Home Assistant migration in September, I expected it to be one of the biggest parts. It turned out to be one of the smallest, because Home Assistant does not switch any of these lights.

The detectors switch the lights themselves

Each detector is linked to its light actuator in the ETS project, so a movement switches the light directly on the bus. Home Assistant is not involved. The lights work when Home Assistant is down, when it restarts after an update, and they kept working while I ran an old and a new instance side by side during the migration.

If your detectors can switch the actuator directly, I would leave the light logic there. Moving it into Home Assistant adds one more component that has to be running before a corridor light comes on, and for a corridor or a staircase I want nothing in between. In this house I use Home Assistant to record when each detector last saw movement and to show those times together on the dashboard.

What Home Assistant does with them

Each detector shows up in Home Assistant as a binary sensor. For each detector, Home Assistant stores the time its binary sensor last changed from off to on in an input_datetime helper, and the dashboard shows those times as an overview. On the old instance that took seven automations, one per detector, which differed only in two entity names. The helper pattern itself is described in my post on a last-motion sensor.

Seven automations become one

On the new instance it is a single automation. It derives the target helper from the entity id of the detector that triggered it, which only works because the naming was already consistent: every detector is called bewegungsmelder_ followed by its location, and every helper letzte_bewegung_ followed by the same location. The mode is parallel, so two detectors that fire in the same second do not drop each other. The example comes from a package file and the location names are placeholders; in the YAML editor of a single automation, leave out the automation: line and the dash before id.

automation:
  - id: last_motion_per_detector
    alias: "Remember the last motion per detector"
    mode: parallel
    triggers:
      - trigger: state
        entity_id:
          - binary_sensor.bewegungsmelder_a
          - binary_sensor.bewegungsmelder_b
          - binary_sensor.bewegungsmelder_c
        from: "off"   # not from unavailable, see below
        to: "on"
    actions:
      - action: input_datetime.set_datetime
        target:
          entity_id: >-
            {{ trigger.entity_id | replace('binary_sensor.bewegungsmelder_',
               'input_datetime.letzte_bewegung_') }}
        data:
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

For another detector I add its entity id to the trigger list and create an input_datetime helper with date and time enabled whose entity id matches the template: binary_sensor.bewegungsmelder_d needs input_datetime.letzte_bewegung_d.

Phantom motion on every restart

The old automations triggered on to: 'on' without a from state. That also matches the change from unavailable to on, so a detector that becomes available again after a restart or a reconnect to the KNX interface, while it reports motion, can update the timestamp without a new movement. On the old instance one of the last-motion helpers showed 19:19:27, the exact second in which I had reloaded the KNX integration that evening. Because the time matched the reload to the second, I concluded that the automation had recorded the detector becoming available again. The new automation requires a change from off to on. If a detector already reports motion when it becomes available again, its helper is only updated at the next change from off to on.

Checking it against the old instance

It was one of four automations I switched on immediately on the new instance instead of leaving them disabled. It writes only into helpers of its own instance and sends nothing to the bus, so two instances recording the same movements cannot get in each other's way. I copied the helper values from the old instance through the REST API, so the dashboard did not start at never.

Then I waited for a real movement. At 21:05:54 someone walked through one of the corridors, and both instances stored the same second. I had copied the values of the other six helpers beforehand, so this one detector was the actual test. It was the first point in the migration where the new instance did the same thing as the old one in real time, through the cloud VM, the VPN and the KNX tunnel. The overall setup is described in my post on running old and new in parallel.

What I have not done

Home Assistant has no motion-light automation in this house, so I cannot report on brightness by time of day or switch-off delays from my own setup. The detectors' own settings, such as how long the light stays on, are parameters in ETS, and I left them as they were during the migration.

Frequently asked questions

Should Home Assistant switch the light when a KNX detector sees motion?

If the detector can be linked to the light actuator in ETS, I keep it on the bus. The light then works without Home Assistant.

Why does my last-motion time jump when Home Assistant restarts?

A trigger with only to: 'on' also fires on the change from unavailable to on. Add from: 'off'.

Can one automation serve several detectors?

Yes, if the entity names follow a pattern. A template then derives the helper from trigger.entity_id.

Related articles

Questions or additions?

Comments run on GitHub Discussions, so you need a GitHub account to write one.