MQTT Switch JSON Command Payload

Hi All,

I can’t quite get the configuration working using the below config snippet inside an mqtt/switch: yaml node, hoping someone may be able to shed some light on what’s happening.

    - name: "Test Output 1"
      unique_id: test_output_1
      state_topic: "miu/8979C74B14705EF4/output/1/state"
      command_topic: "miu/8979C74B14705EF4/control"
      payload_on: '{"chanType":"output","chanNum":"1","property":"on"}'
      payload_off: '{"chanType":"output","chanNum":"1","property":"off"}'
      state_on: "on"
      state_off: "off"
      optimistic: false
      retain: false
      qos: 0

The payload is json as shown above, and works fine when publishing a message via an MQTT app. State functionality works fine.

Thanks, Paul

You are missing a value_template that will allow you to reduce those payloads down to on and off.

I don’t want to reduce these payloads, This is the JSON I actually want to send out to the controller. If I reduce the payloads to on and off then the controller won’t be able to do anything with it. Or am I missing something in my thinking.

For example, if I send the payload via node red, this also works as expected to control the output.

mqtt:
  switch:
    - command_topic: "thermostat_16115/thermostat/set/locked"
      name: Lock buttons 1st Floor
      icon: mdi:lock
      state_topic: "thermostat_16115/thermostat/properties"
      value_template: "{{ value_json.locked }}"
      payload_off: "false"
      payload_on: "true"
      state_off: false
      state_on: true

Here’s an example of a working switch in mqtt. This may be of use to help you work it out.

My understanding is that the value_template is used to parse the state value returned by the state_topic when in JSON format. As my state is nor returned as a JSON string it shouldn’t require the value_template. This seems to be correct as I can see what the state is when toggling it from another source.

I don’t think it has anything to do with the command payload that should be sent to the command_topic, this should just be a JSON payload entered directly into the config for the switch.

What if you leave the {} off the payload_on and payload_off

As an update and for completeness, I did get it working correctly, although I’m not sure how it really differs to what I originally had in the config. The working version is below:

    - name: "Irrigation Solenoid 7"
      unique_id: irrigation_solenoid_7
      state_topic: "miu/8979C74B14705EF4/output/1/state"
      command_topic: "miu/8979C74B14705EF4/control"
      payload_on: '{"chanType": "output","chanNum": "1","property": "on"}'
      payload_off: '{"chanType": "output","chanNum": "1","property": "off"}'
      state_on: "on"
      state_off: "off"
      optimistic: false
      retain: true
      qos: 0

Appreciate the assistance.

Cheers, Paul