Converting MQTT Lock Status

I have this MQTT value for a lock.

{
  "value_id": "3-98-1-0",
  "node_id": 3,
  "class_id": 98,
  "type": "bool",
  "genre": "user",
  "instance": 1,
  "index": 0,
  "label": "Locked",
  "units": "",
  "help": "State of the Lock",
  "read_only": false,
  "write_only": false,
  "min": 0,
  "max": 0,
  "is_polled": false,
  "value": true,
  "lastUpdate": 1605037274413
  "lastUpdate": 1605037274730
}

How do I get the state, as it uses true/false for the locked state? It will be a value template I presume?

Correct. You may wish to use the key called label because it appears to use the same terms that Home Assistant uses to indicate the two states of a lock: LOCKED and UNLOCKED.

  value_template: "{{ value_json.label | upper }}"

Label doesn’t change. The value that changes is “value” which changes between true when locked and false when unlocked. Label always says “Locked”.

Replying to my own message here, thanks for the pointer. Changing the label to value, so it says

value_template: "{{ value_json.value }}"

and also including these two lines

state_locked: true
state_unlocked: false

Made it work correctly. It was the json bit I think I was missing.

Another way to do it is to leave state_locked and state_unlocked with their default values and simply use the template (which you are already using) to convert the received states.

value_template: "{{ 'LOCKED' if value_json.value == 'true' else 'UNLOCKED' }}"

The only thing you may need to tweak is whether the test should use 'true' or true (string value or boolean value). It depends on how Home Assistant interprets the received JSON. My bet is it’ll be handled as a string but I may be wrong.