Problems with a template for a Hue, ABB Smart Switch

I have bought new wall mounted switches to control my Hue Lights.

The switch is setup as a friend of hue switch/ABB Smart Switch but the lack of options in the Hue App is embarrassing (why cant I increase/decrease brightness…) So all four buttons is setup with automations and scripts in Home Assistant. Basically I have just created 20 script and 4 automations.

I have created 4 (light on, off, increated, decreased) x 5 (rooms: stue, kontor, peter, nanna, entre) scripts. And then I have created 4 automations (light on, off, increated, decreased)

This is what one of them look like:

alias: Hue Switch light on
description: ''
trigger:
  - platform: event
    event_type: hue_event
condition:
  - condition: template
    value_template: '{{ trigger.event.data.type == "short_release" }}'
  - condition: template
    value_template: '{{ trigger.event.data.subtype == 1 }}' #subtype is the buttons 1, 2, 3 or 4
  - condition: template
    value_template: '{{ trigger.event.data.device_id in light_switches }}'
action:
  - variables:
      light_switch_entity_id: '{{ light_switches[trigger.event.data.device_id] }}'
  - service: '{{ light_switch_entity_id }}'
mode: single
variables:
  light_switches:
    9a94616864f49e45020f6c2e091553c2: script.on_light_kontor
    e4f0be046ce469afc0bb35123aee677e: script.on_light_stue
    5fe64eb422eded4d7f776f2cbc23ff6b: script.on_light_kontor
    80f424111bd7e3e8dd2de64d0a3a4f49: script.on_light_peter
    c82c8d144c0ad1700979090ba62b6f73: script.on_light_nanna
    9cbc810c6ae937df49354abd45a10bdc: script.on_light_entre

The automations works. But I would like to have just one automation instead of one for turning on the lights, one for turning off, one for increasing light and one for decreasing light.

I was pretty sure I could just use the same kind of templating as with the light_switches and just concatenate half of the name of the name of the script from subtype_list and the other half from light_switches - like I have shown here. But for some reason it doesnt work.

alias: Hue Switch Short Release
description: ''
trigger:
  - platform: event
    event_type: hue_event
condition:
  - condition: template
    value_template: '{{ trigger.event.data.type == "short_release" }}'
  - condition: template
    value_template: '{{ trigger.event.data.subtype in subtype_list }}'
  - condition: template
    value_template: '{{ trigger.event.data.device_id in light_switches }}'
action:
  - variables:
      light_switch_entity_id: '{{ light_switches[trigger.event.data.device_id] }}'
      light_switch_subtype: '{{ subtype_list[trigger.event.data.subtype] }}'
      light_switch_script: '{{ light_switch_subtype }}{{ light_switch_entity_id }}'
  - service: '{{ light_switch_script }}'
mode: single
variables:
  light_switches:
    9a94616864f49e45020f6c2e091553c2: light_kontor
    e4f0be046ce469afc0bb35123aee677e: light_stue
    5fe64eb422eded4d7f776f2cbc23ff6b: light_kontor
    80f424111bd7e3e8dd2de64d0a3a4f49: light_peter
    c82c8d144c0ad1700979090ba62b6f73: light_nanna
    9cbc810c6ae937df49354abd45a10bdc: light_entre
  subtype_list:
    1: script.on_
    2: script.off_
    3: script.increase_
    4: script.dim_

It looks like this in Developer Tools when I press button 4.

    "event_type": "hue_event",
    "data": {
        "id": "kontor_hue_switch_button",
        "device_id": "5fe64eb422eded4d7f776f2cbc23ff6b",
        "unique_id": "114fffea-f23d-4735-9305-f0b6ec28fd3e",
        "type": "short_release",
        "subtype": 4

What am I doing wrong? (and if anyone can think of an easier way than having 20 scripts, let me know)

There are blueprints for similar devices. I suggest you review what they do to solve the same problem.

Basically they listen to every event the device produces (just like you do with an Event Trigger that listens for hue_event) but they move the decision-making out of condition and into action.

In action, a choose determines what to do based on which device caused the trigger and which event it produced.

Some of the blueprints might be a bit more complicated than you require because they are designed to handled event_types produced by either Zigbee2MQTT or ZHA plus their different subtypes. Your case is simplified because you only need one event_type, namely hue_event.

Here’s a blueprint you can mine for ideas:

Let me know if you need more help.