I am new to home assistant and MQTT and seeking some help.
I am trying to use HA and MQTT to turn on and off an LED using an ESP8266. Super basic, just to understand how everything works. I am running hassbian and set up mosquitto on it.
I set up my config file to have an MQTT switch. The switch appears on my HA page but it does not turn the LED on/off. Any advice?
Config file:
`mqtt:
broker: 192.168.1.223
username: my_username
password: my_password
switch:
- platform: mqtt
mqtt_name: "LED"
state_topic: "ha/led"
command_topic: "ha/led"
payload_on: "ON"
payload_off: "OFF"
optimistic: false
qos: 0
retain: true`
Also, here is the arduino IDE code if that helps:
https://pastebin.com/FfaLMXKD
Any and all help for this noobie is greatly appreciated!
your topics look incorrect
stat/ha/led
cmnd/ha/led
I went ahead and made that change, but unfortunately it is still not working
so in config yaml… the mqtt: does not have a ’ in front as you showed here? What broker are you using?
I guess you looked here: https://www.home-assistant.io/components/switch.mqtt/#set-the-state-of-a-device-with-espeasy ?
tom_l
January 9, 2019, 3:06am
5
I’m certainly no expert but looking at the arduino code I don’t think you have a state topic. Your arduino code does not publish the state of the switch. It only receives commands. Your command topics were correct initially.
Try this:
switch:
- platform: mqtt
name: "LED" # see @123’s comment below
command_topic: "ha/led"
payload_on: "ON"
payload_off: "OFF"
optimistic: true
qos: 0
retain: true
I would advise using the mosquitto broker rather than the built in one.
# Home Assistant Add-on: Mosquitto broker
MQTT broker for Home Assistant.
![Supports aarch64 Architecture][aarch64-shield] ![Supports amd64 Architecture][amd64-shield] ![Supports armhf Architecture][armhf-shield] ![Supports armv7 Architecture][armv7-shield] ![Supports i386 Architecture][i386-shield]
## About
You can use this add-on to install Eclipse Mosquitto, which is an open-source (EPL/EDL licensed) message broker that implements the MQTT protocol. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers. For more information, please see [mosquitto].
[aarch64-shield]: https://img.shields.io/badge/aarch64-yes-green.svg
[amd64-shield]: https://img.shields.io/badge/amd64-yes-green.svg
[armhf-shield]: https://img.shields.io/badge/armhf-yes-green.svg
[armv7-shield]: https://img.shields.io/badge/armv7-yes-green.svg
[i386-shield]: https://img.shields.io/badge/i386-yes-green.svg
Make sure you delete the mqtt: section from your configuration file.
Correct, the config file does not have the apostrophe. Something weird with the comment formatting. And I am using mosquitto.
I am using Mosquitto now. I used this guide to install (https://www.youtube.com/watch?v=pBKAjdXUHl4&t=1001s )
123
(Taras)
January 9, 2019, 4:15am
8
GigEmAgs18:
mqtt_name: “LED”
Where in the documentation does it say mqtt_name
is a valid parameter? Seems to me it should be name
, no?
1 Like
finity
January 9, 2019, 1:40pm
9
Also, you might want to change the pastebin to remove the SSID and password to your wifi!
That explains why my switch was displaying the default name and not the specified one, thanks for the catch.
Oh dang! Didn’t even think of that. Thanks for the heads up