Mqtt switch (plugwise) not working properly

Ok, I have some plugwise switches which I run plugwise2py for, which enables MQTT control of these.
That part works fine, from the command line I get it all working, but from HA I have a challenge…

If I configure the switch like this I can switch things on and off perfectly, but the switch always toggles back to off, evne if I turn on the lamp (which works)

- platform: mqtt
  name: "Plugwise Mechanische Ventilatie"
  optimistic : false
  command_topic: 'plugwise2py/cmd/switch/000D6F00002369B9'
  state_topic: 'plugwise2py/state/circle/000D6F00002369B9'
  value_template: '{"mac": "000D6F00002369B9", "switch": "{{ value_json.switch }}"}'
  payload_on: '{"mac": "000D6F00002369B9", "switch": "on"}'
  payload_off: '{"mac": "000D6F00002369B9", "switch": "off"}'
  retain: true

If I configure it like this the status works correctly, but I am unable to switch anything on or off…

- platform: mqtt
  name: "Plugwise Lamp Buffetkast"
  optimistic : false
  command_topic: 'plugwise2py/cmd/switch/000D6F0000237228'
  state_topic: 'plugwise2py/state/circle/000D6F0000237228'
  value_template: '{"mac": "000D6F0000237228", "cmd": "switch":, "val": "{{ value_json.switch }}"}'
  payload_on: '{"mac": "000D6F0000237228", "switch": "on"}'
  payload_off: '{"mac": "000D6F0000237228", "switch": "off"}'
  retain: true

I’m guessing that my problem is that my state topic sends {“switch”: “on”} but that my command topic wants to receive a {'cmd": “switch”, “val”: “on”}

I thought I had that configured correctly in the last example, but ha sends out “switch”: “on” instead of the {“cmd”: “switch”, “val”: “on”}

anyone happen to have a nice solution to this one?

Thanks

Please format your configuration using the instructions in the link in the big blue box at the top of the page. Without that, nobody can make sense of it.

Sorry, changed it. should be good now. thanks.

The value_template: is used to pick out a value from the json. If your payload_on: and payload_off: contain all the json, then I think you can just omit the value_template: altogether.

- platform: mqtt
  name: "Plugwise Lamp Buffetkast"
  optimistic : false
  command_topic: 'plugwise2py/cmd/switch/000D6F0000237228'
  state_topic: 'plugwise2py/state/circle/000D6F0000237228'
  payload_on: '{"mac": "000D6F0000237228", "switch": "on"}'
  payload_off: '{"mac": "000D6F0000237228", "switch": "off"}'
  retain: true

You just have to make sure that the payload received always matches one of configured values exactly.

Edit:
I have just reread the second comment of your first post, and I’m not sure if the first example you posted is correct. What is the configuration that successfully sends a command?

The first one I can switch on and off, but the state does not work.
The second one I can NOT switch, but the state is correct if I switch the device from outside of home assistant

I’m not sure I understand this comment, given your examples. Does it need {'cmd": “switch”, “val”: “on”} or '{"mac": "000D6F00002369B9", "switch": "on"}' from your example?

To turn on or off the switch I need to send {“mac”:“000D6F00002369B9”,“cmd”:“switch”,“val”:“on”}.
Upon successful switching on the switch will return a full response, including the state:

plugwise2py/state/circle/000D6F00019nnnnn {"powerts": 1405452834, "name": "circle4", "schedule": "off", "power1s": 107.897, "power8s": 109.218, "readonly": false, "interval": 10, "switch": "on", "mac": "000D6F00019nnnnn", "production": false, "monitor": false, "lastseen": 1405452834, "power1h": 8.228, "online": true, "savelog": true, "type": "circle", "schedname": "test-alternate", "location": "hal1"}

out of which I would have to get the ‘switch’ part to be able to see if it’s on or off.

One of the restrictions of an MQTT switch is the payload for the command must match the payload for the state response. The best way I have seen to get around this is to use a combination of a binary sensor and a template switch, using the mqtt_publish service. Something like this - I may have got some of the details wrong, but the principle works


# sensor to detect switch state
binary_sensor:
    - platform: mqtt
      name: switch_state
      state_topic: plugwise2py/state/circle/000D6F00019nnnnn
      value_template: '{{value_json.switch }}'
      payload_on: "on"
      payload_off: "off"

# switch to publish mqtt messages
switch:
    - platform: template
      switches:
          first_switch:
              value_template: "{{ is_state('binary_sensor.switch_state', 'on') }}"
              turn_on:
                  service: mqtt.publish
                  data:
                      topic: "plugwise2py/cmd/switch/000D6F0000237228"
                      payload: '{"mac": "000D6F0000237228", "switch": "on"}'
              turn_off:
                  service: mqtt.publish
                  data:
                      topic: "plugwise2py/cmd/switch/000D6F0000237228"
                      payload: '{"mac": "000D6F0000237228", "switch": "off"}'