Converting an input boolean into a lock

Hi,

Here is the situation, a Schlage bluetoooth lock which is only HomeKit enabled, the lock is HomeKit paired but doesn’t show up in Home Assistant even if the HomeKit (bridge) integration is there.
I also have an AppleTV 4K which acts as a HomeKit Hub (as I can lock or unock from outside).
I followed a trick found on this forum : create a dummy input boolean, expose it to HomeKit (which I did) and then setup some HomeKit Automation to sync the input boolean with the HomeKit Lock.
That’s OK, but I have other locks and this one is showing and acting as an input boolean. I would like it to be seen as a lock so that it’s icon and behaviour is the same as the others i.e. its state is locked or unlocked not on or off.

I tried :

homeassistant:
  customize:
    input_boolean.back_door_lock_bool:
      device_class: lock
      icon: mdi: lock

but that doesn’t work.

I think the road to go is templating but I’m not sure this is the right way and I don"t understand all of it.
On the Template Lock page we can read this code :

lock:
  - platform: template
    name: Garage door
    value_template: "{{ is_state('sensor.door', 'on') }}"
    lock:
      service: switch.turn_on
      data:
        entity_id: switch.door
    unlock:
      service: switch.turn_off
      data:
        entity_id: switch.door

my dummy input boolean is : ‘input_boolean.back_door_lock_bool’.
Can someone translate this code in plain english, my translation would begin “create a lock which name is Garage door…” but after this I’m stuck.

Thanks

V.

I have not used the lock function before, so I hope its behavior is similar to switch. So in your case it should look something like this, where the status is depending on the input_boolean state. Since it is a dummy and not a source, I use an extra service to force the dummy to the right state.

lock:
  - platform: template
    name: Garage door
    value_template: "{{ is_state('input_boolean.back_door_lock_bool', 'on') }}"
    lock:
      - service: input_boolean.turn_on
        entity_id: input_boolean.back_door_lock_bool
      - service: switch.turn_on
        data:
          entity_id: switch.door
    unlock:
      - service: input_boolean.turn_off
        entity_id: input_boolean.back_door_lock_bool
      - service: switch.turn_off
        data:
          entity_id: switch.door

If this doesn’t work you could try the switch function and use and icon_template to change the icon depending on the state.

        icon_template: >-
          {% if is_state('input_boolean.back_door_lock_bool', 'on') %}
            mdi:lock
          {% else %}
            mdi:lock-open-outline
          {% endif %}

You need to remove the switch turn on/off services from the template, OP has only the input boolean. Otherwise it’s fine.

Thanks guys, this should work as it looks like what I found elsewhere for different cases but I would like to understand before implementing.
Lets tell you what I get and what I don’t :

lock:
  - platform: template
    name: Garage door

This must be “create a fake lock which name is Garage door”

value_template: "{{ is_state('input_boolean.back_door_lock_bool', 'on') }}"

If ever the input boolean state is “on” then…
And that’s where I don’t get it :

   lock:
     - service: input_boolean.turn_on
       entity_id: input_boolean.back_door_lock_bool
     - service: switch.turn_on
       data:
         entity_id: switch.door

Is this part the one that should be triggered when input_boolean.back_door_lock_bool is on ?

1- If so, and if the input boolean is already on, why would I call a service to turn it on ?
2- If we’re talking about a lock, why am I calling a switch.turn_on service and not a lock.lock service ?

Finally, is this part :

unlock:
      - service: input_boolean.turn_off
        entity_id: input_boolean.back_door_lock_bool
      - service: switch.turn_off
        data:
          entity_id: switch.door

the one called when the input_boolean.back_door_lock_bool is not on ?

As you can see I’m a bit lost.

The value template determines the state of your lock, if the input_boolean is on, the lock shows the state locked, if the input_boolean is off the lock shows unlocked.

The service in the lock: section is executed when you lock the lock, meaning when the lock is unlocked (input_boolean is off) and you loxk it through the UI for example, it will turn the input_boolean on and the status of the lock will change to locked.
Same goes for the unlock: block.

Hum :face_with_monocle: I have to re-read this one…

Thanks

You’re telling me that I have to deal with my input_boolean within the template ?
But I already have an input boolean as

input_boolean:
  back_door_lock_bool:
    name: BckDoorLock
    icon: mdi:Door
    initial: off

This dummy lock works, I mean I can switch it on or off from the UI.

As far as I understand, that’s not the way to do it, I must not make an input boolean as a dummy lock + the template. The template alone is enough to manage all this game ?

What I don’t get is the :
If the input boolean is on then switch on input boolean . That doesn’t make sens for me.

No, you need to create the input boolean in order to use it for the lock.

That’s not what happens. When the inout boolean is on, you template lock will be locked, so you can’t lock it, you can only unlock it and when you unlock it, it executes the service to turn the input boolean off.

So it’s for keeping synced the input boolean with the template.
Let me comment the code as I understand it…

lock: # Among locks
  - platform: template # One is a fake one
    name: Garage door # Which name is Garage door
    value_template: "{{ is_state('input_boolean.back_door_lock_bool', 'on') }}" # When input boolean is on...
    lock: # This dummy lock is locked 
      - service: input_boolean.turn_on # and turn on the input boolean (if ever it was off)
        entity_id: input_boolean.back_door_lock_bool
      - service: switch.turn_on # WHAT SWITCH ? we're only talking of locks and input booleans ??
        data:
          entity_id: switch.door  
    unlock: # When the dummy lock is not on
      - service: input_boolean.turn_off # Turn an input boolean off
        entity_id: input_boolean.back_door_lock_bool #specificaly this input boolean
      - service: switch.turn_off
        data:
          entity_id: switch.door

Am I understanding ?

Yes.

I guess so.

One questoin is still there : what switch ? As We are only dealing with locks and input booleans, what is the switch on line

 - service: switch.turn_on # WHAT SWITCH ? we're only talking of locks and input booleans ??
        data:
          entity_id: switch.door  

and

  - service: switch.turn_off
        data:
          entity_id: switch.door

As I said in one of the previous posts, the switch part is not needed in your case.

Thank you very much for your patience and taking time to explain all in details.

1 Like

Another question comes to me : Would’nt it be easier and more elegant to customize my input_boolean as a lock ?
What’s the difference between this method and making a customization from the UI ?
Would it generate the same lines ?

The only thing that would change would be that you don’t have the services lock.lock, lock.unlock. But I don’t think that really matters for you.

got it ! Thanks.
Yes it matters as this is for sync with HomeKit, so user should be able to lock/unlock from HA, from HomeKit and from the physical lock.

The templated lock is just there to show and to act as a lock, to have icon and lock/unlock state and not On/Off.

could you please post the final working yaml code for this implementation? I can’t figure out which bits to put together and what to remove here

2 Likes