Button to toggle between two scripts configuration help

Hi all,

I recently installed a zigbee 2 way switch to power my chicken coop door. I have two scripts, one for opening and one for closing. I am trying to create a button on the dashboard that will trigger the script.open_coop_door when pressed and change the color of the icon. And then when that button is pressed again, trigger the script.close_coop_door and set to a specific icon color.

I’ve seen some people talk about templates but I don’t know if that’s what would work for me. Any help is appreciated!

You could make a toggle helper and when you push the button have it change the state of the helper to on or off, then base your automation on doing the correct thing depending on that state.

So I started following @smarthomejunkie video on buttons and automations but only part of the video works since HA changed the automation breakdown. I did create a toggle helper labels coop door but am not sure about the automation setup with the newer stuff or what to add on the button.

Make an button card and have it run an automation or a script with what you want it to do. Use a choose action and have it check the state of the helper then if it is off it will run that section of the automation if it is on it will run the other part. Hope that makes sense

It does but I’m also a visual person so I am gonna have to sit and go through it slowly lol

If I get some time later I can post some screenshots of what I’m talking about.

Id appreciate it but understand if you cant

which switch do you have? doesn’t the switch already have an on/off state?

if so, you can simple create a card and point it at the zigbee switch. and have the automation trigger off when the switch turns on and off.

am i missing something?

https://www.amazon.com/dp/B0B979N75C?ref=ppx_yo2ov_dt_b_product_details&th=1
It’s a 2 channel switch. I have running a linear actuator so one switch extends the actuator, the second switch retracts so it’s technically two independent switches and thereby have to have two separate scripts. One to extend, one to retract.

ah, ok. So it’s two different entities. Not a single switch as far as ha is concerned.

Create an input_boolean. Just one automation when it changes. If it turns true trigger one of your switches. If false trigger the other.

The button card will flip the input_boolean for you.

This is what the template switch is for.

Use it to combine the two entities into a switch.

If you share your entities I can help you with the config.

Hey thanks! The button will just be called Coop Door.
The open entity is script.open_coop_door
The clown entity is script.close_coop_door

Here is what I was talking about with the choose option. Create a single script not two and have it choose.

In this example it would look at the toggle for my dremview and turn a light on or off. You can have it look at any state and run whatever actions you want. Then set your button to run the one script on every press.

At the end of that script have it toggle your helper so that the next button press will do the alternate option.

wait… this may be way simplier than we’re putzing about. (and if so, it’s a lesson to descript the whole situation from the start)

what does your script.open_coop_door and script.close_coop_door actually do?

does open_coop_door just turn on switch1 and turn off switch2? (or vice versa) and does close_coop_door do the exact opposite?

if so it’s easier. just do this… forget your scripts.
add 1 automation for switch1. have it make switch2 always be in the opposite state.

description: ""
trigger:
  - platform: state
    entity_id:
      - switch.zigbee_switch_1
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: switch.zigbee_switch_1
            state: "on"
        sequence:
          - service: switch.turn_off
            entity_id: switch.zigbee_switch_2
      - conditions:
          - condition: state
            entity_id: switch.zigbee_switch_1
            state: "off"
        sequence:
          - service: switch.turn_on
            entity_id: switch.zigbee_switch_2

then create your lovelace button card pointing to just switch.zigbee_switch_1

you can also of course use tom_l’s suggestion of a template switch which is a bit more elegant perhaps. but i think this works and is simple if my assumption above is right.

So script.open_coop_door triggers one independent switch to turn on (retracting the linear actuator) and then turn off 20 seconds later. script.close_coop_door triggers another independent switch to turn on (extending the linear actuator) and then turn off 20 seconds later. Triggering one switch does not automatically turn off the other.

I need the scripts to thereby turn off the switch 20 seconds after the switch has turned on because thats how long it takes to open or close the door.

oh, i see. ok, then tom_l’s answer for full elegance. or using rujelus’s approach:

create an input_bool helper (let’s call it coop_door, so you have input_bool.coop_door)

add this automation:

description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.coop_door
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.coop_door
            state: "on"
        sequence:
          - service: script.open_coop_door
            data: {}
      - conditions:
          - condition: state
            entity_id: input_boolean.coop_door
            state: "off"
        sequence:
          - service: script.open_close_door
            data: {}

then go to your lovelace dashboard and create a card pointing to input_bool.coop_door

If you share the config for those two scripts it you will possibly be able to replace all of this with just the template switch:

  • Your automation,
  • the Input boolean,
  • the two scripts.
switch:
  - platform: template
    switches:
      skylight:
        turn_on:
          - service: script.open_coop_door # or put your script sequence here and delete the script
        turn_off:
          - service: script.close_coop_door # or put your script sequence here and delete the script

Now there is currently no state feedback (value template) so the switch will be operating in “optimistic” mode. That means if you operate the door with the physical remote it will get out of sync with the state shown in home assistant. This is also a problem for your automation and input boolean. The only solution is to hide the remote.

No state feedback is the main problem of 433RF and IR devices. There are better options but it is a bit late for that now.

Edit: actually after reading that product description it has wifi too so you may be able to get state feedback with the Tuya integration.

1 Like