Custom Button Card - Toggle Lock Rather Than Open Info

I have 2 Zwave locks that I use the custom:button-card to show the lock status and change colour based on lock’s status. That is all working well.

The issue is when I click the lock, it opens the entity’s history of locked/unlocked. I would rather tap action either lock or unlock depending on it’s current state. Here’s what my code looks like:

- entity: lock.front_door
        type: 'custom:button-card'
        icon: mdi:lock
        size: 50px
        state:
           - value: "locked"
             color: green
             icon: mdi:lock
             tap_action:
               action: call-service
               service: lock.unlock
               service_data:
                  entity_id: lock.front_door
           - value: "unlocked"
             color: red
             icon: mdi:lock-open-variant
             tap_action:
               action: call-service
               service: lock.lock
               service_data:
                  entity_id: lock.front_door

The above config is not generating any errors, but tapping the icon still opens the history rather than calling the associated service.

Thanks for any help.

tap action indentation is wrong

      - entity: lock.front_door
        type: 'custom:button-card'
        icon: mdi:lock
        size: 50px
        state:
           - value: "locked"
             color: green
             icon: mdi:lock
             tap_action:
               action: call-service
               service: lock.unlock
               service_data:
                  entity_id: lock.front_door
           - value: "unlocked"
             color: red
             icon: mdi:lock-open-variant
        tap_action:
          action: call-service
          service: lock.lock
          service_data:
             entity_id: lock.front_door

I also notice that you’re using odd intendation all over the place. Pick a value and stick with it. Makes for easier reading.

Looks like it formatted wrong when I pasted compared to what’s in the yaml. Second tap action is on the same level of the first set. Neither works.

- entity: lock.front_door
  type: 'custom:button-card'
  icon: mdi:lock
  size: 50px
  state:
       - value: "locked"
         color: green
         icon: mdi:lock
         tap_action:
            action: call-service
            service: lock.unlock
            service_data:
               entity_id: lock.front_door
     - value: "unlocked"
       color: red
       icon: mdi:lock-open-variant
       tap_action:
           action: call-service
           service: lock.lock
           service_data:
              entity_id: lock.front_door

Your tap action is indented on value, it’s flat out wrong.

I want separate tap actions based on the lock’s current value. If locked, tap action unlocks. If unlocked. tap action locks.

Then you have to template it. Look at the state level, it does not have tap_action as a valid option for state. I.E. wrong… like i’ve said twice now.

Oh templating, a whole new beast.

Appreciate the guidance, thanks!

  - entity: lock.front_door
    type: 'custom:button-card'
    icon: mdi:lock
    size: 50px
    state:
       - value: "locked"
         color: green
         icon: mdi:lock
       - value: "unlocked"
         color: red
         icon: mdi:lock-open-variant
    tap_action:
      action: call-service
      service: |
        [[[
          return (entity.state === 'locked') ? 'lock.unlock' : 'lock.lock';
        ]]]
      service_data:
         entity_id: lock.front_door
4 Likes

Wow. Again, many thanks. I was just trying to write the code myself, but this is much cleaner.

Thank you!!

Hi I am trying to do the same and a search brought me here. I change the code as below but I get an error while tapping the lock
I get a popup Failed to call service [[[


- type: 'custom:button-card'
      entity: lock.front_door
      type: 'custom:button-card'
      icon: mdi:lock
      size: 50px
      state:
         - value: "locked"
           color: green
           icon: mdi:lock
         - value: "unlocked"
           color: red
           icon: mdi:lock-open-variant
      tap_action:
        action: call-service
        service: |
          [[[
            return (entity.state === 'locked') ? 'lock.unlock' : 'lock.lock';
          ]]]
        service_data:
           entity_id: lock.car_door_lock
``

I don’t know if you fixed your problem yet. But I see a problem in your code.
Second line you tell entity: lock.front_door and on the last line lock.car_door_lock. I suppose you have a problem there.

BUT, I also tried that code from @petro and get the same error message as you (but use the same entity name in my case).

I seem to get the same error, does this no longer work?

I found that simply using toggle worked without using service. This is what works for me:

tap_action:
  action: toggle
1 Like

That works for me too. Thank you!