Mqtt switch switching off

Hi, I need some help with a MQTT switch where the state keeps going to off when you click the switch. I have been trying different solutions for weeks now and combing through the forum and Youtube.

So here is what I have set / happens.

* THE PROBLEM
When I toggle the switch for my pool pump (Sonoff POWR2 with Tasmota) the pump turns on and remains on while the switch in homeassistant turns to the off state after a second. If I double click the switch in HA quickly, the pump will turn off. So in short the issue seems to only be with the state of the switch.

* THE CONFIG / INFO

  • I am running the latest version of HASS.IO and I have installed the Mosquitto broker via the addon section.
  • I have configured numerous (light mqtt: - platform: mqtt) switches (The Sonoff TX ones also with Tasmota) and these are working fine or as I would expect.
###################################
#          MQTT LIGHTS SECTION                          #
###################################
light mqtt:
  - platform: mqtt
    name: "Pool Light"
    state_topic: "pool_light/stat/POWER"
    command_topic: "pool_light/cmnd/POWER"
    payload_on: "ON"
    payload_off: "OFF"
  • My broker section of the configuration.yaml looks like this:
###################################
#          MQTT BROKER SECTION                           #
###################################
mqtt:
  broker: 127.0.0.1
  port: 1883
  client_id: 2WLS-HA
  username: mqtt
  password: mypassword

#  discovery: false
#  discovery_prefix: homeassistant
  • And my switch (not working) section of the configuration.yaml looks like this:
###################################
#          MQTT SWITCHES SECTION                          #
###################################
switch:
  - platform: mqtt
    name: "Pool Pump"
    state_topic: "poolpump/stat/POWER"
    command_topic: "poolpump/cmnd/POWER"
    payload_on: ON
    payload_off: OFF
  • If I listen to the state topic via the Dev Tools/MQTT section, I can see that the broker is communicating to and from the switch. These messages are successfully posted if I use the switches on either the homeassistant or the sonoff web page.
Listen to a topic
 
Listening to
poolpump/stat/POWER
 
Message 0 received on poolpump/stat/POWER at 9:46 AM:
ON
QoS: 0 - Retain: true

I have also reset my config and started again but ended at the same result, so I do not believe its a typo. The next step would have been to pull in the power usage for the pool pump and add some automation around that but if I cant even get the switch to work correctly 0_o …

Thank you in advance for any assistance

Have you tried using discovery instead of manual mqtt setup. i think if you type setoption19 1 in the tamota console this enables discovery for HA

1 Like

You need to put “ON” and “OFF” in quotes. Otherwise YAML regards them as True and False which is not the correct payload.

1 Like

You are missing quote marks for payload entries.

- platform: mqtt
  name: "Garage light" 
  state_topic: "stat/sonoff04/POWER"
  command_topic: "cmnd/sonoff04/POWER"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
1 Like

@swiftlyfalling, @Bluey

I really thought I had tested that before and it did not work. Must have mixed it with the changes I made to the state topic and not tested it properly.

Anyhow, adding the quotation marks fixed it. Thanks a lot.

@DemonAngel44
i set up my MQTT automatically by using the “Add Integration” button so i dont seem to have a MQTT section in my configuration.yaml but i am seeing the same behavior as op on my sonoff basic/tasmota switches. is there a way to fix it with my setup or do i need to configure my 4 switches manually to get rid of this behavior (turn on then off)