Trying to get sonoff basic working with Mosquitto 4.1 on hassio

Hi
I have the following components set up:
hassio 0.90.1
Mosquitto 4.1
Sonoff basic with ESP8266 Community version 2.5.0
Sonoff-Tasmota 6.5.0

Configuration.yaml

mqtt:
  broker: core-mosquitto
  username: !secret mqtt_username
  password: !secret mqtt_password

switch:
  - platform: mqtt
    name: "sonoff 1"
    state_topic: “stat/sonoff_1/RESULT”
    value_template: "{{ value_json.POWER }}"
    qos: 0
    command_topic: “cmnd/sonoff_1/POWER”
    payload_on: "ON"
    payload_off: "OFF"
    availability_topic: “tele/sonoff_1/LWT”
    payload_available: “Online”
    payload_not_available: “Offline”
    retain: true

accesscontrollist

# General Section
# topic readwrite #
topic read $SYS/#

# User Section
user airoguard
topic readwrite stat/sonoff_1/RESULT
topic readwrite cmnd/sonoff_1/POWER1
topic readwrite tele/sonoff_1/LWT

mosquitto config

{
  "logins": [
    {
      "username": "airoguard",
      "password": "XXXXXX."
    }
  ],
  "anonymous": false,
  "customize": {
    "active": true,
    "folder": "mosquitto"
  },
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem"
}

What am I doing wrong?
Thanks
Rob

I believe that bit is only for the built in broker. Try commenting that out or remove.

The Mqtt addon requires an “acl” list for known clients. Info on the config page on the particulars.

Hope it helps.

Hi Akriss
Thanks for quick response. I do appreciate it.

ok so I’ve commented out:

mqtt:
  broker: core-mosquitto
  username: !secret mqtt_username
  password: !secret mqtt_password

and integrated Mosquitto as a new integration referencing the local IP address as host with account/password as stated in Mosiquitto config and “Enable Discovery” ticked.

With the following configuration.yaml:

# mqtt:
#  broker: core-mosquitto
#  username: !secret mqtt_username
#  password: !secret mqtt_password

switch:
  - platform: mqtt
    name: "sonoff 1"
    state_topic: “stat/sonoff_1/RESULT”
    value_template: "{{ value_json.POWER }}"
    qos: 0
    command_topic: “cmnd/sonoff_1/POWER”
    payload_on: "ON"
    payload_off: "OFF"
    availability_topic: “tele/sonoff_1/LWT”
    payload_available: “Online”
    payload_not_available: “Offline”
    retain: true

the result is “sonoff 1 unavailable” being displayed.

Sonoff config is:

Sonoff Basic Module
Sonoff1
 MQTT parameters 
Host ()
192.168.0.17

Port (1883)
1883

Client (DVES_51CD30)
DVES_%06X

User (DVES_USER)
airoguard

Password
••••••••

Topic = %topic% (sonoff)
sonoff_1

Full Topic (%prefix%/%topic%/)
%topic%/%prefix%/

No apparent activity in home-assistant.log:

2019-03-28 12:21:52 WARNING (MainThread) [homeassistant.components.sensor] Setup of platform ring is taking over 10 seconds.
2019-03-28 12:21:52 WARNING (MainThread) [homeassistant.components.camera] Setup of platform ring is taking over 10 seconds.
2019-03-28 12:26:40 WARNING (MainThread) [homeassistant.helpers.entity] Update of camera.front_door is taking over 10 seconds
2019-03-28 12:29:41 WARNING (MainThread) [homeassistant.helpers.entity] Update of camera.front_door is taking over 10 seconds
2019-03-28 12:32:43 WARNING (MainThread) [homeassistant.helpers.entity] Update of camera.front_door is taking over 10 seconds
2019-03-28 12:38:46 WARNING (MainThread) [homeassistant.helpers.entity] Update of camera.front_door is taking over 10 seconds

Mosquitto log (sonoff IP is 192.168.0.35):

