MQTT switch not working ( Hass.io in docker Mosquitto broker add-on)

Hi,
I am new to HA and MQTT so will be happy to get some help.
I created mqtt switch. All configurations below.
I am trying to check it as in an example on " MQTT Switch" page
When I am sending message as folowing

mosquitto_pub -h 127.0.0.1 -t home/bedroom/switch1 -m "ON"

Switch stays in off state and status is “Unavailable”
I enabled MQTT logging and in log I see:

2019-03-12 23:11:41 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to homeassistant/# 
2019-03-12 23:11:41 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to homeassistant/# 
2019-03-12 23:11:41 DEBUG (MainThread) [homeassistant.components.mqtt] Transmitting message on hass/status: online 
2019-03-12 23:11:41 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to home/bedroom/switch1/available 
2019-03-12 23:11:41 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to home/bedroom/switch1 
2019-03-12 23:11:42 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on home/bedroom/switch1/available: b'ON' 
2019-03-12 23:11:42 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on home/bedroom/switch1: b'ON'

So it seems HA gets the message, but switch still in OFF state.
What I am doing wrong?

I have Intel NUC7i5BNK with Ubuntu 18.04 LTS
hass.io with HA 0.89.1 installed in docker
I installed Mosquitto broker add-on
Configurations as following:

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

configuration.yaml:

mqtt:
  broker: 192.168.0.7
  port: 1883
  client_id: home_assistant_1
  keepalive: 60
  username:  mqttuser
  password:  xxxx
  discovery: true
  discovery_prefix: homeassistant
  birth_message:
    topic: 'hass/status'
    payload: 'online'
  will_message:
    topic: 'hass/status'
    payload: 'offline'
  protocol: 3.1.1
  
logger:
  default: warning
  logs:
    homeassistant.components.mqtt: debug

switch:
  - platform: mqtt
    name: "Bedroom Switch"
    state_topic: "home/bedroom/switch1"
    command_topic: "home/bedroom/switch1/set"
    availability_topic: "home/bedroom/switch1/available"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

The ip address you are using is for the localhost, which may not be correct depending on your docker settings and where you run the command from. Try using the address for the broker you have in the ha configuration file.

Edit:
And a second thing, you may have to send the availablitly topic first, to indicate to HA that the device is available before it will acknowledge the state change. Or just remove the availability line from your config.

    availability_topic: "home/bedroom/switch1/available"
1 Like

What device are you using?

With Tasmota your switch should be set like:

switch:

  - platform: mqtt
    name: "BedroomCurtains"
    state_topic: "stat/wallsocket1/POWER1"
    command_topic: "cmnd/wallsocket1/POWER1"
    availability_topic: "tele/wallsocket1/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

No, if you use Tasmota, you don’t need to do anything in homeassistant except set mqtt discovery on.

1 Like

What is a command to send availability_topic without a message?
I mean

mosquitto_pub -h 127.0.0.1 -t home/bedroom/switch1 -m "ON"

Here the topic is home/bedroom/switch1 and message is “ON”
If I want to send availability_topic this way not working:

mosquitto_pub -h 127.0.0.1 -t home/bedroom/switch1/available

So what is the right way to send availability_topic?

No devices yet. I only want to understand how MQTT works and see that I configured all the right way.

Send the message ‘online’ or ‘offline’ to the availability topic. See https://www.home-assistant.io/components/switch.mqtt/#availability_topic

1 Like

This worked. Thank you.

You need to add the payload with the -m parameter. The value of this is defined by the payload_available setting in your HA configuration. Since you haven’t defined that, it is the default of online. So try

mosquitto_pub -t home/bedroom/switch1/available -m "online" 

which should make the switch available, then

mosquitto_pub -t home/bedroom/switch1 -m "ON"

should change the state to on.

1 Like

I am new to miqtt but use HA on an intel NUC using docker. How do I install the miqtt broker in this setup? Do I need to fire up a seperate docker container with the broker in it?

I would just install mosquitto using apt install.

I prefer Docker for any service. I want as few services as possible on the host.

1 Like

That is a valid view too :slight_smile: