Json mqtt switch won't react correctly

hello,

I’ve a device (in this case a sonoff switch, but I’ve other similar switches) that act’s on a json mqtt message
topic = huis/verdieping0/ergens/SonOff_1
on payload: { “S1” : “ON”, other settings if necessary}
So the switch acts on the payload given below correct.

Now the switch answers with something like:
topic: huis/verdieping0/ergens/SonOff_1b
payload: { “S1”:“ON”, “LED”:0, “Button”:0 }

Home assistant doesn’t react on this MQTT message.

No if the switch sends back a payload (exactly): {“S1”:“ON”}
Home Assistant reacts correctly, but …
if I put an extra space (leaving the same correct json message), home assistant doesn’t react.

So I think homeassistant doesn’t perform json encoding.
Below in the comment are a number of things I’ve tried.

Anyone has a clue ?
thanks Stef

switch SonOff_1:
  platform      : mqtt
  #platform      : mqtt_template                #ERROR platform not found
  name          : SonOff 1
  icon          : mdi:lightbulb
  
  command_topic : huis/verdieping0/ergens/SonOff_1
  payload_on    : '{"S1":"ON"}'
  payload_off   : '{"S1":"OFF"}'
  
  state_topic   : huis/verdieping0/ergens/SonOff_1b
  # if none of the statements below: state acts on exact copy of payload
  #state_value_template : "{{ value_json.S1 }}"    # does nothing, state acts on exact copy of payload
  #state_format  : 'json:S1.value'                 # does nothing, state acts on exact copy of payload
  #state_on      : '{{ value_json.S1 == "ON"  }}'  # no reaction at all 
  #state_off     : '{{ value_json.S1 == "OFF" }}'
  #value_template: '{{ value_json.S1 }}'           # no reaction at all

I think the decoding section you need is

  payload_on    : '{"S1":"ON"}'
  payload_off   : '{"S1":"OFF"}'
  state_on: "ON"
  state_off: "OFF"
  value_template: "{{ value_json.S1 }}"

payload_on and payload_off send the payload - its plain text, as you say HA doesn’t do json encoding.

state_on and state_off define the values of the incoming state message, and value_template extracts the correct field from the message.

1 Like

thanks very much gp, you solution works perfect.
greetings, Stef

1 Like