MQTT Switches Error

Hi!
I need some help adding two MQTT switches.

It was everything working when using just one MQTT switch.

switch 4:
  platform: mqtt
  name: "Living Room"
  state_topic: "home/sonoff/living_room/1/stat"
  command_topic: "home/sonoff/living_room/1"
  qos: 1
  payload_on: "on"
  payload_off: "off"
  retain: true

So I just copy and paste to adding another one… like this:

switch 4:
  platform: mqtt
  name: "Living Room"
  state_topic: "home/sonoff/living_room/1/stat"
  command_topic: "home/sonoff/living_room/1"
  qos: 1
  payload_on: "on"
  payload_off: "off"
  retain: true

switch 5:
  platform: mqtt
  name: “Bebedouro"
  state_topic: "home/sonoff/bebedouro/1/stat"
  command_topic: "home/sonoff/bebedouro/1”
  qos: 1
  payload_on: "on"
  payload_off: "off"
  retain: true

But got this:

2018-01-14 01:58:13 ERROR (Thread-2) [homeassistant.util.yaml] while parsing a block mapping
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 250, column 3
expected , but found ‘’
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 255, column 16
2018-01-14 01:58:13 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 250, column 3
expected , but found ‘’
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 255, column 16

Line 250 is the second switch platform line and line 255 is the payload_on line.

How could I config two MQTT switches?
Running 0.54 version…

Thanks

Try this

switch 4:
  - platform: mqtt
    name: "Living Room"
    state_topic: "home/sonoff/living_room/1/stat"
    command_topic: "home/sonoff/living_room/1"
    qos: 1
    payload_on: "on"
    payload_off: "off"
    retain: true

  - platform: mqtt
    name: “Bebedouro"
    state_topic: "home/sonoff/bebedouro/1/stat"
    command_topic: "home/sonoff/bebedouro/1”
    qos: 1
    payload_on: "on"
    payload_off: "off"
    retain: true

Thanks @jivesinger.

Tried this before without the empty line between both.
I get the same error:

2018-01-14 09:04:10 ERROR (Thread-2) [homeassistant.util.yaml] while parsing a block mapping
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 249, column 5
expected , but found ‘’
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 254, column 18
2018-01-14 09:04:12 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 249, column 5
expected , but found ‘’
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 254, column 18

Right ! I think I found the issue… :slight_smile:

In the second switch, the double quote at the start of the name and at the end of the command topic, are not the same as the others.
Replace them with standard double quotes, or get rid of all of them and replace them with singles, and you should be good to go

2 Likes

WOW
Perfect…

How those double quotes appeared there???
and why do they exists btw!!!

Thanks very much

I have no clue, maybe they got sleepy :wink:

You are probably using a word processor to create your files instead of a text editor.
Word processors are really good at replacing quote characters with fancy open and close quotes.

I’m having a similar issue with the configuration; I retyped the values and changed the quotes to single but I keep getting this error.

- platform: mqtt
    switches:
      sonoff_1:
        name: "Sonoff Switch 1"
        command_topic: "cmnd/sonoff_1/power"
        state_topic: "stat/sonoff_1/POWER"
        payload_on: "ON"
        payload_off: "OFF"
        state_on: "ON"
        state_off: "OFF"
        optimistic: false
        qos: 0
        retain: false

With this I get the error…

Invalid config for [switch.mqtt]: required key not provided @ data['command_topic']. 
Got None. (See ?, line ?).

This should be what you need:

switch:
  - platform: mqtt
    name: "Sonoff Switch 1"
    command_topic: "cmnd/sonoff_1/POWER1"
    state_topic: "stat/sonoff_1/POWER1"
    availability_topic: "tele/sonoff_1/LWT"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: false

@TheNotSoSmartHome Thanks for the quick response - I need to add more switches and I thought this format was to be used - please correct me if I’m wrong…

My config is in switches.yaml - I tried this too

- platform: rpi_rf
  gpio: 17
  switches:
    test_light:
    protocol: 1
      pulselength: 162
      code_on: 1332531
      code_off: 1332540
      signal_repetitions: 10

- platform: mqtt
    name: "Sonoff1"
    command_topic: "cmnd/sonoff_1/POWER"
    state_topic: "stat/sonoff_1/POWER"
    availability_topic: "tele/sonoff_1/LWT"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: false