1553675819: |-- getuser(airoguard) AUTHENTICATED=1 by http
1553675819: New client connected from 172.30.32.1 as 879a3d12-a371-4c9d-9f5a-2445aa214d37 (c1, k60, u'airoguard').
1553675843: Saving in-memory database to /data/mosquitto.db.
1553677643: Saving in-memory database to /data/mosquitto.db.
1553679444: Saving in-memory database to /data/mosquitto.db.
1553681245: Saving in-memory database to /data/mosquitto.db.
1553683047: Saving in-memory database to /data/mosquitto.db.
1553684847: Saving in-memory database to /data/mosquitto.db.
1553686649: Saving in-memory database to /data/mosquitto.db.
1553688449: Saving in-memory database to /data/mosquitto.db.
1553690250: Saving in-memory database to /data/mosquitto.db.
1553692051: Saving in-memory database to /data/mosquitto.db.
1553693853: Saving in-memory database to /data/mosquitto.db.
1553695653: Saving in-memory database to /data/mosquitto.db.
1553697455: Saving in-memory database to /data/mosquitto.db.
1553699256: Saving in-memory database to /data/mosquitto.db.
1553701057: Saving in-memory database to /data/mosquitto.db.
1553702857: Saving in-memory database to /data/mosquitto.db.
1553704658: Saving in-memory database to /data/mosquitto.db.
1553706460: Saving in-memory database to /data/mosquitto.db.
1553708261: Saving in-memory database to /data/mosquitto.db.
1553710062: Saving in-memory database to /data/mosquitto.db.
1553711862: Saving in-memory database to /data/mosquitto.db.
1553713664: Saving in-memory database to /data/mosquitto.db.
1553715464: Saving in-memory database to /data/mosquitto.db.
1553717265: Saving in-memory database to /data/mosquitto.db.
1553719067: Saving in-memory database to /data/mosquitto.db.
1553720867: Saving in-memory database to /data/mosquitto.db.
1553721776: Client 879a3d12-a371-4c9d-9f5a-2445aa214d37 disconnected.
1553721794: New connection from 172.30.32.1 on port 1883.
1553721794: |-- mosquitto_auth_unpwd_check(airoguard)
1553721794: |-- ** checking backend http
1553721794: |-- url=http://127.0.0.1:8080/login
1553721794: |-- data=username=airoguard&password=XXXXXX&topic=&acc=-1&clientid=
[INFO] found airoguard on local database
1553721795: |-- getuser(airoguard) AUTHENTICATED=1 by http
1553721795: New client connected from 172.30.32.1 as ad47b603-6851-49ac-8dee-0238725edd00 (c1, k60, u'airoguard').
1553722669: Saving in-memory database to /data/mosquitto.db.
1553724470: Saving in-memory database to /data/mosquitto.db.
1553726270: Saving in-memory database to /data/mosquitto.db.
1553728071: Saving in-memory database to /data/mosquitto.db.
1553729873: Saving in-memory database to /data/mosquitto.db.
1553731674: Saving in-memory database to /data/mosquitto.db.
1553733474: Saving in-memory database to /data/mosquitto.db.
1553735275: Saving in-memory database to /data/mosquitto.db.
1553735781: Client ad47b603-6851-49ac-8dee-0238725edd00 disconnected.
1553736023: New connection from 192.168.0.17 on port 1883.
1553736023: |-- mosquitto_auth_unpwd_check(airoguard)
1553736023: |-- ** checking backend http
1553736023: |-- url=http://127.0.0.1:8080/login
1553736023: |-- data=username=airoguard&password=XXXXXX&topic=&acc=-1&clientid=
[INFO] found airoguard on local database
1553736023: |-- getuser(airoguard) AUTHENTICATED=1 by http
1553736023: New client connected from 192.168.0.17 as 1F8cK6imjoGh26IW7ulMbL (c1, k60, u'airoguard').
1553736023: Client 1F8cK6imjoGh26IW7ulMbL disconnected.
1553736023: New connection from 192.168.0.17 on port 1883.
1553736023: |-- mosquitto_auth_unpwd_check(airoguard)
1553736023: |-- ** checking backend http
1553736023: |-- url=http://127.0.0.1:8080/login
1553736023: |-- data=username=airoguard&password=XXXXXX&topic=&acc=-1&clientid=
[INFO] found airoguard on local database
1553736024: |-- getuser(airoguard) AUTHENTICATED=1 by http
1553736024: New client connected from 192.168.0.17 as 04d7d6b4-addc-4602-a917-40714e7be645 (c1, k60, u'airoguard').
1553736075: Client 04d7d6b4-addc-4602-a917-40714e7be645 disconnected.
1553736093: New connection from 192.168.0.17 on port 1883.
1553736093: |-- mosquitto_auth_unpwd_check(airoguard)
1553736093: |-- ** checking backend http
1553736093: |-- url=http://127.0.0.1:8080/login
1553736093: |-- data=username=airoguard&password=XXXXXX&topic=&acc=-1&clientid=
[INFO] found airoguard on local database
1553736094: New client connected from 192.168.0.17 as 5c33b195-9870-4c44-8912-5d81189f1731 (c1, k60, u'airoguard').
1553736094: |-- getuser(airoguard) AUTHENTICATED=1 by http
1553736857: New connection from 192.168.0.17 on port 1883.
1553736857: |-- mosquitto_auth_unpwd_check(airoguard)
1553736857: |-- ** checking backend http
1553736857: |-- url=http://127.0.0.1:8080/login
1553736857: |-- data=username=airoguard&password=XXXXXX&topic=&acc=-1&clientid=
[INFO] found airoguard on local database
1553736858: |-- getuser(airoguard) AUTHENTICATED=1 by http
1553736858: New client connected from 192.168.0.17 as 3dlqjwaCPHesqFOzA6y4K6 (c1, k60, u'airoguard').
1553736858: Client 3dlqjwaCPHesqFOzA6y4K6 disconnected.
1553736858: New connection from 192.168.0.17 on port 1883.
1553736858: |-- mosquitto_auth_unpwd_check(airoguard)
1553736858: |-- ** checking backend http
1553736858: |-- url=http://127.0.0.1:8080/login
1553736858: |-- data=username=airoguard&password=XXXXXX&topic=&acc=-1&clientid=
[INFO] found airoguard on local database
1553736859: |-- getuser(airoguard) AUTHENTICATED=1 by http
1553736859: New client connected from 192.168.0.17 as 97d71427-25b0-474e-92e6-d37fd0a6d73b (c1, k60, u'airoguard').
1553737076: Saving in-memory database to /data/mosquitto.db.
1553737297: Client 97d71427-25b0-474e-92e6-d37fd0a6d73b disconnected.
1553737301: Socket error on client 5c33b195-9870-4c44-8912-5d81189f1731, disconnecting.
1553737313: New connection from 192.168.0.17 on port 1883.
1553737313: |-- mosquitto_auth_unpwd_check(airoguard)
1553737313: |-- ** checking backend http
1553737313: |-- url=http://127.0.0.1:8080/login
1553737313: |-- data=username=airoguard&password=XXXXXX&topic=&acc=-1&clientid=
[INFO] found airoguard on local database
1553737315: New client connected from 192.168.0.17 as 1bf55e8a-31d6-46cd-8a4b-1da35e59b04b (c1, k60, u'airoguard').
1553737315: |-- getuser(airoguard) AUTHENTICATED=1 by http

