Toggle lock/unlock

Hi, I’m pretty new to HA, but I have a question about lock domains.

From what I can work out, a lock entity can only have a “lock” or “unlock” service. If I try to make it toggle, it fails.

My question is, firstly- is this correct? Are Locks unlike say switches, and can’t be toggled?

And if so, is there an easy work around solution? Can a lock be converted to a switch to allow toggling on/off (lock/unlock)?

To give some background, I have an automated WiFi CatFlap, (Surepet) and its HACs-added entity creates it as a lock able door.
I have successfully created a small 1x1 widget using HTTP Shortcut for my Android, but currently I need to have 2 widgets - 1 for lock, and 1 for unlock.
It would be much more preferable to have a single toggle widget (with a bonus if I can work out a way to change its icon based on the Lock’s status, which appears in HA as a seperate sensor)

Apologies for the lack of technical terms, as mentioned, pretty new to the game, but learning quickly!
Cheers

(edit: thought it might help to provide the request URL and request body from my HTTP Shortcut:
https://xxxxxx.duckdns.org/api/services/lock/lock
{“entity_id”: “lock.locked_all_cat_flap”}
)

You could create a template switch that calls lock and unlock based on the switch state.

2 Likes

I know this is an old request, but here’s what I use. I trigger it from a tap_action on a picture-glance card so I have one-click lock/unlock (default shows more info).

Script:

alias: Toggle Lock
fields:
  lock_entity_id:
    description: Lock Entity ID
    example: lock.front_door
    required: true
    selector:
      entity:
        domain: lock
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ is_state(lock_entity_id, "locked") }}'
        sequence:
          - service: lock.unlock
            data: {}
            target:
              entity_id: '{{ lock_entity_id }}'
    default:
      - service: lock.lock
        data: {}
        target:
          entity_id: '{{ lock_entity_id }}'
mode: single
icon: mdi:lock-clock

To call:

service: script.toggle_lock
data:
  lock_entity_id: lock.front_door

Hope this is helpful to someone.

4 Likes

Adding my 2 cents, I was able to solve this by using a couple of conditional types within my entities card and by creating two separate sensors (lock, unlocked), conditionally displayed based on the state of the lock. A template switch might be a better choice, but I did not consider that while working through this. I think its actually the smarter choice and I’ll look at moving that way. In any case, here’s my approach, hope it helps someone:
In the UI:

  - type: conditional
    conditions:
      - entity: lock.back_door
        state: locked
    row:
      name: Back Door Locked
      entity: sensor.back_door_state_locked
      tap_action:
        action: call-service
        service: lock.unlock
        service_data:
          entity_id: lock.back_door
  - type: conditional
    conditions:
      - entity: lock.back_door
        state: unlocked
    row:
      name: Back Door Unlocked
      entity: sensor.back_door_state_unlocked
      tap_action:
        action: call-service
        service: lock.lock
        service_data:
          entity_id: lock.back_door
    style: |
      :host {
        --paper-item-icon-color:
          {% if states('lock.back_door') == 'unlocked' %}
              rgb(245, 188, 66);
            {% elif states('lock.back_door') == 'locked' %}
              white;
            {% endif %}
            }

My sensors:

    back_door_state_locked:
      friendly_name: Back Door
      value_template: >-
        {% if(states('lock.back_door') == 'locked') %}Unlock{% else %}Locked{% endif%}
      icon_template: >-
        {% if(states('lock.back_door') == 'locked') %}mdi:lock-open{%else%}mdi:lock {%endif%}
    back_door_state_unlocked:
      friendly_name: Back Door
      value_template: >-
        {% if(states('lock.back_door') == 'unlocked') %}Lock{% else %}Locked{% endif%}
      icon_template: >-
        {% if(states('lock.back_door') == 'unlocked') %}mdi:lock-open{%else%}mdi:lock {%endif%}

I know this is an old post but could you give an example how to create a template switch? I tried to read the example you link to, but I don’t get it :frowning:

The code example you have with conditional, maybe I’m little dumb her. I don’t get it where to type that, I thought it was conditional card, but I get errors when using that. Could you please guide me how to be able to use toggle together with a lock

Thank

You need to edit the UI code directly. I believe I started with an entities card and edit its code to add the conditional cards. FWIW, I stopped doing it that way, as I found the highlight color was the same between the two conditions, locked/unlocked.

What I ended up doing instead was using an entities card. Here is the code for that:

- entity: sensor.back_door_state
                type: custom:secondaryinfo-entity-row
                name: Back Door
                secondary_info: '{{states(''lock.back_door'') | title}}'
                style: |
                  :host {
                    --paper-item-icon-color:
                      {% if states('lock.back_door') == 'unlocked' %}
                          rgb(245, 188, 66);
                        {% elif states('lock.back_door') == 'lock.back_door' %}
                          white;
                        {% endif %}
                        }
                tap_action:
                  action: call-service
                  service: script.toggle_back_door_lock
                  service_data:
                    entity_id: lock.back_door

Hope that helps.

Thanks, was going to take the time to make a blueprint for this, but honestly, a script works best and here it already is. Cheers!

I have a Garmin watch and couldn’t for the life of me figure out how to toggle the lock with one button. I was set on making two buttons, one to unlock and another to lock. I don’t know too much about coding and everything was gibberish. Probably could figure out what everyone was talking about if I really looked into it. But then I had a moment of inspiration and created a script using if-then.

IF lock is unlocked THEN lock, ELSE, unlock. Super simple.

Might look into a template switch, so I can get the icons to change, but if you just want it to work, this is it.