Simpler way to define similar automations and reuse code?

I’m currently creating automations for buttons and the lights they control. However I’m essentially creating the same automation over and over again, just with different buttons and different light groups.

E.g.,

- id: '1580246534379'
  alias: Kitchen Button - Dim Down Pcnt
  description: ''
  trigger:
  - device_id: 13646fb4d11343b5bfbfaf777cbb9857
    domain: deconz
    platform: device
    subtype: dim_down
    type: remote_button_short_press
  condition: []
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.kitchen_lights
      brightness: '{% set bri = states.light.kitchen_lights.attributes.brightness
        | int %} {{ [bri-30, 30] | max }}'

is an automation I’ve reused multiple times.

Is there a cleaner way to reuse this code when generating automations? Or is that best practice as is?

You’ll have to switch to the other trigger type. Can’t use the ‘automation editor’ device trigger.

You could create a script, or python_script, that accepts entity_id and brightness as parameters.

The automation’s trigger can include multiple triggering sources. You can pass the trigger object to the script as a parameter.

1 Like

I see you are using deconz, what kind of remotes are you using? I wrote a small app available in HACS that allows easy configuration, smooth dimming through long press of a button and assigning button presses to service calls. The current version in HACS obly supports the Hue Dimmer Switch, however in my dev branch I have a working version that supports the Ikea Tradfri Dimmer and remote and the 2016 version of the Xiaomi switch. If you have another device, I can easily add it to the app. Please let me know in case you are interested in this.

1 Like

Would you be able to generalize that so it supports ZHA as well? I’m currently doing it through NodeRed and while it works, I’d love a native HA implementation.

Yes I could, assuming that ZHA also fires an event when a button is pressed.
Unfortunately it’s not a native HA implementation, I’m not skilled enough for this, it’s just an AppDaemon app.

The remotes I have tried (Hue dimmer, Ikea dimmer and Sylvania dimmer) all fire a zha_event. The Hue and Ikea also support a “hold” event as well. If you want, I can PM you a map of all the different responses from the various remotes.

I’d take an AppDaemon implementation as well. :smiley:

This should be relatively easy to integrate into my app. Please send me the button mappings via PM, I assume they will be the same as with deconz, but I need to check this first.

That’s awesome, I’m definitely interested in using that!

The remotes I’m using at the moment are:

I use the buttons the most as they are just so cheap and generally reliable. Is there anything I can do to help add those to the app?

Thanks!

The Tradfri 5 button remote is already supported in the dev version, for the Xiaomi one I need to know the exact model, I already integrated one but it might be a different model.

To help me integrate it, go to Developer Tools -> Events -> Enter “deconz_event” in the “Listen to events” box -> press start listening. Then press all the possible buttons short, long, multiple times. Provide me the output you see in the “listen to events” box.

Great - I didn’t have time last night, but I will try and do this evening if I can, then will send you the events!

No worries, take your time. I’m busy as well, will see if I find some time on the weekend.

OK here we go. I’ve commented the output so you can see what action I took, but summarising these were:

  • 1 x Press and release
  • 1 x press, hold, release
  • 2 x press
  • 3 x press
  • 4 x press
  • 5 x press

I’ve also checked the model number listed in deconz for the switch, and it is Xiaomi WXKG01LM

# Single press and release:

