Unable to create Mqtt switch in home assistant

Hi,
I just installed HA on my raspberry pi. I want to create a mqtt switch on HA to control my lights etc. I am using raspberry pi with mqtt broker installed on it. I am also using esp8266 wifi module that actually control the lights through the relay attached to it and that wifi module is connected to mqtt host.
I am unable to create mqtt switch on HA. It says configuration error in mqtt and switch. I used this in the configuration.yaml file

mqtt:
broker:192.168.18.140
port:1883
client_id:homeassistant-1
keepalive:60

switch 1:
platform:mqtt
name:“Light”
state_topic:“ledtoggle”
payload_on:“a”
payload_off:“b”
qos:0

Hi,
Can you use the code blocks (see the banner at the top), then we can see the spacing (it’s very important for yaml)

cheers

mqtt should look like this…

mqtt:
  broker: 192.168.1.7
  port: 1883
  client_id: home-assistant
  username: !secret mqtt_usern
  password: !secret mqtt_pw
  keepalive: 60
  protocol: 3.1.1

and the switch…

- platform: mqtt
  name: bathroom_light
  state_topic:   "stat/bathroomPIR"
  command_topic: "cmnd/bathroomPIR"
  qos: 1
  payload_on: "on"
  payload_off: "off"
  retain: true 
  optimistic: false

Note where the spaces are.
My switch is in a separate yaml file so doesn’t have the switch at the top.

1 Like

Hi @keithh666
here is the code for mqtt and switch and the errors i got are below the code.

 mqtt:
   broker:192.168.18.140
   port:1883
   client_id:homeassistant-1
   keepalive:60


switch:
  - platform:mqtt
    name:"Light"
    command_topic:"ledtoggle"
    payload_on:"a"
    payload_off:"b" 
    qos:0

and the errors i got are the following:

Configuration invalid.CHECK CONFIG
Testing configuration at /home/pi/.homeassistant
17-03-05 22:41:58 WARNING (MainThread) [homeassistant.config] Incomplete core config. Auto detected latitude: 11.1111, longitude: 45.3333, elevation: 216
17-03-05 22:41:59 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [mqtt]: expected a dictionary for dictionary value @ data[‘mqtt’]. Got ‘broker:192.168.18.140 port:1883 client_id:homeassistant-1 keepalive:60’. (See ?, line ?). Please check the docs at https://home-assistant.io/components/mqtt/
17-03-05 22:41:59 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [switch]: expected a dictionary. Got OrderedDict([(‘homeassistant’, OrderedDict([(‘name’, ‘Home’), (‘unit_system’, ‘metric’), (‘time_zone’, ‘ssssss/ssd’)])), (‘frontend’, {}), (‘config’, {}), (‘http’, OrderedDict([(‘api_password’, ‘mypassword’)])), (‘updater’, {}), (‘conversation’, {}), (‘history’, {}), (‘logbook’, {}), (‘sun’, {}), (‘sensor’, OrderedDict([(‘platform’, ‘yr’)])), (‘tts’, OrderedDict([(‘platform’, ‘google’)])), (‘mqtt’, ‘broker:192.168.18.140 port:1883 client_id:homeassistant-1 keepalive:60’), (‘switch’, ['platfo… (See /home/pi/.homeassistant/configuration.yaml, line 69). Please check the docs at https://home-assistant.io/components/switch/
Failed config
mqtt:
b
r
o
k
e
r
:
1
9
2
.
1
6
8
.
1
8
.
1
4
0

  p
  o
  r
  t
  :
  1
  8
  8
  3
   
  c
  l
  i
  e
  n
  t
  _
  i
  d
  :
  h
  o
  m
  e
  a
  s
  s
  i
  s
  t
  a
  n
  t
  -
  1
   
  k
  e
  e
  p
  a
  l
  i
  v
  e
  :
  6
  0

switch:
- platform:mqtt name:Light command_topic:“ledtoggle” payload_on:“a” payload_off:“b” qos:0

Successful config (partial)
mqtt:
switch:This text will be hidden

I think you just need to add some spaces, look carefully at mine and compare with yours…

  broker: 192.168.1.7
         ^
  port: 1883
       ^  etc......
  client_id: home-assistant
  username: !secret mqtt_usern
  password: !secret mqtt_pw
  keepalive: 60
  protocol: 3.1.1

See if that helps.
Also make sure you save the files as utf8 format.

@keithh666

thanks alot its working now