Automation trigger based on custom event in Home Assistant

I have a XComfort system that I have configured to send sensor event into Home Assistant as custom event using the API. The custom event is named XComfort and I send in the JSON payload as shown below. I want to create a automation that trigger when a custom event is sent, but ONLY when a given payload is sent. E.g I want the trigger to happen when both “deviceNumber” = “14” and “messageType” = “binary_switch_on”. I can’t get this to work as my trigger fire regardless of the payload. I have tried this (see trigger below), but it does not work as ignores the event_data. Does anyone have any tips?

  trigger:
     platform: event
     event_type: XComfort
     event_data:
        deviceNumber: '14'
        messageType: binary_switch_on

JSON event payload:
{
“deviceNumber”: “14”,
“deviceType”: {
“id”: 29,
“name”: “Motion Detector”,
“OrderCode”: “CBMA- 02/01”
},
“messageType”: “binary_switch_on”,
“dataType”: “NO_DATA”,
“status”: “unknown: 1”,
“data”: “0,0,0,0”,
“signalStrength”: “good”,
“battery”: “good”
}

For this type of trigger, what you specify for event_data does not actually have to exist in the received event data (even though the docs would seem to imply they must.) Rather, it specifies what the values must be if the corresponding keys do exist. So, for your trigger, it’s basically saying if deviceNumber exists it must be ‘14’, but if it doesn’t exist, that’s ok. Same with messageType.

So, what you need to do is to move those parameters to the automation condition section, like this:

    trigger:
      platform: event
      event_type: XComfort
    condition:
      condition: template
      value_template: >
        {{ trigger.event.data.deviceNumber == '14' and
           trigger.event.data.messageType == 'binary_switch_on' }}

If deviceNumber and/or messageType are not in the received event data, this will not cause an error, but will just evaluate to False so the automation action(s) will not run. For the action(s) to run, both must exist, and both must matched the values specified.

1 Like

Excellent…that works great!
Thanx!

1 Like

Hi Iidvar.
I’m a bit curious. Have you managed to integrate xcomfort in ha? Can you please tell me how you have done this? In my house, all the lights and roomcontroll is xcomfort. I’ve been looking for a ha integration but i haven’t got any yet.
What do you use?

Hi Andre,
Yes, I have. I have built a nodeJS API that use the node-hid to communicate with the XComfort USB device. The API translate the binary codes received and translate them into JSON and send them to the HA event stream. I then pick up the events in HA automations. The API expose also endpoints to set dimmer and switch actuators values which is called from HA based on interaction through the UI (lovelace). The nodeJS API is implemented on its own Raspberry Pi (not the same as HA is running on).

At some point I might share my code on GIT, but it is not yet completed as not all events have been decoded 100%…, but it works for my purpose right now.

It would be great if someone took the job and created a HA addon for XComfort :slight_smile:

Hi Lidvar !
Have you done some more that you can share ?
I see there are a few other that has shared some at GIT.
I have a X-Comfort system and are thinking to include that in Home Assistant.

Hi Niels (Jeg tipper du er Norsk?)
Have you been able to successfuly integrate xComfort into HA ?