MQTT Lock State Example

Hey all, this is cross post of my post on reddit yesterday.

Basically, I have a working switch and I’m looking to move it to the mqtt lock component. Unfortunately, I’m having a hard time understanding the value_template that sets the state of the lock entity and I havne’t been able to find a good example of it out in the wild, my search-fu seems to be failing me.

Simply put, what is it expecting in the state message? Locked/Unlocked, On/Off, True,False?

If my hardware is writing locked/unlocked, what do I need to put for value_template?

Thanks all for your help!

HA expects the same payload in the state message as the command message.

You can use the value_template to adjust the state message to match the payload, but you need to be more specific before anyone can help. In particular, what is your configuration, and what messages are being sent back by the lock.

Thanks for the quick reply. This is my current config. I’ve tried posting a lock/unlock (while the value_template was in place) with no change.

My state topic is currently posting locked/unlocked

lock:
 - platform: mqtt
   name: "Door Locks"
   command_topic: "house/lock/door/cmnd"
   state_topic: "house/lock/door/state"
   payload_unlock: "unlock"
   payload_lock: "lock"
   retain: false
   optimistic: false
   qos: 2
   availability_topic: house/status
   payload_available: "online"
   payload_not_available: "offline"
   value_template: '{{ value.x }}'

and what is the state message being sent by the lock?

State messages from the lock are currently ‘locked’ and ‘unlocked’ posted into the ‘house/lock/door/state’ topic

Ok, so if I remove my current value_template and post either a lock/unlock it updates properly.

My question then is what do I need in value_template to match against ‘locked’ and ‘unlocked’

  • I prefer locked/unlocked for state since its way easier to debug and understand at a glance. IMO, commands and states should be differentiated (locked vs lock).

Something like (and I am no expert so you need to check the syntax)

{% if value == 'locked' }lock{% else if  value == 'unlocked' %}unlock{% else %}unknown{% endif %}

But you won’t be looking at MQTT messages very often, so I suggest to take the easy approach and just match the state payload to the command payload and remove the value template altogether.

1 Like

That looks so close… but it doesn’t seem to like ‘value’. It throws a fit for me about the %'s and the if block (even after adding the missing %). I haven’t been able to find too many examples of using value_template to extract the current topic’s payload directly. Many are using value_json objects.

That that to say, Definitely appreciate your help. This is the final ‘solution’ that seems to work for me. This way too, it’ll accept locked/unlocked AND lock/unlock :slight_smile:

value_template: "{{ value | replace('ed','') }}"

TLDR, I’m new at templating, it’s not terribly verbose and I’m not sure how to troubleshoot this without restarting HA a billion times. but this works for me

2 Likes

I cant get my state to update at all what am i doing wrong.

lock:

  - platform: mqtt
    name: "Front Door"
    state_topic: "stat/frontdoor/POWER1"
    value_template: '{{ value_json[“POWER”] }}'
    command_topic: "cmnd/frontdoor/POWER1"
    payload_unlock: "1"
    payload_lock: "0"
    qos: 1
    

mqtt console

MQT: tele/frontdoor/STATE = {“Time”:“2019-08-18T10:51:18”,“Uptime”:“6T23:54:15”,“Vcc”:3.156,“POWER”:“ON”,“Wifi”:{“AP”:1,“SSId”:“FortKnox”,“RSSI”:74,“APMac”:“F0:9F:C2:67:0F:17”}}
10:51:18 MQT: tele/frontdoor/SENSOR = {“Time”:“2019-08-18T10:51:18”,“Switch1”:“OFF”,“Switch2”:“ON”}
10:51:28