Still something wrong?
Thanks
Rob

Also I had set up ACL from the beginning:

acl.conf:

acl_file /share/mosquitto/accesscontrollist

accesscontrollist:

# General Section
# topic readwrite #
topic read $SYS/#

# User Section
user airoguard
topic readwrite stat/sonoff_1/RESULT
topic readwrite cmnd/sonoff_1/POWER1
topic readwrite tele/sonoff_1/LWT

# Client Section
#pattern readwrite stat/sonoff_1/%u/#
#pattern readwrite cmnd/sonoff_1/%u/#

My accesscontrollist is much simpler.

user hassuser1
topic #

user homeassistant
topic #

I recently added the “homeassistant” user. Just for good measure. And a vague recollection that a “homeassistant” user was created and used with the (built in?) broker, at one point.

It works fine for me with out it.

Hope it helps.

I think it did.
I know I could not make the acl work - killed everything till I added that user (homeassistant). I can not find ANY mention of that user in the Tasmota Console or in the Broker log either. I do seem to remember errors related to that user before though. Just for shits & giggles, I’m going to edit the acl and delete the homeassistant user, restart the broker and see if it all falls in a heap again…

Yes falls in a heap!

[INFO] found homeassistant on local database
1553741312: Socket error on client <unknown>, disconnecting.
1553741312: |-- getuser(homeassistant) AUTHENTICATED=1 by http
1553741312: New connection from 172.30.32.1 on port 1883.
1553741328: Socket error on client <unknown>, disconnecting.
1553741328: New connection from 172.30.32.1 on port 1883.
1553741328: |-- mosquitto_auth_unpwd_check(homeassistant)
1553741328: |-- ** checking backend http
1553741328: |-- url=http://127.0.0.1:8080/login
1553741328: |-- data=username=homeassistant&password=******&topic=&acc=-1&clientid=
[INFO] found homeassistant on local database
1553741328: |-- getuser(homeassistant) AUTHENTICATED=1 by http
1553741328: Socket error on client <unknown>, disconnecting.
1553741328: New connection from 172.30.32.1 on port 1883.

This is bizzare… where is it pulling that from? (and of course nothing works again)

and in the log now:

