I just bought a bunch of Ikea Zigbee buttons to control my lights and scenes. They were super cheap and convenient, and my wife will love to not have to open her phone for lights! I know many people solve this problem with automations using presence-detecting sensors, but I really prefer the manual button method.
My issue though, is that I have a hard time configuring the buttons without creating an automation and associated script for each one. I am going to have a big list of almost identical scripts!
I am using scripts because I want to do more than just toggle the light. I want to be able to dim them as well.
I’d like the script to be able to control multiple buttons, and choose the light to control based on the button. That way I could use one script for all basic buttons instead of 2 scripts for each button.
Maybe multiple scripts is fine, since I’m sure many of you probably have hundreds of scripts and automations.
Have you tried an automation with a trigger for each button? If you give each trigger an ID you can then have a “choose” action based on the button that has been pressed.
With buttons I find it easier to use “toggle” services rather than “on” and “off”.
something like this? just as an example for the approach, I did press always to turn it on, and double press to turn it off… remap to what you want vs my stupid example
this is how you can reduce the number of automations. you said you want to limit the number of scripts… I can send how to share scripts as well, however I don’t understand your statement that you want to use scripts because you want to do more than turn on/off the lights… that you want to dim. why can’t you do that in the automation? all the calls you do in a script, you can do in an automation.
could you clarify? if you, for some reason, need to have them in a consolidated script, we can do that too using script fields. happy to show you how if you need…
Like @Stiltjack said, triggers and chose actions are great, here’s an example I use in my wardrobe (contact sensor that triggers the light on and off).
alias: your alias
description: ""
trigger:
- platform: state
entity_id:
- binary_sensor.your_sensor
to: "on"
id: open
from: "off"
- platform: state
entity_id:
- binary_sensor.your_sensor
to: "off"
id: closed
from: "on"
condition: []
action:
- choose:
- conditions:
- condition: trigger
id:
- open
sequence:
- service: light.turn_on
data:
brightness_pct: 100
target:
entity_id: light.your_light
- conditions:
- condition: trigger
id:
- closed
sequence:
- service: light.turn_off
data: {}
target:
entity_id: light.your_light
mode: single
I love this idea. I actually tried something similar this morning, but I had a hard time with getting the script to recognize the triggerId.
The automation trigger:
@fleskefjes Thanks for your example! Is this a standalone trigger inside the script? Is there any downside to doing this over using an automation trigger to trigger the script? Because if there’s no downside, I think this would further simplify my config!
The example is a plain automation, not using scripts, but I don’t see why you couldn’t call a script instead of a a service. Give it a try I would think you put the conditions in the automation though, not in the script.
Ok this might be where I’m getting tripped up. I’m trying to use the automation to trigger a script, then reference the trigger from within the script.
Sounds like I should just be doing the whole thing within the automation like you suggest. I will try that!
This is exactly what I was trying to do. I didn’t realize I had to pass the trigger id through. But I think you’re both right that I can do everything I need to from right within the automation. Thank you!
Thank you everyone for your replies. After some time attempting to write the script, I realized that my button behaviours were slightly different for each, so they were better off in their own dedicated automations.
I think now that I’m addicted to Home Assistant I need to get used to having lots and lots of automations in my list
if you have 2 scripts that you feel like really should be combined, post them and I or someone else will surely give it a shot or tell you we think it should stay separate.