Hey all,
I have what seems to be a fairly simple use case…
I have a Xiaomi button switch that, when pressed, with either tun a light off if the light was on OR will turn a light on if the light was off.
I’ve read through a number of posts that seem to be similar in use case, some of which have used service templates / switch template and others that suggest using scripts, but for the life of me in multiple iterations of configuration have not been able to get anything to work.
Is there an easier way to accomplish this? Am I missing something easy?
I’m a little confused by what you’re trying to do, but what you have can be significantly simplified. Unless, of course, you are trying to do more that’s not obvious (to me) so far.
Yep, do what @pnbruckner suggested, just use a toggle service. It’s worth noting that you can also use the switch.toggle service for a group of lights. My script below:
You have an if statement testing a condition, but the end result will always be “group.ryans_office_lights”. You might also be able to refactor your service template.
You could also create two separate automations with a condition, one for on and one for off. The code will be cleaner because you won’t need to use a template, but the drawback is you have created an additional automation.
Ok, with the additional explanation and the code you wrote I think it’s now clear what you want to do. I.e., when the click happens, turn all the lights in the group on if the overhead lights are off, and turn all the lights in the group off if the overhead lights are on. Is that it? If so, then what you have will work, but it can be simplified a little:
Regarding the toggle service, yes, it will toggle each entity, whether you list it individually or it’s in a group. So all that were off will go on, and all that are on will go off. Now that I understand (I think) what you’re trying to do, that is not the right service to use in this case.
@BrendanMoran, yes, the service template could be re-factored like you say, but I think what you wrote is the reverse of what the OP is looking for.
This is fantastic, thank you @pnbruckner - thanks to this code I’ve got my basement lights group working almost 100% as I want them to. Quick question (and it REALLY doesn’t matter if that answer isn’t a simple one) - is it possible to instead of