petsie
December 21, 2017, 2:22pm
1
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.
gpbenton
(Graham)
December 22, 2017, 5:12am
2
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.
petsie
December 22, 2017, 7:31am
3
gpbenton:
retain flag set.
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!
petsie
January 11, 2018, 12:20pm
5
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
petsie
January 11, 2018, 12:44pm
6
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
bachoo786
(Bachoo786)
February 14, 2018, 12:15pm
9
@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.
BertrumUK
(Neil B)
February 14, 2018, 4:13pm
10
Yes, use the command in console as shown above.
bachoo786
(Bachoo786)
February 14, 2018, 6:43pm
11
@BertrumUK where do i enter power retain 1 ? i cannot find it on sonoffâs web ui I am using tasmota.
BertrumUK
(Neil B)
February 14, 2018, 6:56pm
12
Here is a picture of its location.
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?
gpbenton
(Graham)
April 6, 2018, 5:44am
15
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.
gohm44
(Gohm44)
April 8, 2018, 2:42pm
16
I think this is becauseâŚ:
In result of command âPowerRetain 1â:
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
febalci
(Erkan)
September 17, 2019, 6:07pm
17
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âŚ