Help with MQTT Light Configuration Using JSON Payloads

Hi everyone,

I’m trying to configure an MQTT light in Home Assistant, but I’m running into issues with the state and command payloads. The light uses JSON payloads for both controlling the light and reporting its state. Here’s the setup:

JSON Payloads:

  • Command to turn the light ON:
    {"output": 1, "state": "ON"}
  • Command to turn the light OFF:
    {"output": 1, "state": "OFF"}
  • State topic payloads (published by the light):
    • When the light is ON: {"output": 1, "state": "ON"}
    • When the light is OFF: {"output": 1, "state": "OFF"}

What I Need:

  1. Home Assistant should send the correct JSON payloads ({"output": 1, "state": "ON"} or {"output": 1, "state": "OFF"}) to the command_topic to control the light.
  2. Home Assistant should correctly interpret the state_topic JSON payloads to determine whether the light is ON or OFF.

My Current Configuration:

Here’s what I’ve tried in my configuration.yaml:

mqtt:
  - light:
      name: "Serveur"
      unique_id: "server"
      state_topic: "state/relay"
      command_topic: "command/relay/set"
      availability_topic: "lwt/relay"
      qos: 0
      payload_on: '{"output": 1, "state": "ON"}'
      payload_off: '{"output": 1, "state": "OFF"}'
      state_value_template: "{{ value_json.state }}"
      payload_available: "online"
      payload_not_available: "offline"
      optimistic: false

The issue is that the state doesn’t match the payload_on and payload_off since I can’t find a way to tell HomeAssistant that the payload should also follow a template rule.

Any help or guidance would be greatly appreciated! Thank you in advance.

SO I kinda solved this by doing :
state_value_template: "{{ value_json | tojson }}"

Seems a bit dirty to me