Fairly new HA user here. It’s been a great experience so far and between the documentation and forum search I’ve been able to solve every stumbling block I’ve come across.
I was working on some simple tap button automations last night and got to wondering how a more seasoned user would build the automation. I got it working and am happy with the functionality but I’m sure there are techniques/tricks that I’m just not aware of yet.
Take this automation for example. A Hue Tap Switch with four buttons launching 3 lighting scenes (via scripts) and an Off state.
This is what I came up with, but what might be a cleaner/more advanced approach?
I’m not as good as some of hte others here, but your way works. Personally i’d make it more generic and simply add triggers maybe for each and call the associated script. Then I can expand this to more buttons.
If you make a script called script.unified_living_room_off, the entire choose can be replaced by a single service call employing a template with a dictionary.
You can dispense with the dictionary entirely if you put the desired script’s name in the trigger’s id. So instead of naming it 3 call it dimmed. The template is reduced to:
You can also replace the four Device Triggers with a single Event Trigger. Instead of using trigger.id you would reference the subtype. Let me know if that interests you.
If you remove the line containing id from the Event Trigger, then it no longer restricts itself to just one specific Hue Dimmer Switch but from all of them (assuming you have more than one).
EDIT
The int(0) filter isn’t necessary if trigger.event.data.subtype is already an integer value.
EDIT 2
Use more legible id instead of long-winded device_id.
Now you just need to name hte scripts with the device names and what you want them to do. You can keep the ‘1/2/3/4’ more generic and name your scripts like this as well:
Although streamlining automations can become a fun brain exercise, the deeper question is why have you chosen to create separate scripts for each button-press?
For example, will script.unified_living_room_bright be called by anything other than pressing button #2 on one specific Hue Dimmer Switch?
If it won’t then there are better ways of designing this thing.
As it stands there are two things that could call that script right now: the physical tap switch and a button exposed in Homekit.
In that room there are multiple different sets of lights from multiple lighting vendors each with their own Bright or Dimmed scenes etc. The idea of the Unified script is to have one place where I can tweak what is triggered anytime you want the room to be in the Bright state. I had initially tried to make a scene that contained scenes within it but quickly learned nested scenes are not supported hence the switch to scripts.
Even typing this out I can see how I might be adding extra effort that may not be needed and I could probably build all of this into one automation with multiple triggers…
Would love to hear your thoughts and any suggestions. I appreciate your time and insights, truly golden.