Hi, I tried to setup MQTT for a couple of Sonoff Basic wireless switches and S26 wireless power plugs (flashed with Tasmota 6.4.1) and configure qos2 + retain, but it doesn’t work as I had expected. For each Sonoff device I have created a sensor, like this:
- platform: mqtt
name: "plug1"
state_topic: "homeassistant/tele/plug1/STATE"
value_template: "{{ value_json.POWER }}"
command_topic: "homeassistant/cmnd/plug1/POWER"
availability_topic: "homeassistant/tele/plug1/LWT"
payload_on: "ON"
payload_off: "OFF"
state_on: "ON"
state_off: "OFF"
payload_available: "Online"
payload_not_available: "Offline"
optimistic: false
qos: 2
retain: true
When I click (switch ON or OFF) on this entity in the Lovelace front-end, it will switch ON or OFF the Sonoff device, but the switch indicator in the Lovelace interface will almost immediate move back. More than a minute later it will return to the correct position. Until than it is not possible to switch the Sonoff device again. So it works, but with a 5 minute delay!
I run HA (0.85) and MQTT (eclipse-mosquitto) in docker.
Here is the HA MQTT debug logging of some events of one of the Sonoff S26 plugs:
# initialization
jan 14 06:33:14 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/tele/plug1/LWT: b'Online'
jan 14 06:33:14 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/tele/plug1/STATE: b'{"Time":"2019-01-14T06:31:23","Uptime":"0T12:31:28","Vcc":3.536,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER":"ON","Wifi":{"AP":1,"SSId":"Bachstraat20","BSSId":"04:A1:51:52:BF:C0","Channel":6,"RSSI":62}}'
jan 14 06:33:14 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/cmnd/plug1/POWER: b'ON'
jan 14 06:33:16 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to homeassistant/tele/plug1/LWT
jan 14 06:33:16 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to homeassistant/tele/plug1/STATE
jan 14 06:33:16 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/tele/plug1/LWT: b'Online'
jan 14 06:33:16 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/tele/plug1/STATE: b'{"Time":"2019-01-14T06:31:23","Uptime":"0T12:31:28","Vcc":3.536,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER":"ON","Wifi":{"AP":1,"SSId":"Bachstraat20","BSSId":"04:A1:51:52:BF:C0","Channel":6,"RSSI":62}}'
# The Sonoff device reports it state.
# The switch in the Lovelace interface shows ON.
jan 14 06:36:22 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/tele/plug1/STATE: b'{"Time":"2019-01-14T06:36:23","Uptime":"0T12:36:28","Vcc":3.533,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER":"ON","Wifi":{"AP":1,"SSId":"Bachstraat20","BSSId":"04:A1:51:52:BF:C0","Channel":6,"RSSI":64}}'
# After I moved the switch in the Lovelace interface to OFF
# Note: the switch in the interface moved back by it self to ON!
jan 14 06:36:57 DEBUG (MainThread) [homeassistant.components.mqtt] Transmitting message on homeassistant/cmnd/plug1/POWER: OFF
jan 14 06:36:57 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/cmnd/plug1/POWER: b'OFF'
jan 14 06:36:57 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/stat/plug1/RESULT: b'{"POWER":"OFF"}'
jan 14 06:36:57 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/stat/plug1/POWER: b'OFF'
# The Sonoff device reports it state
# Now the switch in the Lovelace interface moves to OFF!
jan 14 06:41:22 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/tele/plug1/STATE: b'{"Time":"2019-01-14T06:41:23","Uptime":"0T12:41:28","Vcc":3.534,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER":"OFF","Wifi":{"AP":1,"SSId":"Bachstraat20","BSSId":"04:A1:51:52:BF:C0","Channel":6,"RSSI":62}}'
# After I moved the switch in the lovelace interface to ON
# Note: the switch in the Lovelace interface moved back by it self to OFF!
jan 14 06:41:44 DEBUG (MainThread) [homeassistant.components.mqtt] Transmitting message on homeassistant/cmnd/plug1/POWER: ON
jan 14 06:41:44 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/cmnd/plug1/POWER: b'ON'
jan 14 06:41:44 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/stat/plug1/RESULT: b'{"POWER":"ON"}'
jan 14 06:41:45 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/stat/plug1/POWER: b'ON'
jan 14 06:46:22 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/tele/plug1/STATE: b'{"Time":"2019-01-14T06:46:23","Uptime":"0T12:46:28","Vcc":3.532,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER":"ON","Wifi":{"AP":1,"SSId":"Bachstraat20","BSSId":"04:A1:51:52:BF:C0","Channel":6,"RSSI":60}}'
# The Sonoff device reports it state.
# Now the switch in the Lovelace interface moves to ON!
jan 14 06:51:23 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/tele/plug1/STATE: b'{"Time":"2019-01-14T06:51:23","Uptime":"0T12:36:28","Vcc":3.533,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER":"ON","Wifi":{"AP":1,"SSId":"Bachstraat20","BSSId":"04:A1:51:52:BF:C0","Channel":6,"RSSI":64}}'
And so on...
My Sonoff device config:
Here is my HA MQTT config:
mqtt:
broker: 192.168.11.100
discovery_prefix: homeassistant
discovery: true
username: !secret mqtt_username
password: !secret mqtt_password
And my MQTT related automations:
automation:
###############################################################################
- alias: "Power state on HA start-up"
trigger:
platform: homeassistant
event: start
action:
- service: mqtt.publish
data:
topic: "homeassistant/cmnd/+/state"
payload: ""
###############################################################################
- alias: "Enable MQTT discovery for all devices"
trigger:
platform: homeassistant
event: start
action:
- service: mqtt.publish
data:
topic: "homeassistant/cmnd/+/SetOption19"
payload: "1"
###############################################################################
- alias: "Sonoff state on HA start-up"
trigger:
platform: homeassistant
event: start
action:
- service: mqtt.publish
data:
topic: "homeassistant/cmnd/+/STATUS"
payload: "2"
Tasmota my_user_config.h
MQTT settings:
#define MQTT_STATUS_OFF "OFF" // [StateText1] Command or Status result when turned off (needs to be a string like "0" or "Off")
#define MQTT_STATUS_ON "ON" // [StateText2] Command or Status result when turned on (needs to be a string like "1" or "On")
#define MQTT_CMND_TOGGLE "TOGGLE" // [StateText3] Command to send when toggling (needs to be a string like "2" or "Toggle")
#define MQTT_CMND_HOLD "HOLD" // [StateText4] Command to send when button is kept down for over KEY_HOLD_TIME * 0.1 seconds (needs to be a string like "HOLD")
// -- MQTT topics ---------------------------------
// Example "tasmota/bedroom/%topic%/%prefix%/" up to 80 characers
#define MQTT_FULLTOPIC "homeassistant/%prefix%/%topic%/" // [FullTopic] Subscribe and Publish full topic name - Legacy topic
// %prefix% token options
#define SUB_PREFIX "cmnd" // [Prefix1] Sonoff devices subscribe to %prefix%/%topic% being SUB_PREFIX/MQTT_TOPIC and SUB_PREFIX/MQTT_GRPTOPIC
#define PUB_PREFIX "stat" // [Prefix2] Sonoff devices publish to %prefix%/%topic% being PUB_PREFIX/MQTT_TOPIC
#define PUB_PREFIX2 "tele" // [Prefix3] Sonoff devices publish telemetry data to %prefix%/%topic% being PUB_PREFIX2/MQTT_TOPIC/UPTIME, POWER and TIME
// May be named the same as PUB_PREFIX
// %topic% token options (also ButtonTopic and SwitchTopic)
#define MQTT_TOPIC PROJECT // [Topic] (unique) MQTT device topic, set to 'PROJECT "_%06X"' for unique topic including device MAC address
#define MQTT_GRPTOPIC "sonoffs" // [GroupTopic] MQTT Group topic
#define MQTT_BUTTON_TOPIC "0" // [ButtonTopic] MQTT button topic, "0" = same as MQTT_TOPIC, set to 'PROJECT "_BTN_%06X"' for unique topic including device MAC address
#define MQTT_SWITCH_TOPIC "0" // [SwitchTopic] MQTT button topic, "0" = same as MQTT_TOPIC, set to 'PROJECT "_SW_%06X"' for unique topic including device MAC address
#define MQTT_CLIENT_ID "DVES_%06X" // [MqttClient] Also fall back topic using Chip Id = last 6 characters of MAC address
// -- MQTT - Telemetry ----------------------------
#define TELE_PERIOD 300 // [TelePeriod] Telemetry (0 = disable, 10 - 3600 seconds)
// -- MQTT - Home Assistant Discovery -------------
#define HOME_ASSISTANT_DISCOVERY_ENABLE 1 // [SetOption19] Home Assistant Discovery (0 = Disable, 1 = Enable)
How can I properly setup a Sonoff device with qos2 and retain with this 5 minute delay?