[INFO] found mqttuser on Home Assistant
1553741498: |-- getuser(mqttuser) AUTHENTICATED=1 by http
1553741498: New client connected from 10.90.11.22 as DVES_02A43B (c1, k10, u'mqttuser').
1553741498: |-- mosquitto_auth_unpwd_check(mqttuser)
1553741498: |-- ** checking backend http
1553741498: |-- url=http://127.0.0.1:8080/login
1553741498: |-- data=username=mqttuser&password=*****&topic=&acc=-1&clientid=
[INFO] found mqttuser on Home Assistant
1553741498: New client connected from 10.90.11.54 as DVES_684E87 (c1, k10, u'mqttuser').
1553741498: |-- getuser(mqttuser) AUTHENTICATED=1 by http

I have also removed the integration and the broker more times than I care to count. Just checked AGAIN and I have no homeassistant user ANYWHERE I can see. It is so weird. I know homeassistant is not a reserved ‘user’ that you can’t use anymore… I just do NOT understand this at all.

If I remove the Homeassistant user from the ACL it only works if I set active: false…
I would really love to properly understand this.

ok so I’ve simplified accesscontrollist with the same, but no joy.

user airoguard

topic #

user homeassistant

topic #

If I take out

availability_topic: “tele/sonoff_1/LWT”

from configuration.yaml the sonoff switch in the card becomes available but when you turn it on it turns back off in a second or two and not activity on the Sonoff itself.

However log is now showing:

019-03-28 20:35:15 WARNING (MainThread) [homeassistant.components.sensor] Setup of platform ring is taking over 10 seconds.
2019-03-28 20:37:55 WARNING (SyncWorker_15) [homeassistant.components.tplink.switch] Could not read state for 192.168.0.44: Communication error
2019-03-28 20:39:55 WARNING (SyncWorker_5) [homeassistant.components.tplink.switch] Could not read state for 192.168.0.44: Communication error
2019-03-28 20:40:59 WARNING (SyncWorker_10) [homeassistant.components.tplink.switch] Could not read state for 192.168.0.44: Communication error

192.168.0.44 is a separate tp-link switch for the pool. so perhaps something is wrong with these two switches together.

switch:
  - platform: mqtt
    name: "sonoff 1"
    state_topic: “stat/sonoff_1/RESULT”
    value_template: "{{ value_json.POWER }}"
    qos: 0
    command_topic: “cmnd/sonoff_1/POWER”
    payload_on: "ON"
    payload_off: "OFF"
#    availability_topic: “tele/sonoff_1/LWT”
#    payload_available: “Online”
#    payload_not_available: “Offline”
#    retain: true
#    optimistic:

tplink:
  discovery: true
  switch:
    - host: 192.168.0.44

I noticed that you had " retain: true " as a option. Retain should not be used for switches. It causes the issue you are seeing I believe.
Set the switches to " retain: false "

I’m not to sure about this part.
You may need to unset the retain flag for it to work properly.
I’m sorry the procedure on how to unset the flag escapes me.

EDIT: found the thread on how to unset the flag:

Hope its of help.

Brilliant, that worked.
Thanks very much.

Although now I have to clear the retained switch.
Will do some research and look for “unset the retain flag” and come back to this thread.

Easiest way is to get MQTT Explorer and you can delete it in there.

And I learned from troubles with my sonoff config that you should not use the
state_topic.

all my Tasmota’d switch’s have the “state_topic” enabled. I have no problems. In fact most of my Mqtt devices have that line in the config.

Can you please elaborate on that. What did it solve?

Thank you.

I was having troubles with my sonoff switch by mqtt and lolouk44 pointed out to me
that value_template: “{{ value_json.POWER1 }}” was not an option. So I remembered wrong about the state_topic. Sorry for the confusion.

Adding the homeassistant user to /share/mosquitto/accesscontrollist fixed this for me. I guess it makes sense. I’m assuming the homeassistant user is what allows hass to read / write from the topics and the other user(s) are what my devices use to connect.

Regarding the value_json.POWER1 mentioned, I think this just needs to be changed to value_json.POWER. I had this issue with a config I had copied from a post somewhere. Looking at the console of my Sonoff switch showed the right payload.

For anyone else that ends up here withe little hair left, I managed to make mine work after two days of trying pretty much everything.

Hardware: “Mirabella Genio Bulb” from Australian K-Mart - flashed with Tasmota firmware.
Symptoms: Configured MQTT (mosquitto with access lists as above) and the Integration with auto discover on. Clients all connect (confirmed with MQTT explorer) but the homeassistant user won’t.

I can’t even remember were I came across this setting, but once you have started it all and have the symptoms above. Go into the console of the bulb and run this command.

SetOption19 on

Just put it in the command but and hit enter. Instantly my bulbs were discovered and available. Horaay. I feel like the guy in my avatar. I won but I’m a lot balder.