How do I use Aqara events reported by deCONZ in a template condition for light automation?

Hi everyone!

Almost a brand new user of HA here and I’ve absolutely loved setting my home up the past week.
I use a Raspberry Pi 3B booting from an external SSD together with a bluetooth adapter, an Aoetec Z-stick Gen5+, a Conbee II. So far I have about 7 different environment sensors, a few smart plugs and a very low number of Flair ViYu RGB LED lamps. All devices are running zigbee and the majority of the equipment I have is Aqara used via the deCONZ-addon. Everything has been working really good so far and I’ve had zero hardware issues or problems connecting new stuff. All devices has been added through the Phoscon web-gui and I’ve just started deCONZ to look at it but haven’t touched a thing there.

But, what I’m trying to accomplish is controlling a RGB-LED via a 2-button rocker Aqara switch (model:.WXKG02LM) in the following way
Short press Left button = on
Short press Right button = off
Long press Left button = start slow (30s) transition from current state to 1% brightness (dim down) (done via brightness_pct and transition)
Long press Right button = start slow (30s) transition from current state to 95% brightness (“dim up”)
So far so good, that’s working a-ok already.

What I do want to implement is that after doing a long press on either button I want to be able to short press either button to stop the dimming no matter if it’s getting brighter or darker and here is where I seem to have gotten completely stuck.
My idea was to try to read from the system when the last long press was made on a certain switch and if that was less than, let’s say, 5 seconds when doing the next short press then the brightness transition would halt.
I found quite a bit of information of that “deconz_event” should provide that info. After going into the DevTools in HA and setting up a listener for the same and I instantly saw the reported info when pressing the button and I was pleased to see the following:

Event 28 fired 7:09 AM:

{
    "event_type": "deconz_event",
    "data": {
        "id": "living_room_switch",
        "unique_id": "00:15:8d:00:02:83:dd:6b",
        "event": 2002,
        "device_id": "5f2374ebe26a47e9f2d8881331d163db"
    },
    "origin": "LOCAL",
    "time_fired": "2021-02-20T06:09:53.937958+00:00",
    "context": {
        "id": "d4c610fdea1b537a971ec6a2981b6e09",
        "parent_id": null,
        "user_id": null
    }
}

After many hours of trying and testing and googling, I realize I have no idea how to via a template grab “time_fired” and use that in the templates’ condition. Everything I’ve tried just reports “None” or “unknown” and after about 6 hours straight I’d thought I come here to ask people with more experience :slight_smile:

I’ve tried so many iterations I’m just assuming it’s gotten worse and worse after each try, but what I was hoping to do is something similar to this as a secondary condition on the automation along with a simple check if the light is turned on as the first condition:

condition: template
value_template: >
  {{ as_timestamp(now()) -
  as_timestamp(state_attr('deconz_event.living_room_switch', 'time_fired')) |
  int(0) > 5 }}

I can’t take any credit for this attempt, it’s mostly bits and pieces from other threads here on the forums. Even if I roughly understand what it’s supposed to be doing I’m quite unsure on the syntax and primarily where to collect the state from if that’s even the correct way of doing it.
Am I going about this all wrong? Am I close and just need a nudge in the right direction?

Any help would be appreciated and I hope my first post is informative enough. I think I covered most things but if I can provide more info to make it easier to help, please let me know.

Thanks!

Have you compared what you’ve done so far to the integration examples? https://www.home-assistant.io/integrations/deconz/

Hi and thanks for the very fast reply!

I have read through that page more times than I’d like to admit but I just don’t understand how to translate that into something hands-on so to speak. I think I’m also confusing myself with the minor information overload trying to get started and most things donein a perhaps a bit to quick tempo so I apologize if I’m regurgitating a topic that’s been brought up before.

Does the article mean with the text under the section “Remote Control Devices” that the only information I can use is the entity ID and the event and not time_fired?

If so, would you reckon a timer would be a decent “workaround” and check states of that instead?

Thanks again!

I don’t think you can utilize the time fired data. But that information can’t be of interest to you since it will more or less be “now” and aimed for the logbook.

Fair enough! Thanks for the input.

I actually went ahead with trying a timer by controlling and checking that timers state in a few automations and without much logic testing or adding more sensors to the rule (motion sensor) everything seems to be just working like I was hoping it would. This needs further testing of course but so far so good.

As soon as I have more actual lights to control I have some more work to do before pasting any more configs in here but if anyone in the future reads this and I forgot to add my first non-lab setup config, feel free to reply in this thread and I’ll do it as quickly as I can :slight_smile:

@Robban thanks for the input and lending me your ears for a moment, much appreciated!

1 Like

Hey
I was in need of the same and was able to fix it as follows, in case somebody needs it.
Background / use case: extremely seldom, deConz lights turn on all of a sudden, there is no light-switch or user that triggered it (also, no autmation or script). It happened recently in the bedrom, so I was in need of a fix.

Here’s how:

  1. Since one cannot utilise the time_fired of a deconz_event, I need something to pass the time of when the light-switch has been pressed. To do so, create an input_button: input_button.light_bedroom

  2. Pass the deconz_event of pressing the button to this very input_button. The event 1000, 2000, and 5000 are the events that are fired when I press one of the three upper buttons of my deConz light-switch.

alias: Fix - Light Bedroom - when triggered last time by deConz switch
description: >-
trigger:
  - platform: event
    event_type: deconz_event
    event_data:
      id: bedroom
      event: 1000
  - platform: event
    event_type: deconz_event
    event_data:
      id: bedroom
      event: 3000
  - platform: event
    event_type: deconz_event
    event_data:
      id: bedroom
      event: 5000
condition: []
action:
  - service: input_button.press
    data: {}
    target:
      entity_id: input_button.light_bedroom
mode: single
  1. Turn off the light if it has not been turned on by either a light-switch or user (App)
    Template 1 ensures that no user has turned on the light via the App
    Template 2 ensures that no light-switch in this room has been pressed/turned-on in the last 5 seconds. This is represented by the input_button.light_bedroom which receives all turn-on events of the deConz light-switch.
alias: Fix - Light Bedroom turn off
description: ''
mode: single
trigger:
  - platform: device
    type: turned_on
    device_id: xyz
    entity_id: light.bedroom
    domain: light
condition:
  - condition: template
    value_template: '{{trigger.to_state.context.user_id == none}}'
  - condition: template
    value_template: >-
      {{ (as_timestamp(now()) -
      as_timestamp(states.input_button.light_bedroom.last_updated)) > 5 }}
action:
  - type: turn_off
    device_id: xyz
    entity_id: light.bedroom
    domain: light

Hope that helps.
Cheers

1 Like