HA - SONOFF switch does not work properly

My HA configuration for SONOFF switch does not work properly, in one case (testcase1) the
control goes through HA interface - but does not work with a HA restart.

In the other case (testcase2) the configuration works at the restart but not via the HA interface.

Where do I make the mistake in the configuration?

 ###################################################
 ## MQTT BROCKER SETTINGS
 ###################################################
 mqtt:
   broker: !secret mqtt_broker
   port: !secret mqtt_port
   username: !secret mqtt_username
   password: !secret mqtt_password
   client_id: !secret mqtt_client_id
   keepalive: 60
   birth_message:
     topic: "tele/rb3a/LWT"
     payload: "Online"
     qos: 1
     retain: true
   will_message:
     topic: "tele/rb3a/LWT"
     payload: "Offline"
     qos: 1
     retain: true

 ################################################### 
 ## MQTT Settings
 ###################################################
 pid_file /var/run/mosquitto.pid
 persistence true
 persistence_location /var/lib/mosquitto/
 log_dest topic
 log_type error
 log_type warning
 log_type notice
 log_type information
 connection_messages true
 log_timestamp true
 log_dest file /var/log/mosquitto/mosquitto.log
 include_dir /etc/mosquitto/conf.d
 
 
 ###################################################
 ## SWITCH: Sonoff Basic Module SETTINGS
 ###################################################
- platform: mqtt
  name: "testcase1"
  state_topic: "stat/M1120N/POWER"
  command_topic: "cmnd/M1120N/POWER"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  optimistic: false
  retain: false
   
- platform: mqtt
  name: "testcase2"
  command_topic: "cmnd/M1120N/POWER"
  state_topic: "tele/M1120N/STATE"
  value_template: "{{ value_json.POWER }}"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  optimistic: false
  retain: false
   

testcase1:

  • works for restarting Sonoff Basic.
  • ON/OFF switching HA.
  • ON/OFF switching with Alexa.
  • Wrong state when restarting HA.

This is based, because when HA restarts, HA will not check the state_topic.

testcase2:

  • works for restarting Sonoff Basic.
  • ON/OFF switching with Alexa.
  • ON/OFF switching HA.

This is based because Sonoff will not send the “tele/M1120N/STATE” when the state has changed.

testcase1 is the correct configuration, but you must program the sonoff to send the state with the retain flag set. This tells the broker to send the last message on that topic to any new clients that subscribe, such as when HA restarts.

Thank you, it works fine now.
retain flag set was the mistake.

1 Like

Hello @petsie! Could you please tell me how you`ve changed the retain flag?

thanks!

Dies sind meine Konfigurationen:
Zuätzlich habe ich beim Switch noch “PowerRetain On” gesetzt.

siehe: https://github.com/arendst/Sonoff-Tasmota/wiki/Home-Assistant

HAS MQTT Settings

 ###################################################
 ## MQTT BROCKER SETTINGS
 ## sudo nano packages/connectors.yaml
 ###################################################
 mqtt:
   broker: !secret mqtt_broker
   port: !secret mqtt_port
   username: !secret mqtt_username
   password: !secret mqtt_password
   client_id: !secret mqtt_client_id
   keepalive: 60
   birth_message:
     topic: "tele/rb3a/LWT"
     payload: "Online"
     qos: 1
     retain: true
   will_message:
     topic: "tele/rb3a/LWT"
     payload: "Offline"
     qos: 1
     retain: true

MQTT BROCKER Settings

 ################################################### 
 ## MQTT Settings
 ## sudo nano /etc/mosquitto/mosquitto.conf 
 ###################################################
 pid_file /var/run/mosquitto.pid
 persistence true
 persistence_location /var/lib/mosquitto/
 log_dest topic
 log_type error
 log_type warning
 log_type notice
 log_type information
 connection_messages true
 log_timestamp true
 log_dest file /var/log/mosquitto/mosquitto.log
 include_dir /etc/mosquitto/conf.d

SONOFF Switch Settings

 ###################################################
 ## SWITCH: Sonoff Basic Module SETTINGS
 ## sudo nano switch/officeroom.yaml
 ###################################################
- platform: mqtt
  name: "HP Drucker"
  state_topic: "stat/M1120N/POWER"
  command_topic: "cmnd/M1120N/POWER"
  value_template: "{{ value_json.POWER }}"
  availability_topic: "tele/M1120N/LWT"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  optimistic: false
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: true
1 Like

Sorry…
These are my configurations:
In addition, I have the switch still “PowerRetain On” set.

If you’re using tasmota, go into ‘Console’ in the Sonoff’s web UI, type ‘PowerRetain 1’ and hit enter.

1 Like

Thanks…hours of reading and frustration and then it`s just…

1 Like

@Bobby_Nobble @Antonio_Santos is this for when I restart HA? Because when I restart HA my sonoff tasmota dhows as ‘off’ but it’s still on.

Yes, use the command in console as shown above.

@BertrumUK where do i enter power retain 1 ? i cannot find it on sonoff’s web ui I am using tasmota.

Here is a picture of its location.

thank you !!

need some more help on this! I set everything as intended however now when I restart a sonoff it defaults to lights on because it seems to receive a turn on message from mosquitto mqtt broker.

my settings:

mqtt:
  broker: core-mosquitto
  username: admin
  password: password
  client_id: HomeAssistantClient
  keepalive: 60
  birth_message:
    topic: "tele/hass1/LWT"
    payload: "Online"
    qos: 1
    retain: true
  will_message:
    topic: "tele/hass1/LWT"
    payload: "Offline"
    qos: 1
    retain: true

light:

  - platform: mqtt
    name: "On/Off Lamp Tafeltje"
    state_topic: "stat/LampTafeltje/RESULT"
    value_template: '{{ value_json["POWER"] }}'
    command_topic: "cmnd/LampTafeltje/POWER"
    availability_topic: "tele/LampTafeltje/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true

and I set PowerRetain 1 in the sonoff console.

edit:

included the following automation:

  • alias: “Power state on HA start-up”
    trigger:
    platform: homeassistant
    event: start
    action:
    • service: mqtt.publish
      data:
      topic: “cmnd/LampTafeltje/POWER”
      payload: “”

now it seems to work but please can this be made simpler with less code?

This is because the retain flag is set for the command_topic. This causes the broker to send the last message to be sent to any client that subsequently subscribes to that topic - such as when your sonoff turns back on.

I think this is because…:

In result of command “PowerRetain 1”:

  • Topic: “stat/LampTafeltje/RESULT” is not retained

  • Topic: “stat/LampTafeltje/POWER” is retained

You can change:

state_topic: "stat/LampTafeltje/POWER" <----- POWER instead of RESULT
    value_template: '{{ value_json["POWER"] }}' <--- remove it

This solution work for my basic sonoff (1 chanel).

2 Likes

As i have the same problem and did everything here; the sonoff should also be restarted after console command ‘PowerRetain 1’ or ’ powerRetain on’ otherwise it still does not work. Also, @gohm44, your solution is what solved my case including after sonoff restart, Thanks…