Mqtt how to add a switch

Okay so, I don’t quiet understand the process of setting a switch as an mqtt. Can any switch be an mqtt? I have one connected to my wifi, it has it’s own IP and all but I don’t know how to make it show on my mqtt explorer to see which topic I need to listen to.

I have setup the mosquitto on my hassio and I added a switch to conf.yaml file as shown below.

But still when I try to read a symple node red debugger it shows that there are no mqtt devices. Could someone help me solve this

Only switches that can be set to send out MQTT message can be used.

On sonoff devices on the webpage on the device you need to enter the details of your MQTT server. The MQTT device is then recognized by HA. You may have to restart.

If your device cant be controlled by MQTT but by http then below is an example

  - platform: command_line
        command_on: 'curl -k -s  ""'
        command_off: 'curl -k -s  ""'
        friendly_name: OK Press

or if it can be controlled by ssh

  - platform: command_line
        command_on: "ssh -i /config/ssh_keys/router -o 'StrictHostKeyChecking=no' [email protected] 'uci set wireless.radio0.disabled=0; uci commit wireless; wifi reload'"
        command_off: "ssh -i /config/ssh_keys/router -o 'StrictHostKeyChecking=no' [email protected] 'uci set wireless.radio0.disabled=1; uci commit wireless; wifi reload'"
        command_state: "ssh -i /config/ssh_keys/router -o 'StrictHostKeyChecking=no' [email protected] 'uci get wireless.radio0.disabled'"
        value_template: "{{ value == '0' }}"
1 Like

The switch must have MQTT capability and set as a client to the MQTT broker. Othewise You can’t find it in Your broker.

Here an example for a working Shelly Plug-S as a switch:

- platform: mqtt
  name: MQTT Plug-01 switch
  unique_id: MQTT Plug-01 switch
  payload_on: "on"
  payload_off: "off"
  state_topic: shellies/Plug-s-01/relay/0
  command_topic: shellies/Plug-s-01/relay/0/command
1 Like