Mqtt what am I missing?

So as I am learning about HA I am running into some issues with mqtt switches. If I set up only one switch it works, but if I try to set up two switches only the top ones seems to work.

This is what I have on config.yaml

switch:
platform: mqtt
name: “Luz Cuarto Vale”
command_topic: “cmnd/CuartoVale/power”
state_topic: “stat/CuartoVale/POWER”
qos: 1
payload_on: “ON”
payload_off: “OFF”
retain: true

switch:
platform: mqtt
name: “Luz Cuarto Miriam”
command_topic: “cmnd/CuartoMiriam/power”
state_topic: “stat/CuartoMiriam/POWER”
qos: 1
payload_on: “ON”
payload_off: “OFF”
retain: true

If I delete one, save and reboot, the one left works great. If I have both of them only the one I list first will show up. Since both of them work fine by themselves I don’t think syntax is the issue, but then again what do I know?

Any suggestions?

  1. Please format your post as per the blue banner at the top of the page.

  2. You should only have one switch: block. List multiple switches using the ‘-’ character. e.g.

switch:
  - platform: mqtt
    name: "name 1"
    etc...
  
  - platform: mqtt
    name: "name 2"
    etc...
1 Like

Tom, wow you are quick! I posted since I am about to go to bed and figured I wouldn’t get a reply until tomorrow. Thank you so much. Next time I post code will follow the correct instructions.

thank you so much!

or

switch 1:
   - platform: mqtt
     name: “Luz Cuarto Vale”
     command_topic: “cmnd/CuartoVale/power”
     state_topic: “stat/CuartoVale/POWER”
     qos: 1
     payload_on: “ON”
     payload_off: “OFF”
     retain: true

switch 2:
   - platform: mqtt
     name: “Luz Cuarto Miriam”
     command_topic: “cmnd/CuartoMiriam/power”
     state_topic: “stat/CuartoMiriam/POWER”
     qos: 1
     payload_on: “ON”
     payload_off: “OFF”
     retain: true

Have you actually tried that?

yeah, my whole config file is like that.
works for lights,sensors, media players, zones too.

It’s still under the one switch: block though?

nope

Are you using sonoff switches?

Then the only problem I’d have with recommending that method is that it is not documented and thus support for it may be dropped without warning.

yeah if the ever change it i got allot of copy pasting to do to get it organized again

https://www.home-assistant.io/docs/configuration/devices/

It’s an acceptable option

1 Like

well yesterday I was able to get one switch working with MQTT but was having issues getting the second one to work. Since then, and after about 5 fresh installs I can’t seem to get a single switch to work with mqtt

Does the “etcher” format the SD card each time the hass file is flashed? I basically take sd out of the pi, run etcher on it, then put it back in. Or does it just copy files and might leave some of my bad files there?

On my very first install I was able to add my zoneminder cameras without much of an issue. Today on my last install I am having issues as well. The camaras list but don’t display.

Not sure what to think. Things I got going relatively easy on my previous installs I am having a hard time with now. Not sure what else to blame except maybe the SD card. I tried to remove all partitions with gparted but wasn’t able to unmount some partitions so I just gave up and put it back into etcher.

The top one is working because it’s formatted correctly in the yaml.

The second one is incorrect.

Please review the link to the documentation on adding devices.

Both answers you’ve gotten are correct to fix it.

1 Like

I actually started another mqtt post today. I followed Dr Zzs video first time around, and got one switch to work. Well now, with fresh installs I can’t seem to get it going even when I follow the video step by step as I did the first time

You don’t need to re-flash HA on to the SD card when you have a configuration problem.

Run the configuration validation in the configuration / general menu. If it passes, good, go ahead and restart HA or reload the section you’ve changed (script, group, automation, or core). Ifi it fails, work out what you’ve done wrong and try validating again.

Lurching around reflashing the card isn’t going to help. It’s not working because you have an error in your YAML. You have not yet posted a correctly formatted yaml code so anyone can help you. Start there and stop continually thinking you can do the same thing over and over and get a different result. The answer will present itself simply when you post your code correctly.

I had my original setup of about a week, had added zoneminder, etc. Everything was fine until I did something wrong on the yaml file and I wasn’t verifying my changes. I rebooted and never got the web ui.

I’ve haven’t had much luck in past working with ssh, so basically I didn’t know how to access the web ui again. Didn’t have samba installed either. So figured I could spend hours figuring out how to get back in to fix my file, or just start from scratch. I assumed I could get everything back and going in about 30 minutes.

This is my config for mqtt

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

And this is the mqtt part of my config.yaml file

mqtt:
  broker: 10.0.0.227
  username: mqttuser
  password: mqttUserPW
  
switch:
  platform: mqtt
  name: “Luz Cuarto Vale”
  command_topic: “cmnd/CuartoVale/power”
  state_topic: “stat/CuartoVale/POWER”
  qos: 1
  payload_on: “ON”
  payload_off: “OFF”
  retain: true

See that’s the problem. NEVER reboot. Ever. Really. If you reboot and bypass the config checker, if there is any YAML error, HA won’t start and you get no webUI and you then need ssh to be able to check the logfile for errors and then edit the yaml files to correct.

There is a config checker but it sometimes seems to not pick up errors. When you use Configuration->restart server it won’t restart the server if there are any errors.

Are you using hass.io and the MQTT broker add-on? It looks like you are to me. IF you are, then you can delete the mqtt: section from config yaml as it is no longer needed. You then also add the MQTT integration in Configuration->Integrations.

I also created a Home Assistant User for my MQTT and so my addon configuration looks like this:

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

Those are the bog standard defaults with no username/password defined. I created a user: mqttuser with a password mqttUserPW as per your example and then you don’t need to specify it in the addon and don’t need mqtt: section at all in config yaml. (Unless you have a broker configured some other way or on some other machine and not using the hass.io addon)

Regarding the switch… you don’t need the retain: true and in fact this will screw you over. But not the cause of your current issues. see here https://www.home-assistant.io/components/switch.mqtt/

I believe from the docs you need this:

switch:
  - platform: mqtt

etc.

You COULD also use MQTT discovery and you would then also delete the whole mqtt switch from config as well as mqtt: section.

To do that, you would delete the mqtt: and mqtt switch and restart HA.
Then from the tasmota (assume you use Tasmota) web interface SetOption12 and you will see switch.xxxxxx in the entities which you can customize.

You keep giving us your MQTT settings, but it’s not MQTT that is the problem!

It is your SWITCH configs! Please read the docs.