Why won't this switch?

Hi. I’m hoping someone could look at my config and tell me if I’ve missed something?

Basically, I have 2 switches (One Sonoff Mini & one Sonoff Basic). All I want to do is to be able to switch either one of them from the Overview page, which seems simple enough. But if I’m honest, I seem to be struggling.

Heres my config

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

mqtt:
  broker: 192.168.1.91
  port: 1883
  username: "Ydraiggoch"
  password: "Password"

switch:
  - platform: mqtt
    name: "Balcony"
    command_topic: "cmd/sonoff01/power"
    state_topic: "stat/sonoff01/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
    
  - platform: mqtt
    name: "Fish"
    command_topic: "cmd/sonoff02/power"
    state_topic: "stat/sonoff02/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

I have the switches visible on the Overview page;

image

But when I try to switch one of them on, it toggles on for about 5 seconds before turning off again automatically.

I suspect this is something to do with the MQTT, but not sure what.

I have the Mosquitto Add On installed and each sonoff is connected (according to the Log & Console in Tasmota.
Here is a snip from the MQTT Log;

1575211104: New connection from 192.168.1.86 on port 1883.
[INFO] found Ydraiggoch on local database
1575211105: New client connected from 192.168.1.86 as DVES_A76427 (p2, c1, k30, u’Ydraiggoch’).
1575211334: Saving in-memory database to /data/mosquitto.db.
1575213135: Saving in-memory database to /data/mosquitto.db.
1575214936: Saving in-memory database to /data/mosquitto.db.
1575216737: Saving in-memory database to /data/mosquitto.db.
1575218538: Saving in-memory database to /data/mosquitto.db.
1575220339: Saving in-memory database to /data/mosquitto.db.
1575222140: Saving in-memory database to /data/mosquitto.db.
1575223941: Saving in-memory database to /data/mosquitto.db.
1575225742: Saving in-memory database to /data/mosquitto.db.
1575227543: Saving in-memory database to /data/mosquitto.db.
1575229344: Saving in-memory database to /data/mosquitto.db.
1575231145: Saving in-memory database to /data/mosquitto.db.
1575232647: Client auto-6265ECCA-A22F-9C79-5C11-D775021797B9 disconnected.
1575232668: New connection from 192.168.1.91 on port 1883.
[INFO] found Ydraiggoch on local database
1575232669: New client connected from 192.168.1.91 as auto-FBF6F6AA-DA5F-52A9-B2CF-4A719AB40635 (p2, c1, k60, u’Ydraiggoch’).
1575232946: Saving in-memory database to /data/mosquitto.db.

191.168.1.86 is one of the switches and 191.168.1.91 is the Home Assistant (Py) and MQTT

Could this have anything to do with both the MQTT and HAS starting at the same time (on the same IP address?

Thanks for any suggestions

Hi,

Basically the state topic is incorrect. I have loads of Sonoff’s that work exactly like you have in you example but some don’t. I think there is a setup option to configure it but I can never find it.

command_topic: “cmd/sonoff01/power” = This is the power on/off command and obviously if the switches are clicking on then this command is correct.

state_topic: “stat/sonoff02/POWER” = This is the reply from the sonoff, If this is wrong then HA does not think it turned off/on the switch and reverts back after 5 seconds.

Try - stat/sonoff02/RESULT as the state topic instead.

If you log onto the web interface of the switch and go to the console and then trigger it from HA you will see what the switch is trying to pass back as the state.

Simon

HI Simon,

Thanks for the detailed response. Unfortunately, that never worked. Changing the State_Topic never changed what happens. Although I maybe never explained the problem very well.

The Sonoff itself doesn’t click, when I press on the toggle switch on the overview graphic, the toggle switches over to the right for a few seconds (suggesting that its switched on - but no action at the Sonoff) then automatically switches back.

There is no change in the Sonoff Console. See Log;

06:38:32 MQT: tele/sonoff01/SENSOR = {“Time”:“2019-12-02T06:38:32”,“Switch1”:“ON”}
06:43:32 MQT: tele/sonoff01/STATE = {“Time”:“2019-12-02T06:43:32”,“Uptime”:“0T15:05:18”,“UptimeSec”:54318,“Vcc”:3.475,“Heap”:30,“SleepMode”:“Dynamic”,“Sleep”:50,“LoadAvg”:19,“MqttCount”:1,“POWER”:“OFF”,“Wifi”:{“AP”:1,“SSId”:“BTHub6-J7KT”,“BSSId”:“80:20:DA:FF:46:F6”,“Channel”:6,“RSSI”:60,“LinkCount”:1,“Downtime”:“0T00:00:07”}}
06:43:32 MQT: tele/sonoff01/SENSOR = {“Time”:“2019-12-02T06:43:32”,“Switch1”:“ON”}

Thanks again

@Ydraiggoch Your config has a couple of typos is all it is…should be like this for the full sonoff config:

  - platform: mqtt
    name: "Rumpus AV"
    state_topic: "stat/sonoff/POWER"
    command_topic: "cmnd/sonoff/POWER"
    availability_topic: "tele/sonoff/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

Note: it is case sensitive POWER is not power and cmnd not cmd

PS: you probably want to set “retain: false” as well

1 Like

Hi Wellsy,

I’ll give that a go. Hopefully it’s that simple!
Unfortunately, I’m away for a few days now, so will try it once I get back and post how I get on.

Thanks for the help

1 Like

Hi Wellsy,

That done the trick! I just change cmd to ‘cmnd’ and changed true to false and it worked! So, Thank you very much for your help. Hugely appreciated.

1 Like

Your welcome and glad you have it sorted now!