Dynamic tap_action Button Card

Hi All,

I hope that you guys can help me here.
I would like to make a garage button card. The button card should do the following

  1. Change the icon,colour and texts when the garage is opened or closed
  2. When the garage door opened, tap on the card will close it
    When the garage door closed tap on the card will open it

I can get the item 1 work but cannot make item 2 work.

I tried to use if statement with entity_id to select between input_boolean.opengarge and input_boolean.closegarage when the sensor detect whether the garage open or close but it did not work
note:
input_boolean.garageopened is the sensor
input_boolean.opengarage is the output to open garage
input_boolean.closegarage is the output to close garage

Is it possible to use if statement with entity_id to dynamically select different boolean to activate ? Or is there a better way of doing this if this does not work ?

Below is my code:

type: custom:button-card
entity: input_boolean.garageopened
color_type: card
color: white
icon: mdi:garage
state:
  - value: 'on'
    icon: mdi:garage-open
    color: orange
    name: Garage Opened
  - value: 'off'
    color: green
    name: Garage Closed
tap_action:
  action: call-service
  service: input_boolean.turn_on
  service_data:
    entity_id: >
      {% is_state('input_boolean.garageopened', 'on') %}        
        input_boolean.closegarage
      {% else %}
        input_boolean.opengarage
      {% endif %}

Hi, I see two possible solutions:

  1. Replace all input_booleans with a single one, which closes the garage when it is turned off an opens the garage when it is turned on and which is updated when the status of the garage door changes. Then you can use the input_boolean.toggle service with your button card.
  2. Create a script, which checks the condition and calls either the one or the other input_boolean. By pressing the button, you can call the script then, by calling the service script.<name-of-script>.
    Script Syntax - Home Assistant

I would prefer the first solution if it is possible, but the second should work as well.

1 Like

Hi Tobi-bo

Thank you for your suggestion, can’t do option 1 as the input_boolean linked to external controller so maybe I have to create a script as your suggestion.

Thanks

Hi Tobi-bo

Thank you, after follow your suggestion I got it to work. Below is the code if anyone interested
Custom Button Card Code:

type: custom:button-card
entity: input_boolean.garageopened
color_type: card
color: white
icon: mdi:garage
state:
  - value: 'on'
    icon: mdi:garage-open
    color: orange
    name: Garage Opened
  - value: 'off'
    color: green
    name: Garage Closed
tap_action:
  action: call-service
  service: script.garagetoggle

Script:

service: input_boolean.turn_on
target:
  entity_id: |
    {% if is_state('input_boolean.garageclosed', 'on') %}
      input_boolean.garage
    {% elif is_state('input_boolean.garageopened','on') %}
      input_boolean.closegarage
    {% else %} {% endif %}
1 Like