First time user, light/switch state issue

Hi all!

Running HA on my RPi3 with MQTT devices (switches, sensors)

Have currently one issue!

  • switch/light status is always in the “ON” state after HA restart (or RPi3 restart).

Switching the light on the wall (changing the state of the lights) is reported back to HA fine just after every restart the light status (toggle switch) shows “ON” even if the light is “OFF”

  - platform: mqtt
    name: "Hodnik"
    state_topic: "stat/hodnik/LIGHT"
    command_topic: "cmnd/hodnik/LIGHT"
    payload_on: "ON"
    payload_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

Just did some testing!

The issue is that if the light is ON and the switch looses power (its set to be OFF when the power comes back) HA did not get a OFF massage and the state stay’s ON and it switches the light ON

Looks like you’re using a sonoff? does it not send an mqtt command when it regains power?
The other option is to comment out the state_topic, but then you won’t have the state recorded like the switch…

Yes it’s a Sonoff.

It does send the “OFF” mqtt command when it connects to the broker. But still HA switches the light on (to the last state set on HA frontpage).

hmm, try changing your qos on home assistant and the sonoff to 2. Then the broker should retain the message until all subscribers arrive

can’t see the option for the sonoff, just try it on home assistant. It’s the subscriber anyway…

No changes!

If HA switch / state stay’s on “ON” when the sonoff looses power it will switch to “ON” when it regains power.
I want it to stay OFF.

Maybe try put qos: 0 again and retain: false.

Sounds a bit strange, so it definitely when home assistant starts it turns the sonoff on?
I ask because there is a setting in the sonoff about if you want it to retain its last state and also if you want it to turn on or off after a power cycle.

PowerOnState on the sonoff is set to “OFF” / “0”

If HA restarts the state goes to ON not the actual switch (sonoff stay’s OFF)

Oh ok, idk about that. Hope someone else chips in, I’ll need this answer as well. Good luck!