Using events to trigger actions

I am using KNX, which can fire events when something happen, for some things i want to use events instead of state changes to trigger an automation on button-press. I put the log on debug ant it looks like this:

2019-02-11 23:25:16 DEBUG (MainThread) [xknx.telegram] <Telegram group_address=“GroupAddress(“12/3/1”)”, payload="<DPTBinary value=“1” />" telegramtype=“TelegramType.GROUP_WRITE” direction=“TelegramDirection.INCOMING” />

But how can i use events to trigger the action?

You need to see what the HA event looks like. Just after that message you should see one that looks something like this:

2019-02-16 08:29:32 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event ...

If you can find that and post it here, I can help you create a corresponding automation trigger.

Thank you, that is very helpful. if i understand the log right, this is the event created immidiatly afterwards:

2019-02-16 15:38:41 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event knx_event[L]: address=7/3/1, data=1>

Well, the details seem slightly different (i.e., the address in your first post is “12/3/1”, whereas in your second post it’s “7/3/1”, so maybe this line was from a previous event.) Also, it’s always helpful when posting YAML code, log messages, etc. to format the text properly following the instructions at the top of the page.

But I think you want something like this:

trigger:
  platform: event
  event_type: knx_event
  event_data:
    address: '12/3/1'
3 Likes

Yeah, I had to change the logging to capture the “proper event” so it is a different event. Sorry for the confusion. The data is also important (due to how knx works, this is a lightswitch, so 1 is on, and 0 is off), would this work for it to only trigger when data=1?

trigger:
   platform: event
   event_type: knx_event
   event_data:
      address: '12/3/1'
      data: '1'

Yes, that would work.

It’s important to understand, anything you put under event_data: is not required to actually be in the event. But if a key (like address or data in this case) is in the event data, then it must match the value you provide in the trigger under event_data: (if you provide one.)

That’s probably confusing. Here’s some examples given the way you wrote the trigger. The following events will cause the trigger to fire:

address=12/3/1, data=1
address=12/3/1
data=1
address=12/3/1, data=1, blah=yuk
yuk=blah

But these would not:

address=12/3/1, data=0
address=7/3/1, data=1
address=7/3/1
data=hmm

If you want to require certain key/values in the event data, then use a condition:

trigger:
  platform: event
  event_type: knx_event
condition:
  condition: template
  value_template: >
    {{ trigger.event.data.address == '12/3/1' and
       trigger.event.data.data == '1' }}

If knx_event type events always have address and data, then the simpler trigger should work.

5 Likes

BTW, I should also mention, it’s not 100% clear if the data is a string ('1') or a number (1), so you may have to try both:

event_data:
  data: '1'

or

event_data:
  data: 1
1 Like

I tried it out know, and it works perfectly, thanks a lot for the help (I used “data” as a number, KNX just sends a single bit, where 1 is on, and 0 is off, for that kind of switch)

1 Like

It’s a week I’m killing myself over understanding how to trigger an action when the value of a certain group does not change, but it’s sent.

My case is something like this: I have a “switch everything off” key, that sends a value “0” on address 3/6/16. The problem is that 3/6/16 is always off, otherwise I would have everything on! I just needed to trigger an action in HA to turn off also the music when that switch is pressed, but there is never a value that changes!

This solved my problem: THANK YOU! It should absolutely be present in the documentation!!!

2 Likes

I have almost the same issue, working with the knx scene sensor.
Trying to fire automations on receiving knx scenes wont work proper. If I am using a sensor state trigger for my automation, it is only fired once, the knx scene will arrive the first time. If you send the knx scene a second time, nothing will happen, because the state of the knx scene sensor wont change :-/.
I got no Bus:Handling event “knx_event”. I only get:

2020-04-02 23:37:59 DEBUG (MainThread) [xknx.telegram] <Telegram group_address=“GroupAddress(“0/4/0”)”, payload=“” telegramtype=“TelegramType.GROUP_WRITE” direction=“TelegramDirection.OUTGOING” />

but no knx_event.
Do you also have these behaviors and solved them?

My fault, fire_event has to be enabled in knx configuration.
But unfortunately, my automation wont fire with given conditions.
Have to dig a little more.

I also had trouble with automation not firing on a scene being sent on a specific GA. This specific scene was the “all off”. In the end I had a good look at the data again, with the following coming out of the event listener in HA:

{
    "event_type": "knx_event",
    "data": {
        "event_type": "knx_event",
        "data": {
            "address": "31/1/1",
            "data": [
                0
            ]
        }
    },
...
}

I tried catching this event with:

  - alias: Hue spots ON
    trigger:
      platform: event
      event_type: knx_event
      event_data:
        address: "31/1/1"
        data: 0

Which did not work. I then noticed that the actual scene number is sent as [ 0 ] (array) instead of 0 (scalar). Next thing I tried was:

  - alias: Hue spots ON
    trigger:
      platform: event
      event_type: knx_event
      event_data:
        address: "31/1/1"
        data: 
        - 0

This was also not working. I finally settled on the following, which does seem to work:

    trigger:
      platform: event
      event_type: knx_event
    condition:
      condition: template
      value_template: >
        {{ trigger.event.data.address == '31/1/1' and
           trigger.event.data.data[0] == 0 }}
1 Like