Mqtt and switches

I’m trying to show a switch on my “overview” to turn a sunoff with mqtt, but the switch isn’t shown, what could I be overlooking?

# mqtt Mosquitto
mqtt:
  broker: 192.168.0.5
  username: MQTTuser
  password: mypassword

# switches
switch:
  - platform: mqtt
    name: "switch"
    icon: mdi:lightbulb-on
    command_topic: "cmnd/sonoff8/power"
    state_topic: "stat/sonoff8/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

If you have pressed “take control” for lovelace you need to add it manually. And refresh the page.

I always restart the whole linux server. :slight_smile:

I do not know what you mean with the “take control” for lovelace

Are you using the Hassio addon as MQTT server?

no,

I have Home Assistant (no Hass.io) so I cannot use add-ons

I installed mosquitto as mqtt broker using this thread :

Are you using Mosquitto for other stuff and it works?
Try with a MQTT client to see the messages connecting to the server (MQTTBox is a Chrome Extension).

Have you looked at the states (< >) oage, on the left bottom side, looking for switch.name-of-the-switch ?

I tried connecting with deferent clients and it indeed seems that there is something wrong with my mqtt broker

just curious, but if there is something wrong with the mqtt broker, the whole switch will not show up?

Most probably it’s like this!

Have you tried using the embedded MQTT broker as described here?

You can just give it a try! Of course you have to change also the Tasmota device MQTT config

Found my problem, used the wrong username to connect to mosquitto broker
Now it seems to work, but when I click the switch, the LED on the sonoff blicks (meaning it received the command?) but the relay doesn’t turn on.
I’m almost there, just need to figure out why the sonoff doesn’t go on.

(I also tried sending with MQTTLens, same thing : LED blinks the moment I send “ON” or “OFF”, but the relay doesn’t go on)

Try to add/modify as below

state_on: "ON"
state_off: "OFF"
optimistic: false
qos: 0
retain: false

retain to false will prevent to turn on/off after the command was sent, but not received (for example you send the command at evening and have the switch turn on/off at night after reconnecting).

Doesn’t seem to work.
Will experiment later some more…

What if you toggle from the tasmota web ui?
Try to look at mqtt client what happens and repeat from HA

From the tasmoto web UI, the relay turns on or off. Should I also receive the state on my topic?

You should.
Have you added and modify the switch as pe rmy code?

I’ll show you my code tomorrow.
But what I did see was that mqtt Lens (mqtt client) continuasly loses and reconnect to the mqtt broker.

Try to use the embedded broker

I was under the impression that the embedded broker only worked with HASSIO.

If I was wrong, what are the benefits/dis-benefits over mosquitto?

I’ve been struggling to get mosquitto to work with username and password set when running on my Mac Mini.

I thought the same, but seems that it has also non-HASSIO installation (in Hassio it’s an addon that you have to install and run).

Have you read all the note in the page I linked before?
I read a lot of info regarding mosquiitto installed separately that might be one of your case.

I’m using Hassio and the MQTT addon.

What are you running HassIO on?

I have an Intel NUC with Proxmox as virtualizer, and I have a virtual machine with Ubuntu Server 18.10 with Hassio in Docker