KNX binary sensor (mass) evaluation

I have a setup with multiple KNX room thermostats and correspondingly electrical heaters attached to Shelly switches. Thermostats each send a binary object to control heating, thus I created automations to switch on and off respective Shellys, with e. g. triggers

domain: knx
type: telegram
trigger: device
destination:
  - 1/2/1

and

condition: state
entity_id: binary_sensor.knx_interface_7
state:
  - "on"

This works nicely to switch on the respective heater. Unfortunately, the other way with state == off is never executed, thus I’d like to understand how to create a proper trigger to switch off again.

Furthermore, since I have a bunch of heaters / thermostats, it would be great to kind of transfer the status of the KNX binary object onto the respective switch, without creating individual on / off automations. I strongly assume this works with templates / scripts, but so far no clue on how to tackle this.

Thx
Andreas

Hi :wave:!
You can do this in various ways. One would be to use trigger ids and choose, another would be to use templates. This would look roughly like

action: |
  {% if trigger.payload %}
    switch.turn_on
  {% else %}
    switch.turn_off
  {% endif %}

where trigger is the knx telegram trigger and trigger.payload is the telegrams raw payload (0 or 1 for DPT1)

Hi Matthias,

thanks for the explanation. Overall I got the idea to use the (Jinja) templates. But I have problems to understand the details, which code I need to include in the state box of the binary sensor template, assuming my (simplified) automation so far looks like this:

- id: Heizung ein
  triggers:
  - domain: knx
    device_id: e59a...
    type: telegram
    trigger: device
    destination:
    - 1/2/1
  conditions:
  - condition: state
    entity_id: binary_sensor.heizung_buro_eg
    state:
    - 'on'
  actions:
  - action: switch.turn_on
    target:
      entity_id: switch.heizung_buro_eg

Can you give me few more hints what the template should look like in this case?

Furthermore I’m not sure if the template should include the KNX trigger at all or if I should rather have a single heating automation catching the respective KNX group trigger and subsequently executing a bunch of template to just map known conditions to specific actions?

Thx
Andreas

Not sure what the condition is about… is this a knx entity? If so, you could just use that as trigger (state_change), not the knx telegram thing.

Initially I tried to trigger right on the binary state change of the KNX entity, but this didn’t work. Hence I’m now triggering with the KNX entity itself and afterwards evaluating the state of entity, with separate rules for on and off. Now I “templated” the action part like this, but likely made a mistake, since the Shelly output doesn’t change.

  actions:
  - action: |
      {% if binary_sensor.heizung_buro_eg %}
        switch.heizung_buro_eg.turn_on
      {% else %}
        switch.heizung_buro_eg.turn_off
      {% endif %}
  mode: single

Assuming I would include multiple templates for the different mapping, I also need to replace / change the “mode: single”, or?

That’s not how actions are called. Have a read at the docs https://www.home-assistant.io/docs/automation/action/

Ok, what I understand from automation actions I should do it like this, but it still doesn’t change the relay state. What is still missing / incorrect there?

actions:
  - action: |
      {% if binary_sensor.heizung_buro_eg -%}
        switch.turn_on
      {%- else -%}
        switch.turn_off
      {%- endif %}
    target:
      entity_id: switch.heizung_buro_eg

This doesn’t check or compare the state of that entity id.
You’ll probably want states("binary_sensor...") == "on" or something. Try it in developer tools.

Or use two triggers and assign each a different trigger id. Then use a choose block to branch out for on / off. You wouldn’t need templates that way.

Thanks a lot! After all it’s working nicely also with multiple actions to a single KNX trigger. I think this is the more convenient way, since I just need to copy & past respective templates now.