@atlterry

This is from my switches.yaml file called in by switch: !include switches.yaml in the configuration.yaml file.

#switches.yaml:
#switch:
  - platform: mqtt
    name: "Lab Bench Outlet 1"
    state_topic: "stat/labbench/POWER1"
    command_topic: "cmnd/labbench/POWER1"
    availability_topic: "tele/labbench/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true
  - platform: mqtt
    name: "Lab Bench Outlet 2"
    state_topic: "stat/labbench/POWER2"
    command_topic: "cmnd/labbench/POWER2"
    availability_topic: "tele/labbench/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true
  - platform: mqtt
    name: "Lab Bench Outlet 3"
    state_topic: "stat/labbench/POWER3"
    command_topic: "cmnd/labbench/POWER3"
    availability_topic: "tele/labbench/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true
  - platform: mqtt
    name: "Lab Bench Outlet 4"
    state_topic: "stat/labbench/POWER4"
    command_topic: "cmnd/labbench/POWER4"
    availability_topic: "tele/labbench/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true

Your file changes indent which could be causing you error. Try this:

#switch:
  - platform: rpi_rf
    gpio: 17
    switches:
      test_light:
      protocol: 1
        pulselength: 162
        code_on: 1332531
        code_off: 1332540
        signal_repetitions: 10

  - platform: mqtt
    name: "Sonoff1"
    command_topic: "cmnd/sonoff_1/POWER"
    state_topic: "stat/sonoff_1/POWER"
    availability_topic: "tele/sonoff_1/LWT"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: false

Also, double check the naming of the command and topic commands. I have flashed my devices with Tasmota and note that my command topic have a number at the end.

@TheNotSoSmartHome Thanks again … I was having issues with mosquitto and resolved them - tasmota works fine via the web interface but I can;t get HA mqtt messages to work via the HA interface.

I deleted the the rf switch entry and now there are no config errors… here are some screen shots.
Would appreciate some help.
Thanks

Has the Sonoff connected to the broker? Your Sonoff log show connect failed.

Example from one of my devices:

09:21:07 MQT: Attempting connection...
09:21:07 MQT: Connected
09:21:07 MQT: tele/br2-fan/LWT = Online (retained)
09:21:07 MQT: cmnd/br2-fan/POWER = 

If the sonoff is not connected then the sonoff cannot receive any messages from the broker or HA.

Double check your username and password settings for the broker/sonoff.

@TheNotSoSmartHome the log told me that Tasmota was not alive and it turned out I had not made an entry in the service config to start tasmota before HA.

Thank you very much for your help. Next - adding more sonoffs to replace my X10’s - this is turning out to be a real fun experience

This is strange - the log shows “Switch1” for SENSOR

The only time the sonoff shows in the UI is with these settings and it’s always unavailable

My switches.yaml

- platform: mqtt
  name: "sonoff_1"
  command_topic: "cmnd/sonoff_1/POWER"
  state_topic: "stat/sonoff_1/RESULT"
  availability_topic: "tele/sonoff_1/LWT"
  payload_on: "ON"
  payload_off: "OFF"
  state_on: "ON"
  state_off: "OFF"
  optimistic: false
  qos: 0
  retain: true

My groups.yaml:

first_floor_group:
name: 1st Floor
entities:
   - light.linear_lb60z1_dimmable_led_light_bulb_level
   - switch.sonoff_1

Any suggestions?

Thanks

The only thought I have is what is the name of the device?

I do not know how HA will map name: "sonoff_1" as I know name: "Lab Bench Outlet 1" will map to switch.lab_bench_outlet_1. You can check this in the Devloper section on the button that looks like < >

Just to double check, both HA and the sonoff are connected to the broker correct? I previously noted that the sonoff could not connect to the broker and I don’t understand this:

What are you running HA on and which broker are you using? Do you have the logs from the broker?

@TheNotSoSmartHome my entity id showed up as switch.sonoff_1 and no matter what I did it showed unavailable so I used “Sonoff Switch 1” and it now shows up as sonoff_switch_1 and works fine. I added the

payload_available: "Online"
payload_unavailable: "Offline"

Thanks for sticking it out and helping. Cheers!