Fail to parse Kincony MQTT switch value to get output real state

Hello,
I have a kincony 32 relay board with a MQTT integration.
I am able to control the relay (on/off) but in home assistant the status stay “unknown”

I spend many hours in tries, read the seller and HA docs and forum but I still stuck. I found people that had the same trouble but apparently without answer

What I have
The yaml configuration gave by the supplier is below

mqtt:  
  switch:
  - name: 'KC868-32_output-1'
    unique_id: a8s-kcs-output-1
    state_topic:  'KC868_A32/1234/STATE'
    command_topic: 'KC868_A32/1234/SET'
    payload_on:  '{"output1":{"value":true}}'
    payload_off:  '{"output1":{"value":false}}'
    value_template: "{{ value_json.output1.value }}"
    state_on: 1
    state_off: 0

What works:
The output is physicaly set to ON or OFF on the real board
What fail:
The status in home asistant still “unknown”
In home assistant logs I get an error:

Template variable error: 'value_json' is undefined when rendering '{{ value_json.output1.value }}
Error parsing value: 'value_json' is undefined (value: {"input1":{"value":false},"input2":{"value":false},"input3":{"value":false},"input4":{"value":false},"input5":{"value":false},.....

On the MQTT server I see the command and the response in the goot topics (SET or STATE)
The full response is below

{"input1":{"value":false},"input2":{"value":false},"input3":{"value":false},"input4":{"value":false},"input5":{"value":false},"input6":{"value":false},"input7":{"value":false},"input8":{"value":false},"input9":{"value":false},"input10":{"value":false},"input11":{"value":false},"input12":{"value":false},"input13":{"value":false},"input14":{"value":false},"input15":{"value":false},"input16":{"value":false},"input17":{"value":false},"input18":{"value":false},"input19":{"value":false},"input20":{"value":false},"input21":{"value":false},"input22":{"value":false},"input23":{"value":false},"input24":{"value":false},"input25":{"value":false},"input26":{"value":false},"input27":{"value":false},"input28":{"value":false},"input29":{"value":false},"input30":{"value":false},"input31":{"value":false},"input32":{"value":false},"output1":{"value":false},"output2":{"value":false},"output3":{"value":false},"output4":{"value":false},"output5":{"value":false},"output6":{"value":false},"output7":{"value":false},"output8":{"value":false},"output9":{"value":false},"output10":{"value":false},"output11":{"value":false},"output12":{"value":false},"output13":{"value":false},"output14":{"value":false},"output15":{"value":false},"output16":{"value":false},"output17":{"value":false},"output18":{"value":false},"output19":{"value":false},"output20":{"value":false},"output21":{"value":false},"output22":{"value":false},"output23":{"value":false},"output24":{"value":false},"output25":{"value":false},"output26":{"value":false},"output27":{"value":false},"output28":{"value":false},"output29":{"value":false},"output30":{"value":false},"output31":{"value":false},"output32":{"value":false},"adc1":{"value":0},"adc2":{"value":0},"adc3":{"value":0},"adc4":{"value":0},}

What I tried
I tried several configuration according to the ha documentation and related topics.
for example like

...
     value_template: "{value_json}"
     state_on:  '{"output1":{"value":true}}'
     state_off: '{"output1":{"value":false}}'

I have no more error in HA logs but the status still unknown.
I also tried to simulate the device answer by posting directly in the MQTT server to try to understand and find a working combination but without succeed

Thanks for help

Use mqttexplorer to get a better idea of the topics

It’s not proper JSON due to the ending comma ,}, which is why value_json won’t work.

Try:

    value_template: >
      {% set value_json = value | replace(",}", "}") | from_json %}
      {{ value_json.output1.value }}

Thank you so much both.

@Spiro I was trying with mqttExplorer and another web client.
My trouble was not on the mqtt side but sometime it help to get a different point of view :slight_smile:

@koying
I was aware of the extra coma, I saw it when I threw the payload in a JSON parser. But even without the coma nothing worked.

Your solution helped me more with the value_json.output1.value, I was not able to make it works …
Now everything will work perfectlly.
I will also continue with some fake switch to better understand templating as describe in the documentation.

Thank you both for your time


For anyone seeing this topic for a kincony MQTT config with the KCS firmare. This is my final configuration for a relay

mqtt:
  switch:
   - name: 'KC32_578_o1'
     unique_id: KC32_578_o1
     state_topic:   'KC868_A32/123456/STATE'
     command_topic: 'KC868_A32/123456/SET'
     payload_on:  '{"output1":{"value":true}}'
     payload_off: '{"output1":{"value":false}}'
     value_template: >
      {% set value_json = value | replace(",}", "}") | from_json %}
      {{ value_json.output1.value }}
     state_on:  true
     state_off: false

I have noticed that under certain circumstances (e.g when I switch quilky on/off) the board does not sometime send it last status.

if you have the case you can get the whole status by publishing
get_datas":{"value":true}}

You can bruteforce by adding it to each command

     payload_on:  '{"output1":{"value":true},"get_datas":{"value":true}}'
     payload_off: '{"output1":{"value":false},"get_datas":{"value":true}}'

Or make a special request, for example each minute to be sure to get updated