Command_topic and State_topic with different payloads

I have a device which when I publish with “cmnd/testtopic/POWER” with payload “Off”, it comes back with response “Off 0” instead of just “Off”. Publishing with payload “On” will get response “On 100” instead of just “On”.

  • platform: mqtt
    name: “Test”
    command_topic: “cmnd/testtopic/POWER” #device will only accept On and Off
    state_topic: “stat/testtopic/POWER” #responds with “On 100” or “Off 0”
    payload_on: “On 100”
    payload_off: “Off” Note: Tried with “Off 0” but device will not turn off
    retain: true
    qos: 1
    optimistic: true Note: setting to false wont even turn the device off as the slider stays at On

Due to this, I’m only able to turn the device on/off. I’m not able to get the correct status shown if I manually turn the device on/off outside of HA as its not getting the correct response on HA. I started looking at state_value_template but I’m not able to understand how it truly works. Am I on the right track? Help?

Thanks!

Set up an MQTT binary_sensor to receive your switch states, then set up an template switch using the binary_sensor states as the state, and service: mqtt.publish to command on/off.

I have one set up the same but am not at home to paste as an example :roll_eyes:

Thanks for the tips. I managed to get it to work with the following code if anyone needs it. Problem is I have 20 of these switches. Is there anywhere I can get this to work with the std MQTT switch?

binary_sensor:

  • platform: mqtt
    name: “Test Sensor”
    state_topic: “stat/testtopic/POWER”
    payload_on: “On 100”
    payload_off: “Off 0”
    qos: 1

switch:

  • platform: template
    switches:
    testswitch:
    value_template: “{{ is_state(‘binary_sensor.test_sensor’, ‘on’) }}”
    turn_on:
    service: mqtt.publish
    data:
    topic: “cmnd/testtopic/POWER”
    payload: “on”
    turn_off:
    service: mqtt.publish
    data:
    topic: “cmnd/testtopic/POWER”
    payload: “off”
1 Like

If your switch is for a light then your in luck. There is a MQTT template_light which has separate templates for state and command values.

For some strange reason there is no equivalent MQTT template_switch.

Go figure !