Custom button card : action depending on state

Why do I get this pop-up instead of switching my aircon on/off ?

type: custom:button-card
name: gang
state:
  - value: 'on'
    color: red
    spin: true
    tap_action:
      action: call-service
      service: climate.turn_off
      service_data: {}
      target:
        entity_id: climate.airco_gang
  - value: 'off'
    color: green
    spin: false
    tap_action:
      action: call-service
      service: climate.turn_on
      service_data: {}
      target:
        entity_id: climate.airco_gang
entity: sensor.airco_gang_is_on_or_off

Because you’re not following the Readme. Tap_action doesn’t go under state, it goes directly under the card (like state, name, entity). Tap_action is not an option within state.

Follow the Readme on Github and copy/paste the examples and alter them to your own needs. In your case you should either use toggle as action (if climate supports that) or use a template like explained there.

Also, it’s easier to ask for help in a thread dedicated to this card that has already over 6000 replies, than creating your own. You can search withing that thread and most likely have answers to a lot of questions:

Thank you for pointing me the way. I’m just starting with HA, so it’s a lot to accumulate and finding my way where to go for information & help.

I think I can’t use toggle as action, because I had to create a binary sensor to know the status of my aircon.

I think I almost there, but my Java is far away :wink:

type: custom:button-card
name: gang
state:
  - value: 'on'
    color: red
    spin: true
  - value: 'off'
    color: green
    spin: false
tap_action:
  action: call-service
  service: |
    [[[
      if states['binary_sensor.airco_gang_is_on'] == 'on'
        return 'climate.turn_off';
      else
        return 'climate.turn_on';
    ]]]
  service_data: {}
  target:
    entity_id: climate.airco_gang
entity: binary_sensor.airco_gang_is_on

1 Like

I ran into this too. For the tap_action I call a script in which I check the state and call the appropriate action. This way I do not have to mess with Java.

2 Likes

Did it that way too :wink:

tap_action:
  action: call-service
  service: script.airco_togglen
  service_data:
    entity_id: climate.airco_gang
  target: {}
entity: binary_sensor.airco_gang_is_on