String interpolation for sequence

Here is the beginning of the automation for my scene switches.

You effectively get three variables:

  • switch_name - typically I map all switches in a room to the same name so all switches in a given room work the same.
  • press_type - single, double or hold
  • button_number - I remap to zero based so I can access an array of actions (remove the -1 if you don’t want that).

You can add as many triggers as you want to the automation (I currently have 6).

alias: MQTT Button Press
mode: parallel
description: ""
triggers:
  - trigger: mqtt
    options:
      topic: zigbee2mqtt/Living Entry Scene/action
  - trigger: mqtt
    options:
      topic: zigbee2mqtt/Kitchen Entry Scene/action

variables:
  switch_map:
    living_entry_scene: living
    kitchen_entry_scene: kitchen

  topic_name: "{{ trigger.topic.split('/')[1] | lower | replace(' ', '_') }}"
  button_split: "{{ trigger.payload.split('_') }}"

  switch_name: "{{ switch_map.get(topic_name, 'unknown') }}"
  press_type: "{{ button_split[1] }}"
  button_number: "{{ button_split[0] | int(0) - 1 }}"

conditions:
  - condition: template
    value_template: "{{ switch_name != 'unknown' }}"

actions: 
   ......

I standardize my buttons so the first button, controls the lights in the room the switch is in and button 4 does the same function on all switches in the home, but what actions you put in the script is up to you.