How can I configure many zigbee buttons in fewer scripts?

Hey Folks,

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!

image

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.

Here is my “Living Room - On” script:

alias: Button - Living Room On
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.living_room_2
            state: "on"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.living_room_2
            data:
              brightness_pct: 100
      - conditions:
          - condition: state
            entity_id: light.living_room_2
            state: "off"
        sequence:
          - service: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.living_room_2
mode: single
icon: mdi:gesture-tap-button

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.

Any feedback is much appreciated!

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 :slight_smile:

description: ""
mode: single
trigger:
  - device_id: 0c352c29b1ebbd275053cd12d48dcd75
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: remote_button_short_press
    id: "on"
  - device_id: 0c352c29b1ebbd275053cd12d48dcd75
    domain: zha
    platform: device
    type: remote_button_double_press
    subtype: remote_button_double_press
    id: "off"
condition: []
action:
  - service: light.turn_{{ trigger.id }}
    target:
      entity_id: light.bed1

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…

1 Like

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:

trigger:
  - device_id: b7888c167c39807f66b9ce4fd10ae446
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: turn_off
    id: "off"

The script condition:

  - conditions:
          - condition: trigger
            id: off

If there’s something obvious I’m doing wrong here, let me know.

@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 :slight_smile: 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!

1 Like

Try it and paste your automation here if you get stuck.

1 Like

you can do that if you want:


mode: single
trigger:
  - device_id: 0c352c29b1ebbd275053cd12d48dcd75
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: remote_button_short_press
    id: "on"
  - device_id: 0c352c29b1ebbd275053cd12d48dcd75
    domain: zha
    platform: device
    type: remote_button_double_press
    subtype: remote_button_double_press
    id: "off"
condition: []
action:
  - service: script.your_script
    data:
      which_trigger: {{ trigger.id }}

this will call your script and pass in the trigger id as {{ which_trigger }} that you can use in the script.

I will do this if I reuse the script for multiple purposes. but if it’s only to be used for this one use case, I just leave it in the automation.

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!

You could also create a single script with arguments as well.

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 :smile:

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.