Tasmota Switch Topics - Driving me crazy!

I’m pretty sure there is an answer to my question somewhere, but after going in circles for the past 4 hours reading forums and wikis I’m hoping that someone can point me in the right direction.

NB: I’m very new to both Home Assistant and Tasmota.

My goal is actually very simple:
I would like to use 4 of the GPIOs as remote switches to trigger automations within Home Assistant.
Hardware: NodeMCU ESP8266

MY PROBLEM:
I cannot seem to get Tasmota to send a unique MQTT string for each of the 4 switches.
Each of the 4 GPIO inputs have been configured in the UI as a separate switch - i.e. Switch1, Switch2, Switch3, and Switch4.
When grounding any one of the GPIOs, the MQTT message sent is identical, no matter which switch is activated. e.g.
17:28:20 MQT: stat/NMCU001/RESULT = {“POWER”:“ON”}
17:28:20 MQT: stat/NMCU001/POWER = ON

The same identical message is sent for each and every switch.

QUESTION:
How do I get each switch to send a unique MQTT message to Home Assistant?
For example when grounding a switch:
Switch1: stat/NMCU001/POWER1 = ON
Switch2: stat/NMCU001/POWER2 = ON
Switch3: stat/NMCU001/POWER3 = ON
Switch4: stat/NMCU001/POWER4 = ON

Note, there are NO relays connected to the NodeMCU.

ANY help greatly apprciated!

Hmm… looking at the code, if you had at least one relay you would be able to use the
SetOption26 1 command to force including the number in the power topic. However, I’m not sure if it actually supports having seperate power states if there are not enough lights/relays. In any case, that option does literally nothing if you don’t have at least one light/relay. One option is to add virtual relays, but that is probably not ideal.

One simple but probably not desirable option that avoids virtual relays is to use SwitchTopic# sometopic (replace # with the switch number) to make it send messages to cmnd/sometopic/POWER. By using a different topic per switch you could distinguish the switches. This is not quite as nice as the next solution.

For best results you want to use the rules feature to completely override the default behavior. For example to get exactly the behavior described above, you can send the following sequence of commands:

Rule1 on Switch1#state do Publish stat/NMCU001/POWER1 %value% endon
Rule1 + on Switch2#state do Publish stat/NMCU001/POWER2 %value% endon
Rule1 + on Switch3#state do Publish stat/NMCU001/POWER3 %value% endon
Rule1 + on Switch4#state do Publish stat/NMCU001/POWER4 %value% endon
Rule1 1

If doing something other than basic relay setup, many people around here prefer to use ESPHome as their custom firmware instead of Tasmota, as ESPHome completely avoids these sorts of undesired default behaviors. The downside is that ESPHome requires YAML configuration, rather than configuration via an on device web GUI.

Awesome - thank you so much Kevin!

I’m not yet comfortable with yaml so have gone with the rule setup on Tasmota for now - working 100%

Awesome - thank you so much Kevin!

I’m not yet comfortable with yaml so have gone with the rule setup on Tasmota for now .

**I’m glad you raised the setoption26 option as I also need something similar in a scenario with 1 relay, but 2 switches - neither being linked to the relay on the NodeMCU. It does however not seem to be working - am I missing something in the default config - such as a switchmode setting? **

This is my setup with 2 switches and 1 relay:

As you can see below, both switch 1 and 2 refer to POWER1 instead of POWER1 & POWER2 respectively. They also both trigger the relay.

image

As before, the help is much appreciated.

Yeah, I thought that might happen even with Option26: “However, I’m not sure if it actually supports having seperate power states if there are not enough lights/relays.” Option26 ensures power state updates always include the number, but it does not magically ensure there are as many of them as you might want.

But it looks like it it only creates power states for the number of relays that exist, (with a minimum of one, if no relays exist). Thus it ends up mapping the later switches to earlier power states. For this scenario, I would be tempted to just add a rule for the second switch, while letting the first one control the relay’s power state as normal.

The other option is to add a virtual relay, which is simply configuring a relay on a GPIO pin that nothing is actually connected to.

Looks like I’ll have to get up to scratch on yaml sooner than later… Thanks Kevin, will stick to using rules for now.

I would just add 4 Relays and 4 switches

Even the tho the Relays do nothing but it switches will do all the work

and each time the switch get change it will change the relay which will send the MQTT Message

image