Recommendations to organize configuration files for KNX integration

Are there any best practices and/or recommendation on how to best organize the configuration file(s) for the KNX integration?

Putting everything into one file will get confusing quite quickly. Home assistant has some means to split the configuration.

This will allow you to split across the different domains (sensor, switch, cover, light, etc.).

Personally I would prefer to split across devices, e.g. have a separate file for every device. Some of my devices have entities in different domains, though, e.g. there could be binary_sensor, sensor and switch entities, etc.

Maybe I’m thinking to complicated here, but I don’t see a way to merge different files across different domains.

For example, this is how I would like to organize my files:

og_z1_pm1.yaml:

binary_sensor:
    - name: "OG-Z1-PM1 Presence"
      state_address: "2/4/1"
sensor:
  - name: "OG-Z1-PM1 Temperature"
    state_address: "2/4/7"
    type: temperature

og_z2_pm1.yaml:

binary_sensor:
    - name: "OG-Z2-PM1 Presence"
      state_address: "2/5/1"
sensor:
  - name: "OG-Z2-PM1 Temperature"
    state_address: "2/5/7"
    type: temperature

After including those files, I would like the entries for each category to be added to the responding category.

Any recommendations on how to achieve this and/or other general recommendations on how to organize configuration files, specially for the KNX integration, which tends to become quite massive, since a lot of details have to be specified (addresses, state_addresses, etc.).

I opted for a pretty simply approach: One file for each KNX domain. So I have:

knx_binary_sensor.yaml
knx_climate.yaml
knx_covers.yaml
...

That way I know they only contain KNX related entries, e.g. all non KNX binary sensors are not in there, but have their own file. Pretty quick to find what you are looking for in case you need to tweak something (at least in my setup with currently 125 KNX entities).

I then include them from my main configuration file.

knx:
  binary_sensor: !include knx_binary_sensor.yaml
  climate: !include knx_climate.yaml
  cover: !include knx_cover.yaml
  event: !include knx_event.yaml
  expose: !include knx_expose.yaml
  light: !include knx_light.yaml
  sensor: !include knx_sensor.yaml
  switch: !include knx_switch.yaml
1 Like

Maybe Packages are what you are looking for. I have never tested it myself and don’t know if configuration errors are logged with eg. the proper filename when you use that.
Personally I use one big file and navigate with the editors search bar :upside_down_face:

The problem with this is that modern sensors (especially presence detectors) have very many communication objects across all domains (switch, binary_sensor, sensor, etc.). I don’t like to spread this across 3 or more different files. When I change something (which happens every now), I need to change multiple files :-(.

Haven’t tested it myself either. Anyone experience with that?

It’s mostly the same for me, but I’m beyond 1000 lines of code by now. Also entities from the same device from different domains are still split across the whole file and are hundreds of lines apart from each other. Technically it works fine, but as a human, I don’t like this, even with more advanced text editors.

I prefer for stuff that is related to be next to each other ;-).

I guess for Knx, I don’t think in devices but only in group addresses. It doesn’t matter what properties of a device I change as long as the GAs stays unchanged.
When I make changes about a GA, every occurrence of it can be easily found with search.
Ymmv

I guess for Knx, I don’t think in devices but only in group addresses. It doesn’t matter what properties of a device I change as long as the GAs stays unchanged.
When I make changes about a GA, every occurrence of it can be easily found with search.

Some devices have multiple domains that they interact with (light, switch, etc.), and when adding / installing a new device, I would rather copy a simple file and adjust the group addresses rather than copying multiple sections in a big file.

I’ve played around with the packages feature, however unsuccessfully.

  packages:
    pack1:
      light:
        - platform: knx
          name: "Licht KG-FL"
          address: "5/1/101"
          state_address: "5/1/102"
      sensor:
        - platform: knx
          name: "Licht KG-FL Betriebsstunden"
          state_address: "5/1/103"
          type: long_delta_timesec
          sync_state: expire 30
        - platform: knx
          name: "Licht KG-FL Stromwert"
          state_address: "5/1/105"
          type: current
          sync_state: expire 30
        - platform: knx
          name: "Licht KG-FL StromzÀhler"
          state_address: "5/1/106"
          type: active_energy
          sync_state: expire 30
      binary_sensor:
        - platform: knx
          name: "Licht KG-FL Lastausfall"
          state_address: "5/1/108"
          device_class: "problem"
          sync_state: expire 30
      switch:
        - platform: knx
          name: "Licht KG-FL Betriebsstunden Reset"
          address: "5/1/104"
          device_class: switch
        - platform: knx
          name: "Licht KG-FL StromzÀhler Reset"
          address: "5/1/107"
          device_class: switch

This results in the following errors:

platform: knx is not supported. Afaik you can however use multiple knx: keys with these packages.
So I think you could

# configuration.yaml
homeassistant:
  packages: !include_dir_named packages

# packages/knx_1.yaml
knx:
  light:
    - ...
  switch:
    - ...

# packages/knx_2.yaml
knx:
  light:
    - ...
  switch:
    - ...

But I have not tested this myself
 give it a try.

2 Likes

That seems indeed to work just fine.

So finally I’m able to organize my KNX the way I prefer to. Since I have ~ 100 devices and want to expose as much as possible, this makes my life considerable easier.

Thank you ;-).

1 Like

Actually it works even better than expected. I can also put some automation along into the package file.

A use case, for instance, is to reset some counters (e.g. BetriebsstundenzĂ€hler = Operating hour counter). You need to send a 1 to a group address. Unfortunately I didn’t find a better solution than to implement this is switch within Home Assistant. However the switch doesn’t have a state and so I have to reset it to off after some time (1 second in my case):

knx:
  light:
    - name: "Licht KG-FL"
      address: "5/1/101"
      state_address: "5/1/102"
  sensor:
    - name: "Licht KG-FL Betriebsstunden"
      state_address: "5/1/103"
      type: long_delta_timesec
      sync_state: expire 30
  binary_sensor:
    - name: "Licht KG-FL Lastausfall"
      state_address: "5/1/108"
      device_class: "problem"
      sync_state: expire 30
  switch:
    - name: "Licht KG-FL Betriebsstunden Reset"
      address: "5/1/104"
      device_class: switch
automation:
  - alias: reset_operating_hours_counter_kg_fl_licht
    description: "Reset operating hours counter for KG-FL Licht"
    mode: single
    trigger:
      - platform: state
        entity_id:
          - switch.licht_kg_fl_betriebsstunden_reset
        to: "on"
        for:
          seconds: 1
    condition: []
    action:
      - service: switch.turn_off
        target:
          entity_id: switch.licht_kg_fl_betriebsstunden_reset

As you can see you can also put automation in the package file, since this logically belongs to this device. Quite happy with this.

Use a button :wink:
https://www.home-assistant.io/integrations/knx/#button

I agree with @farmio

I solved it like the following with my Garagedoor, which needs two different ‘buttons’ instead of a switch on only one Groupaddress:

button:
  - name: "Garagentor schließen"
    address: "8/1/0"
    payload: 1
    payload_length: 0
  - name: "Garagentor öffnen"
    address: "8/1/0"
    payload: 0
    payload_length: 0

You could also use a cover (or template cover) for this. Maybe set assumed_state in customize. It would probably be a better fit eg. for voice assistants since you can set a proper device_class.

Sounds interesting, could you provide an example? Would be perfect if I could use my third groupadress which is ‘stop garagedoor’.
Would be perfect If i could use it like a windowcover in my UI with up/stop/down buttons

You can configure a “stop_address” for Knx covers. Use the up/down GA for “move_long_address” and set traveling times.
See KNX - Home Assistant