Different payload for state_topic and command_topic

A question from a completely newbie…

I have an ESPEasy setup with two relays controlled via h/w logic (4013). “state” is referring to current relay state. The rules looks like:

on TurnOn1 do
if [relay1#state]=0
pulse,12,0,100
endif
endOn

on TurnOff1 do
if [relay1#state]=1
pulse,12,0,100
endif
endOn

I’m trying to add this as a switch to HA using MQTT.

If I set the payload to “0” and “1” the state is updated correctly in HA, but I can’t toggle the HW from HA.

switch:
  - platform: mqtt
    name: "InWall1 Switch a"
    state_topic: "/Inwall1/relay1/state"
    command_topic: "/Inwall1/cmd"
    payload_on: "1"
    payload_off: "0"
    qos: 0
    retain: true

If I set the payload to the required event command, then I can trigger the toggle of the HW but is unable to receive the new state back to HA.

switch:
  - platform: mqtt
    name: "InWall1 Switch a"
    state_topic: "/Inwall1/relay1/state"
    command_topic: "/Inwall1/cmd"
    payload_on: "event,TurnOn1"
    payload_off: "event,TurnOff1"
    qos: 0
    retain: true

I’ve found other posts referring to binary_sensor but have been unable to get it to work.

How can I solve this?

This is one of the weaknesses of the HA implementation.

I think you can use the value_template: parameter to modify the incoming payload to match the outgoing payload using something like

  {% if value == "0" %} "event,TurnOff1" {% else %} "event,TurnOn1" {% endif %}

but its not something I have tried.

It would really be easier (from an HA point of view), if you could have your light have an identical payload for command and status.

maybe just have the esp publish the state back? That’s what I do

Yes, I manage to get it to work.

This is what I ended up with:

  - platform: mqtt
    name: "InWall1 Switch a"
    state_topic: "/Inwall1/relay1/state"
    value_template: >-
      {% if value is equalto "0" %}
        event,TurnOff1
      {% else %}
        event,TurnOn1
      {% endif %}
    command_topic: "/Inwall1/cmd"
    payload_on: "event,TurnOn1"
    payload_off: "event,TurnOff1"
    qos: 0
    retain: true

Thanks for your help :slight_smile:

1 Like

It looks like that the latest (0.75) release actually fixes the problem, so that you can have different states :smile: