Why is my first switch reverting to OFF seconds after I turn it on? (Multi switch device)

I know, this looks like other topics already posted… and I read those (2 or 3 came up in a search). Most state this is HA not getting the MQTT data back stating the switch is on or off. But I do not think this is the case here.

Here is the setup:

  • HASSIO on a raspi. 0.98.5
  • Several sonoff and ESP8266 devices flashed with tasmota (this one is an ESP8266 in question - 6.6.0(sonoff) core is 2_5_2/2.2.1 - 2 relays on the device, no buttons, switches or sensors defined).
  • I have watched the video on retain, and cleared old flags plus used the buttonon,off steps in the video to clear states, and retain is false.
  • I have been watching the MQTT traffic with explorer, and it looks correct.
  • Switch (POWER2) works. Switch (POWER1) also responds to HA - it will turn on if you ask it from HA, and if you fast-double click the HA switch before it can revert, it will also turn off correctly. But normally it switches on in HA and then reverts after a second, while the switch remains on.

HA states its subscribing to everything i think it should. I have debug MQTT logging enabled. (also found a tasmota using retain and i thought i got em all but… this was a great place to catch that - turn on MQTT debug and restart HA, then see what it subscribes to and if it gets a retained message immediately)

I see the MQTT state is being received by HA for the problem switch. This is the thing that stumps me. If something was not published, I could see HA reverting. But in the logs it shows it DOES get the state:

2019-09-13 11:08:10 DEBUG (MainThread) [homeassistant.components.mqtt] Transmitting message on cmnd/tasmo-2958/power: ON
2019-09-13 11:08:10 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on stat/tasmo-2958/RESULT: b’{“POWER1”:“ON”}’

This is identical to the traffic for the same device, but different switch (which works fine):

2019-09-13 11:09:01 DEBUG (MainThread) [homeassistant.components.mqtt] Transmitting message on cmnd/tasmo-2958/power2: ON
2019-09-13 11:09:02 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on stat/tasmo-2958/RESULT: b’{“POWER2”:“ON”}

Wait… I just noticed something… POWER vs POWER1 vs POWER2 - should that be POWER1 and POWER2? As in POWER does not default to 1? Let me change the device MQTT definition in config to POWER1 instead of just POWER, and restart…

Ah. OK - then when you are doing multiple power relays on one device, make sure you append the number on all of the entries - because even though it is coming to HA, HA is not assuming POWER is the “POWER1” state… which I guess sort of makes sense.

OK - so is this documented somewhere? That you need to add numbers everywhere if you are not using just one relay? I sure missed it. Would be a common mistake if you are used to working with singles all the time and add your first multi-relay device… (this is my 4th ESP8266 custom device, but the first one with more than one relay)

Oh well… so I figured it out, but i’ll post this anyhow in case someone else runs into it. It is not MQTT retention issues at all… it is just something very easy to overlook.

Post your yaml code

Tasmota is easy. You could just have used setoption19 1, and everything would have been configured correctly.

Except, I do not want HA discovering all my Tasmota devices by default and I do not want to have to add excludes to the configuration.yaml. I’m a control freak I guess, and since a lot of my devices are DIY one-offs for sensors or specific purposes I test them constantly before I want them seen elsewhere (by HA for example).

I did try autodiscovery for lots of stuff when I first started using HA, and Unifi was a big mess. :slight_smile: Of course I also had some issues with multiple isolated IoT networks and MQTT not answering on 3 interfaces at once… (I just reverted to a single network, but Unifi rules lock down the IoT stuff.)

The YAML change was simply adding 1 to the command topic:

    command_topic: "cmnd/tasmo-2958/power1"
    state_topic: "stat/tasmo-2958/RESULT"
    value_template: "{{ value_json.POWER1 }}"

Although in trying to figure it out, I did change the value template to something that was RAW vs JSON, moved devices around in order, etc… just to see if it made a difference. It did not. I just needed to specifically tell HA to look at power"1".

I wonder if “power1” works for single relay devices as well? Maybe a best practice is to always use numbers, if you ever expect to do more than one switch or relay per device.

I know I need to use 1/2/3/4 with my sonoff 4 ch pro’s, but with my sonoff basics,sonoff minis, sonoff rf, sonoff pow or Shellie’s I never added a digit.