Loading...
E.G.

KNX IP Gateway Group Cache: Stale States in Home Assistant

· updated
Table of Contents

52 lights and switches in my house sit on switching actuators whose application program has no feedback object. Home Assistant knew their state only when it saw the switching command itself. After every restart they stood at unknown, and after a central "all off" some of them stayed wrongly on. Between the end of September and 11 October I worked on that in several steps, and one of them turned out to be a problem in the KNX IP gateway, an older model with firmware from 2016. I am not naming the manufacturer or model here. The behaviour is a documented feature of that gateway, and I do not know which other gateways have a similar option. In November I changed the re-reading again, after a test showed that half of the read answers never arrived.

Read flags so Home Assistant can ask

In the ETS I set the "Read" flag on the object "Output n – Switch" of each actuator and partially programmed the actuators. With the read flag set, I can use the switching address as the entity's state address in Home Assistant. All 52 addresses returned an answer, though at that point the test did not tell me whether the answers came from the actuators or from the gateway. A script takes the ETS export and sets the addresses for reading and listening per light in the knx.yaml. Setting the flags in bulk also caught nine objects where "Read" does not belong: central functions, cover objects of one actuator and a push button. Per group address exactly one object should answer, so I removed those nine again, programmed the affected devices and compared the new export against the old one with a script: 9 flags changed, no link changed, no device added or missing.

Re-reading after the central command

The central "all off" goes to its own group address, and which outputs follow it is stored only in the actuator parameters. I did not want to rebuild that mapping in Home Assistant. A telegram on that address raises a knx_event, and an automation then reads all lights again. Since November it listens to the seven central groups for lights, also runs after a new KNX connection, a KNX reload and a Home Assistant start, and reads the lights one at a time; the reason is in the last section. This is the current version from my knx.yaml, with the group addresses replaced by placeholders and the list of 80 read addresses shortened. The two sensor entity ids are those of my KNX interface; if the sensor for the physical address has no value, the automation stops before reading:

# packages/knx.yaml (excerpt): group addresses replaced by placeholders,
# the list of read addresses shortened
knx:
  event:
    - address: ["9/2/1", "9/2/2", "9/2/3", "9/2/4", "9/2/5", "9/2/6", "9/2/7"]  # Nur Licht-Zentralgruppen, keine Sperrfunktion 9/2/8

automation:
  - id: knx_zentral_nachlesen
    alias: "KNX: Lichtzustände nach Zentralbefehl nachlesen"
    description: "Liest Lichtzustaende nach Zentralbefehlen, KNX-Wiederverbindung und HA-Start vom Aktor."
    initial_state: true
    mode: restart
    variables:
      licht_zentraladressen: ["9/2/1", "9/2/2", "9/2/3", "9/2/4", "9/2/5", "9/2/6", "9/2/7"]
      licht_leseadressen:
        - "9/1/1"
        - "9/1/2"
        - "9/1/3"
        # ... 77 more addresses, 80 in total
    triggers:
      - trigger: event
        event_type: knx_event
        event_data:
          telegramtype: GroupValueWrite
        id: zentral
      - trigger: state
        entity_id: sensor.knx_interface_verbindung_hergestellt
        id: verbindung
      - trigger: event
        event_type: event_knx_reloaded
        id: reload
      - trigger: homeassistant
        event: start
        id: start
    conditions:
      - condition: template
        value_template: >-
          {{ trigger.id != 'zentral' or
              trigger.event.data.destination in licht_zentraladressen }}
    actions:
      - delay:
          seconds: 35
      - condition: template
        value_template: "{{ has_value('sensor.knx_interface_physikalische_adresse') }}"
      # 300 ms Abstand: Sammelabfragen lieferten im Bustest nur 39/80 Antworten.
      - repeat:
          for_each: "{{ licht_leseadressen }}"
          sequence:
            - action: knx.read
              data:
                address: "{{ repeat.item }}"
            - delay:
                milliseconds: 300

The gateway answered for the actuators

After these changes Home Assistant still showed some lights as on after the central command, so I looked at where the read answers came from. The gateway has a setting its web interface calls a local group cache, described as a way to reduce bus load. It answers read requests with the last value it has seen on that address, without asking the actuator. It does see the central command, but it does not translate it into the individual outputs. So after "all off" it could still answer "on" for a light the central command had just switched off.

