Mosquitto MQTT not working in UI (while it is from the terminal and from Node-RED)

I’m new to Home Assistant: I just installed it and try to add two Shellies (a Shelly 1 and a Shelly Dimmer) via MQTT. I installed Mosquitto and via Node-RED I can see messages coming in and I can turn on and off the light, so MQTT is working. I also added them to configuration.yaml, and they do appear, but the status isn’t shown (always off) and if I try to turn the Shelly 1 on (in developer/status) nothing happens and the gui button switches back to off.

This is my definition in configuration.yaml:

light:
  - platform: mqtt
    name: "Living room stairs"
#    availability_topic: "shellies/shelly1-98F4ABF32644/online"
    state_topic: "shellies/shelly1-98F4ABF32644/relay/0"
    command_topic: "shellies/shelly1-98F4ABF32644/relay/0/command"
    payload_on: "on"
    payload_off: "off"
    payload_available: "true"
    payload_not_available: "false"
    retain: true
  - platform: mqtt
    schema: template
    name: "Living room dimmer"
#    availability_topic: "shellies/shellydimmer-DB3216/online"
    state_topic: "shellies/shellydimmer-DB3216/light/0/status"
    command_topic: "shellies/shellydimmer-DB3216/light/0/set"
    state_template: "{% if value_json.ison %}on{% else %}off{% endif %}"
    command_off_template: '{"turn":"off"}'
    command_on_template: >
        {"turn": "on"
        {%- if brightness is defined -%}, "brightness": {{brightness | float | multiply(0.3882 + 1) | round(0)}}{%- endif -%}}
    brightness_template: "{{ value_json.brightness | float | multiply(2.55) | round(0) }}"
    retain: true

if I send “on” and “off” to shellies/shelly1-98F4ABF32644/relay/0/command via Node-RED it does work.

What could be the issue?

Probably don’t have an mqtt agent installed in home assistant. Node red has its own.

# Example configuration.yaml entry
mqtt:
  broker: IP_ADDRESS_BROKER

Just a guess! I would forget about it!

Thanks for your reply!

I did have that line in configuration.yaml, but I made an error in the IP address. Nevertheless, changing it doesn’t help (Mosquitto’s broker address is the same as the IP address of Home Assistant, 192.168.8.201, if I installed the Mosquitto broker add-in locally, isn’t it?). I also tried removing the entire mqtt section from configuration.yaml and setting broker to localhost, but neither worked.

Can you publish those topics directly from home assistant developer tools >> MQTT

Set the topic and just type “on” or “off” in the field below.

If that works, then the issue is somewhere with the config. If that doesn’t work, it’s with the mqtt agent setup. If your broker is running on the same device, localhost should work fine. Dont’ forget username/password if you configured one.

mqtt:
  broker: localhost
  username: !secret mqtt_user
  password: !secret mqtt_pass

Looking at your config, I’ll only focus on the Living room stairs one for now. You should NOT set the retain flag to true unless you want your light to turn on/off if it disconnects from the broker and reconnects. Idealy, you should set the retain flag to true in the shelly device, and for the state topic only. The shelly should manage its own power on state should the device lose power and reboot. Otherwise, I don’t see anything wrong with it. Because it’s a relay though, it might make more sense to make it an mqtt switch rather than mqtt light. But in this case, it shouldn’t matter…

I have set Mosquitto to anonymous, so if I understand it correctly that means I don’t need to set username and password for mqtt, correct?

This is my Mosquitto configuration:

logins: []
anonymous: true
customize:
  active: true
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

Nothing happens if I publish payload “on” or “off” to “shellies/shelly1-98F4ABF32644/relay/0/command” (broker now set to localhost).

Silly question, but you did restart home assistant after adding the configuration.yaml changes?

Sure I did :slight_smile: And I tried another restart, without much success.

Following the instructions on https://www.home-assistant.io/docs/mqtt/testing/, I did some testing: if I run this in the terminal I do see data coming in:

$ mosquitto_sub -h 192.168.8.201 -v -t  "shellies/#"

and this turns the light on:

$ mosquitto_pub -h 192.168.8.201 -t shellies/shelly1-98F4ABF32644/relay/0/command -m on

However, if I try the same from the Developer Tools, nothing happens (broker now set to 192.168.8.201).

(I renamed the topic, because the problem has clearly nothing to do with the Shellies, but with Mosquitto)

I think you need to set

anonymous: false

and create a user in home assistent

Edit : from the documentation

Known issues and limitations

  • Since version 4.1 of the add-on, an explicit ACL definition is now required if you plan to use legacy logins and "anonymous": true

I created a user with username ‘mqtt’ and password ‘mqtt’, set those credentials in the Shellies, updated the Mosquitto configuration:

logins:
  - username: mqtt
    password: mqtt
anonymous: false
customize:
  active: true
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

I updated the mqtt section in configuration.yaml:

mqtt:
    broker: 192.168.8.201
    username: mqtt
    password: mqtt

I also added the user to accesscontrollist:

user mqtt
topic readwrite #

Now it doesn’t work at all: not in Node-RED (which keeps forgetting username and password), not via the Developer Tools and not via the console, where

$ mosquitto_sub -h 192.168.8.201 -u mqtt -P mqtt -v -t  "shellies/#"

and

$ mosquitto_pub -h 192.168.8.201 -u mqtt -P mqtt -t shellies/shelly1-98F4ABF32644/relay/0/command -m on

both doesn’t do anything.

What am I doing wrong?

The mosquitto add-on is very picky about the configuration. Myself, I never use it, I just install regular mosquitto. But if you use Home Assistant (on HassOS), you can’t.

I tried a fresh installation of Home Assistant, which initially didn’t make any difference. But now I found out what the problem was: I had to set customize.active to true

logins: []
anonymous: true
customize:
  active: true
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

Now everything works :smiley:

Short question:

brightness | float | multiply(0.3882 + 1)

I found this part in another post and it works, but I do not understand what the multiply(0.3882 + 1) is for. Do you?

Nevermind, I figured it out.