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.
image

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
    switches:
      ok:
        command_on: 'curl -k -s  "http://192.168.1.5/web/remotecontrol?command=352"'
        command_off: 'curl -k -s  "http://192.168.1.5/web/remotecontrol?command=353"'
        friendly_name: OK Press

or if it can be controlled by ssh

  - platform: command_line
    switches:
      5_8ghz_wifi_router:
        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

So… this is an old topic… but It is mostly where I am… and it doesn’t help me as much as I need… so I will see if I can keep this going and get some help…

I am a SUPER NOOB with HA and MQTT… I know almost nothing about this…

When you say ( like people are supposed to know )

Here an example for a working Shelly Plug-S as a switch:
and
state_topic: shellies/Plug-s-01/relay/0

where do you get that info? I have a MQTT configured switch… I think that’s the terminology… how do I know what to use in place of ‘shellies/Plug-s-01/relay/0’?

my switch’s log has:

18:43:07.604 MQT: stat/tasmota_E6458D/RESULT = {"POWER":"ON"}
18:43:07.607 MQT: stat/tasmota_E6458D/POWER = ON
18:43:12.707 MQT: stat/tasmota_E6458D/RESULT = {"POWER":"OFF"}
18:43:12.713 MQT: stat/tasmota_E6458D/POWER = OFF
18:44:37.854 MQT: stat/tasmota_E6458D/RESULT = {"POWER":"ON"}
18:44:37.857 MQT: stat/tasmota_E6458D/POWER = ON
18:44:40.227 MQT: stat/tasmota_E6458D/RESULT = {"POWER":"OFF"}
18:44:40.232 MQT: stat/tasmota_E6458D/POWER = OFF

when I press the power button on the physical switch… does any of that map to:
shellies/Plug-s-01/relay/0 ?

  • thanks - jack

You are not providing near enough information.

Provided your switch or light or whatever you are using is connected to the MQTT broker you can either use a MQTT client or via the MQTT integration itself to find what the path is and use that in whatever way you want. Could be a sensor you want to create or a climate or whatever you need.
Personally i have alot more experience with MQTT Explorer and its easier to browse then the integration one.

What version of Tasmota is running ? Do you use the Tasmota integration or the MQTT integration ? SetOption19 0 or SetOption19 1 ?

I made this WAY more complicated that it was… Turns out I didn’t need to know any of this.

I was looking for how to add a MQTT device and stumbled on this page… I saw stuff like:

  - platform: command_line
    switches:
      ok:
        command_on: 'curl -k -s  "http://192.168.1.5/web/remotecontrol?command=352"'
        command_off: 'curl -k -s  "http://192.168.1.5/web/remotecontrol?command=353"'
        friendly_name: OK Press

and assumed that I needed to know what that stuff was… turns out Homeassistant ( in my case ) doesn’t need to know any of that. It ‘discovered’ my MQTT devices and added them automatically or asked me if I wanted to set them up. I just finished flashing a RATGDO Garage Door control module and by the time I flashed it and fired up homeassistant, it has already put:


on my dashboard.

It knew how to setup my teckin sp20 ( flashed with the tasmota software ) too:

So… I was just making this way more harder than it is… sorry about that…