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?
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.
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.
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
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