How to extract "relay/0" from an MQTT payload?

Hello everyone!

When my relay is switched, I get the following messages on MQTT (the first is the switch command, the second the feedback from the relay)

maison-relay1/relay/0/set 1
maison-relay1/data {"relay/0":"1","time":"2018-04-04 13:38:34","mac":"68:C6:3A:A3:AD:46","host":"maison-relay1","ip":"","id":2032}

I would like HA to understand the state of the switch via the JSON payload of the second line. To this, I added to the switch section

  - name: maison_relay1
    platform: mqtt
    state_topic: maison-relay1/data
    command_topic: maison-relay1/relay/0/set
    payload_on: 1
    payload_off: 0
    availability_topic: maison-relay1/status
    state_format: 'json:"relay/0".value'
    payload_available: "1"
    payload_not_available: "0"

The state is not retained (or rather - acquired) when I switch the switch on (the switch button in HA goes to teh right, then comes back to the “off” state, which I assume to mean that the state was not acquired).

I believe that the problem is with the way the key relay/0 is to be addressed. My example is from the the JSONPath documentation which suggests using quotes.

I also tried to have payload_available: 1 and payload_available: "1" (the latter being probably the correct one).

How should I address this?


Try adding
state_value_template: "{{ value_json.relay/0 }}"

to your light config

Thank you but unfortunately this did not work either.

Following @jivesinger path, I finally found the right incantation (with the help of

For switches: value_template: '{{ value_json["relay/0"] }}'

For lights: state_value_template: '{{ value_json["relay/0"] }}'

Note that there is a state_ prefix for lights, otherwise the stanza is the same.