MQTT Switch - Payload

Hi,
Trying to build a simple Irrigation controller in HA.
I have the ESP base controller working with the relays, accepting a MQTT topic as follows

I publish the folloing through MQTT to turn on Zone 1

Command topic: "Home/Sprinkler1/Command"
Command Body: {"zone" : "1", "command" : "ON"}

The ESP controller publish back the following to indicate the relay has been turned on

Status Topic: "Home/Sprinkler1/Status/1"  // 1-4 represents each sprinkler Zone
Status Body: {"command":"ON"}

Using MQTT commands direct through MQTT,fx everything behaves as expected.

In HA I have switch defined

  • platform: mqtt
    name: “Sprinkler - Zone 1”
    state_topic: “Home/Sprinkler1/Status/1”
    command_topic: “Home/Sprinkler1/Command”
    qos: 1
    payload_on: “{"Zone":"1", "command":"ON"}”
    payload_off: “OFF”
    retain: true
    value_template: ‘{{ value_json.command }}’

When I toggle the switch on in HA it correctly publishes the topic and body that reflects turning the relay on, but the switch imediatley turns off. I am lost as to see why it works this way

thanks

I just tweaked the HA Component to the following based on some further reading, but did not make a differnce, the switch in HA toggles off straight after turning on.

  • platform: mqtt
    name: “Sprinkler - Zone 1”
    state_topic: “Home/Sprinkler1/Status/1”
    command_topic: “Home/Sprinkler1/Command”
    qos: 0
    payload_on: “{"Zone":"1", "command":"ON"}”
    payload_off: “{"Zone":"1", "command":"OFF"}”
    retain: true
    value_template: ‘{“command” : “{{ value_json.command }}”}’

And tried this as well. Starting to do my head in

  • platform: mqtt
    name: “Sprinkler - Zone 1”
    state_topic: “Home/Sprinkler1/Status/1”
    command_topic: “Home/Sprinkler1/Command”
    qos: 0
    payload_on: “{"Zone":"1", "command":"ON"}”
    payload_off: “{"Zone":"1", "command":"OFF"}”
    retain: true
    value_template: ‘{{ value_json[“command”] }}’

You have come across one of the limitations of the HA implementation of an MQTT switch - the payload for the command and the status messages have to be symmetrical. This is because the payload_on is used for both the command and must exactly match the status (subject to the value_template:)

As such, I don’t think you can get your current messages to work. There are other ways, but I suggest that you make the command and status message have the relevant zone in the topic, and just have the payload as a simple ON or OFF

Thanks for the info, re-wrote my ESP app and have now managed to get it working how I would like.

1 Like

Hello,

I have the same problem with asymmetrical payloads. MQTT Switch is an ESP8266 with Tasmota as a Serial Brdige to control my old LG TV. At the moment I can switch the TV on, but the switch in HA jumps back to off, because it doesn’t get the right state. So I can’t switch the TV off.
To switch the TV on the command_topic is “Home/LR/sonoff_tv/cmnd/SerialSend” and the payload is “ka 00 01”.
The payload in the state_topic “Home/LR/sonoff_tv/tele/RESULT” is then “a 01 OK01x”.
What do I have to template to get this to work?
Here is my code:

  - platform: mqtt
    name: "TV Power"
    state_topic: "Home/LR/sonoff_tv/tele/RESULT"
    value_template: '{% if value_json.SerialReceived == "a 01 OK01x" %} "ka 00 01" {% else %} "ka 00 01" {% endif %}'
    command_topic: "Home/LR/sonoff_tv/cmnd/SerialSend"
    availability_topic: "Home/LR/sonoff_tv/tele/LWT"
    payload_on: "ka 00 01"
    payload_off: "ka 00 00"
    payload_available: "Online"
    payload_not_available: "Offline"
    #optimistic: false
    #qos: 0
    #retain: true
  - platform: mqtt
    name: "TV Mute"
    state_topic: "Home/LR/sonoff_tv/tele/RESULT"
    #value_template: '{{ value_json["SerialReceived"] == "e 01 OK00x" }}'
    value_template: '{% if value_json["SerialReceived"] == "e 01 OK00x" %} on {% else %} off {% endif %}'
    command_topic: "Home/LR/sonoff_tv/cmnd/SerialSend"
    availability_topic: "Home/LR/sonoff_tv/tele/LWT"
    payload_on: "ke 00 00"
    payload_off: "ke 00 01"
    payload_available: "Online"
    payload_not_available: "Offline"
  - platform: mqtt
    name: "TV Energy"
    state_topic: "Home/LR/sonoff_tv/tele/RESULT"
    value_template: '{{ value_json["SerialReceived"] == "q 01 OK04x" }}'
    command_topic: "Home/LR/sonoff_tv/cmnd/SerialSend"
    availability_topic: "Home/LR/sonoff_tv/tele/LWT"
    payload_on: "jq 00 04"
    payload_off: "jq 00 00"
    payload_available: "Online"
    payload_not_available: "Offline"

Nothing of this works to switch off…

Anybody with an idea?

The problem was fixed in a recent release. You can now use state_on and state_off parameters to define the payload for the state message.

1 Like

Ok, nice. My workaround was a binary_sensor and a template switch:

binary_sensor
  - platform: mqtt
    name: get_tv_power
    state_topic: "Home/LR/sonoff_tv/tele/RESULT"
    value_template: '{{ value_json["SerialReceived"] }}'
    payload_on: "a 01 OK01x"
    payload_off: "a 01 OK00x"
    availability_topic: "Home/LR/sonoff_tv/tele/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"

switch:
  - platform: template
    switches:
      tv_power:
        value_template: "{{ is_state('binary_sensor.get_tv_power', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.set_tv_power
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.set_tv_power

will try the state_on and state_off now to define the payload…

Edit: WORKS! Thank you - Now the documentation needs to be updated.

So, now I have the same problem with a Tasmota switch. After turning on the state gets not updated and stays off. I believe it did work on 0.89 HA, but not anyomore. Availability detection works great.
Is it me or HA? Any help welcome.

the config:

switch:

  • platform: mqtt
    name: “SHP22”
    state_topic: “home/floating/SHP22/stat/POWER”
    command_topic: “home/floating/SHP22/cmnd/POWER”
    availability_topic: “home/floating/SHP22/tele/LWT”
    payload_on: “ON”
    payload_off: “OFF”
    state_on: “Aus”
    state_off: “An”
    payload_available: “Online”
    payload_not_available: “Offline”
    optimistic: false
    qos: 0
    retain: true

mqtt result (via MQTTfx):

home/floating/SHP22/stat/POWER
ON