Event 1 fired 6:57 PM:
{
    "event_type": "deconz_event",
    "data": {
        "id": "study_button",
        "unique_id": "00:15:8d:00:03:3e:f7:b0",
        "event": 1002
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-30T18:57:16.151335+00:00",
    "context": {
        "id": "30426998b12142bbb7c8b06c241969f2",
        "parent_id": null,
        "user_id": null
    }
}
Event 0 fired 6:57 PM:
{
    "event_type": "deconz_event",
    "data": {
        "id": "study_button",
        "unique_id": "00:15:8d:00:03:3e:f7:b0",
        "event": 1000
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-30T18:57:15.944754+00:00",
    "context": {
        "id": "953a3a4cd7c740bd98893ce51beb646e",
        "parent_id": null,
        "user_id": null
    }
}


# Single press, hold and release

Event 2 fired 6:57 PM:
{
    "event_type": "deconz_event",
    "data": {
        "id": "study_button",
        "unique_id": "00:15:8d:00:03:3e:f7:b0",
        "event": 1003
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-30T18:57:57.352069+00:00",
    "context": {
        "id": "aa7d2459cd72433d9d71be89d53e056a",
        "parent_id": null,
        "user_id": null
    }
}
Event 1 fired 6:57 PM:
{
    "event_type": "deconz_event",
    "data": {
        "id": "study_button",
        "unique_id": "00:15:8d:00:03:3e:f7:b0",
        "event": 1001
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-30T18:57:54.090486+00:00",
    "context": {
        "id": "4463623904d2488b94bd9f1b38255727",
        "parent_id": null,
        "user_id": null
    }
}
Event 0 fired 6:57 PM:
{
    "event_type": "deconz_event",
    "data": {
        "id": "study_button",
        "unique_id": "00:15:8d:00:03:3e:f7:b0",
        "event": 1000
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-30T18:57:53.565833+00:00",
    "context": {
        "id": "c58ce673d7ed43ceac7c60845351a28e",
        "parent_id": null,
        "user_id": null
    }
}

# 2 X press

Event 0 fired 7:00 PM:
{
    "event_type": "deconz_event",
    "data": {
        "id": "study_button",
        "unique_id": "00:15:8d:00:03:3e:f7:b0",
        "event": 1004
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-30T19:00:17.980291+00:00",
    "context": {
        "id": "ae42649fc0204d498e2ac4461185e8ae",
        "parent_id": null,
        "user_id": null
    }
}

# 3 X press

Event 0 fired 7:00 PM:
{
    "event_type": "deconz_event",
    "data": {
        "id": "study_button",
        "unique_id": "00:15:8d:00:03:3e:f7:b0",
        "event": 1005
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-30T19:00:30.207910+00:00",
    "context": {
        "id": "a34e635853d64cc7ab4296585b821256",
        "parent_id": null,
        "user_id": null
    }
}

# 4 X press

Event 0 fired 7:00 PM:
{
    "event_type": "deconz_event",
    "data": {
        "id": "study_button",
        "unique_id": "00:15:8d:00:03:3e:f7:b0",
        "event": 1006
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-30T19:00:36.582055+00:00",
    "context": {
        "id": "e4b439dc41624e83bf554f84bfdd8b61",
        "parent_id": null,
        "user_id": null
    }
}


# 5 X press

Event 0 fired 7:00 PM:
{
    "event_type": "deconz_event",
    "data": {
        "id": "study_button",
        "unique_id": "00:15:8d:00:03:3e:f7:b0",
        "event": 1010
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-30T19:00:46.103412+00:00",
    "context": {
        "id": "19310dd1fe6143229224c482b6ef60a7",
        "parent_id": null,
        "user_id": null
    }
}

Do you need anything else?
THanks!

I see two release events with the same event (1000) but the press events all have different events.

How does one interpret this data to determine when a press event occurred?

1000 is fired at the beginning of a single press - either short or long.

The release event is:
1002 - quick release
1003 - long release

I think 1001 is fired during the hold.

I.e., for short press and release, the events are:
1000 - press
1002 - release

for long press and release, the events are:
1000 - press
1001 - hold
1003 - release

FYI - note I copied the events directly from the log, so the most recent event (i.e., the release) is listed first

Thanks, that makes sense. I didn’t know it identified two kinds of press events.

I also now realize that in your first example, the events are in reverse-chronological order.

No that’s all I need. I have something you can test. Did you already install AppDaemon?

Yeah, I’ve got AppDaemon installed, I’m hoping the configuration is correct…

Ok founds logs, it should be working