According to the manufacturer's manual the cache answers only read requests with low priority. Home Assistant reads with that priority, so every one of its reads was a candidate for a cached answer. Until the cause was fixed I left the re-read automation switched off for some days, because re-reading only fetched the cached values again.

How to check whether your gateway does this

I checked unlinked addresses, addresses without a read flag and the sender of the answers; only the last two gave me evidence of cached answers. They need a bus monitor or the ETS group monitor and a few read requests sent by hand with low priority, because according to the manual the cache answers only those.

1. Send a read request to a group address that is not linked to any device. In my case those got no answer, so silence on an unlinked address does not rule out a cache.

2. Send a read request to an address whose actuator object does not have the read flag yet. If an answer arrives, check its sender and the other objects linked to that group address before attributing it to a cache. One of my addresses answered before its read flag was set.

3. Look at the sender of the answers. An answer from the actuator carries the actuator's device address. The answers I got carried the tunnel address through which the new Home Assistant instance is connected to the gateway.

If you see the second or third sign, switch the lights on individually, send the central command, and read them again. With the cache I had, the lights covered by the central command then reported their old state.

Switching the cache off, and two more tunnels

The gateway's web interface has settings for tunnel addresses (up to 16), tunneling and routing each on or off, the local group cache, "initialise at start" and "clear cache". The login password was not documented anywhere. There is no password reset on this model, only a factory reset, and that also deletes the tunnel addresses and every other setting. I recovered the login without a factory reset, and the password is now in the password manager. The web interface allows one session at a time, and a second login throws out the first.

I switched the group cache off and raised the tunnels from 2 to 4. With two tunnels, one each for the old and the new instance running in parallel, the ETS got no connection at all while both were connected, and the gateway did not even answer its connection attempt. With four, the ETS got a tunnel without disconnecting either Home Assistant instance. I did not restart the gateway after the change. A firmware update does not exist for this model, and the successor is new hardware.

Checking the states after 6 seconds

The acceptance test after the change: three lights on, central "all off" sent, and a look at Home Assistant after 6 seconds. Two were off, one still showed on, with its last-changed time unchanged. That looked exactly like the old cache problem.

In October I put this down to timing. The automation then asked Home Assistant to update roughly 80 KNX lights and switches at once, the 52 without a feedback object and the ones that have one, the read telegrams left through the KNX interface's outgoing queue one after another, and the answers came back spread over several seconds. In this test the third light showed the correct state, off, after about 9 seconds. Anyone testing a re-read like this should check in the group monitor that an answer has arrived for each tested light before comparing the states. The delay configured in the automation only says when the requests start.

A second test in November

In November I looked at it again, because during the parallel run some lights still showed wrong states. I sent 80 read requests back to back from each instance. Both instances sent all 80, and on both only 39 of the 80 addresses got an answer; one series took about 3.4 seconds. With 300 ms between the requests, 80 of 80 answered. I have not proved where the telegrams got lost; the path through the gateway is my guess. The October test with three lights was too small to show this.

The automation now waits 35 seconds on the new instance and then reads the 80 addresses one by one with 300 ms in between, about 24 seconds per run. The old instance runs the same automation after 3 seconds, so the two read one after the other when both receive the same central command; after independent restarts or reloads their runs can still overlap. In the final test each instance got all 80 answers from the actuators within 2 seconds of each request, from 12 actuator addresses and none from a tunnel address. I tested this with read requests only; switching on site and the physical central buttons are not tested yet.

Frequently asked questions

How can I tell whether my KNX IP gateway answers from a cache?

Check in a bus monitor whether read answers come from the tunnel address instead of the actuator; mine did, even for an actuator object without a read flag. If yours do too, look for a group cache setting in the gateway's manual or web interface.

Does switching off the cache increase bus load?

The manufacturer describes the cache as a way to reduce bus load, so with it off every read goes to the actuator. I did not measure bus load before or after the change, so I cannot say by how much it went up.

How many tunnels do I need for two Home Assistant instances and the ETS?

Each of the three clients needs its own tunnel. I configured four and all three connected; I did not test a configuration with three tunnels.

Related articles

Questions or additions?

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