MQTT lock state from JSON

OK, I struggle with something that’s probalby extremely easy once again…

I just managed to get my new bluetooth/mqtt lock to work, but I can’t for the life of me figure out how to get the current state…

So via MQTT I get this state message: keyble/mac/status

{"battery_low":false,"lock_status":"LOCKED","lock_status_id":3,"pairing_allowed":false}

In my configuration.yaml I added:

mqtt:
  lock:
    - name: "Eingangstüre"
      unique_id: lock123456789
      availability_topic: "keyble/mac/client/status"  

      command_topic: "keyble/mac/command"
      payload_lock: "lock"
      payload_unlock: "unlock"
      payload_open : "open"
      
      json_attributes_topic: "keyble/mac/status"

      state_topic: "{{value_json.lock_status}}"

      state_locked: "LOCKED"
      state_unlocked: "UNLOCKED"

      optimistic: false
      qos: 1

This is obviously wrong, but how do I add the lock_status value to the state_topic?

Right now I can only see it in the Attributes field in the UI, but the state is always unlocked (so the little lock is open und yellow - also in the history it only says locked unlocked)

also another question: where is the “open” button in the UI? I don’t really need it, but would be nice to know. I only have LOCK and UNLOCK right now (manually sending “open” via MQTT to keyble/mac/command also works)

state_topic is a constant, not a template. It’s the topic where the lock’s state is posted. You’re looking for value_template. That’s where you put a template to extract the current state from the payload.

open is a relatively new addition I believe, its possible the UI doesn’t support it. I’m not really familiar with it since I don’t have any locks that support open.

I just tried that:

      state_topic: 
        - value_template: "{{value_json.lock_status}}"

but HA won’t even let me refresh the configuration…

All this doesn’t make sense to me… even if I knew where to put the value_template, how would it know what topic to look at? There are three topics ( client/status, /command and /status) and nowhere does it say how to “link” the template to the right one… (except for availability)

      state_topic: "keyble/mac/status/lock_status"

doesn’t work either (even though it would be the most logical )

well ok … THIS works

mqtt:
  lock:
    - name: "Eingangstüre"
      unique_id: lock123456789
      availability_topic: "keyble/mac/client/status"  

      command_topic: "keyble/mac/command"
      payload_lock: "lock"
      payload_unlock: "unlock"
      payload_open : "open"
      
      json_attributes_topic: "keyble/mac/status"

      state_topic: "keyble/mac/status"
      value_template: "{{value_json.lock_status}}"

      state_locked: "LOCKED"
      state_unlocked: "UNLOCKED"

      optimistic: false
      qos: 1


I have no Idea why, because nowhere does it say that this generic value_template should be used for the state of the lock :woman_shrugging:

Is this even the right way to do it, or ist it just luck that it works?!

Yes, that’s correct. value_template is specifically the template used to extract the value from the payload in state_topic. It doesn’t apply to any of the other topics. If you notice for all the other topics there is a corresponding _template field (json_attributes_template, availability_template, etc.)

The reason value_template is weird is because it is the oldest. MQTT has been around for a long time and has grown a ton. I’m fairly certain at the time value_template was added to the schema it was the only template option and it wasn’t considered that there might be others someday.

Should it be renamed to state_template? Yes. But that would be a breaking change for almost every single mqtt entity in existence. So I’m guessing the authors did not want to do that and kept it.

That being said, the doc for that field could probably use a tweak. It doesn’t seem to mention state at all.

that does make sense - for me a template is completely generic and can be used for everything, but like you say - changing it would cause a lot of trouble. A mention in the docs what it does would be nice though :sweat_smile: