HA and SwitchModes in Sonoff/Tasmota (sending commands from one Sonoff to another)

Hi

Sorry for newbie question, just working on understanding HA and Sonoff (Tasmota) integration.
Hass.io 0.93 on Raspberry Pi 2 + Sonoff Basic on Tasmota 5.12.0, generic inte

mqtt:
  broker: core-mosquitto
  
# switches
switch:
  - platform: mqtt
    name: "Sonoff Test1"
    command_topic: "cmnd/sonoff-151/power"
    state_topic: "stat/sonoff-151/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
  - platform: mqtt
    name: "Sonoff Test2"
    command_topic: "cmnd/sonoff-152/power"
    state_topic: "stat/sonoff-152/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

I can see Sonoffs in HA and can toggle them via Web interface and exeuting commands (to configure SwithMode, SwitchTopic).

Q1: How to configure HA and Sonoffs (mentioned above) to create scenario

  • sonoff-151 / GPIO14 closed (and released) turns ON sonoff-152 relay
  • sonoff-151 / GPIO14 closed (and released) turns OFF sonoff-152 relay

Q2: How to configure HA and Sonoffs (mentioned above) to create scenario

  • sonoff-151 / GPIO14 double clicked (2 times closed and released) turns ON sonoff-152 and sonoff-152 relay (two at once)

I know it is beginner question.

Is GPIO 14 what you have mapped to the state_topic of each switch ?
Actually I’m going to guess NO.
I just did some searching on tasmota and realised why I like ESPEasy so much. Sheesh !

If you get the tasmota sorted, drop me a line. I’d be happy to help you with the single and double clicks :slight_smile:

In my case switch (single for Sonof Basic) is mapped to relay state, it is not related to GPIO14, every change of relay is published to mosquitto MQTT. I’m able (for now) publish “cmnd/sonof-151/POWER1” from GPIO14 of Sonoff-151 in the proper mode (clik and release) - “SwitchMode 4” and “SwitchTopic sonoff-151” as web console command. GPIO01 and GPIO03 as well (POWER2 and POWER3) but for some reason published twice (left something to tune).

My question is rather related to HA configuration (kind of trigger: … and action: …). I do not know ESPEasy but I think the home logic should to be implemented in HA, not on Sonoff. Except logic related to clicks -
single, double, tripple and long clicks should IMHO on Sonoff FW. So you can help me with this :-).

OK, so assuming GPIO 14 publishes ON to topic cmnd/sonof-151/POWER1 when pressed and OFF when released.

You can create a MQTT binary_sensor to present your pushbutton to HA

In your config.yaml

binary_sensor:
 - platform: mqtt
   name: "Sonoff Test1 Pushbutton"
   state_topic: "cmnd/sonof-151/POWER1"
   payload_on: "ON"
   payload_off: "OFF"

We can then use this in an automation that checks for sequential ‘on’ then ‘off’ events within a timeframe ( 1 second )

- alias: Sonoff Test2 Toggle
  initial_state: true
  trigger:
  - platform: state
    entity_id: binary_sensor.sonoff_test1_pushbutton
    from: 'off'
    to: 'on'
  action:
  - delay: 00:00:01
  - condition: state
    entity_id: binary_sensor.sonoff_test1_pushbutton
    state: 'off'
  - service: switch.toggle
    entity_id: switch.sonoff_test2

If the pushbutton is held on for longer than 1 second, the condition fails and the automation stops.

Many thanks for fast answer.

switch:
  - platform: mqtt
    name: "Sonoff 151"
    command_topic: "cmnd/sonoff-151/power"
    state_topic: "stat/sonoff-151/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
  - platform: mqtt
    name: "Sonoff 152"
    command_topic: "cmnd/sonoff-152/power"
    state_topic: "stat/sonoff-152/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

binary_sensor:
 - platform: mqtt
   name: "Sonoff Test1 Pushbutton"
   state_topic: "cmnd/sonof-151/POWER1"
   payload_on: "ON"
   payload_off: "OFF"
 
automation:
- alias: Sonoff Test2 Toggle
  initial_state: true
  trigger:
  - platform: state
    entity_id: binary_sensor.sonoff_test1_pushbutton
    from: 'off'
    to: 'on'
  action:
  - delay: 00:00:01
  - condition: state
    entity_id: binary_sensor.sonoff_test1_pushbutton
    state: 'off'
  - service: switch.toggle
    entity_id: switch.sonoff_test2

Sonoff-151 is toggling itself, propably taking POWER1 from MQTT just before HA can handle it. I’m not sure the publish that is done by GPIO14 (on Sonoff-151) is properly formed, maybe it has no payload (ON).

Sonoff-151 is configured by

  • Configuration Module: GPIO14 -> 09 Switch1
  • Console commands “SwitchTopic sonoff-151”, “SwitchMode 4”
20:22:22 CMD: gpio
20:22:22 MQT: stat/sonoff-151/RESULT = {"GPIO1":"10 (Switch2)","GPIO3":"11 (Switch3)","GPIO4":"0 (None)","GPIO14":"9 (Switch1)"}

Are you intentionally using ‘off’ lowercased in example (from ‘off’) above ?

Yes. It’s the ‘off’ state of the binary_sensor as opposed to the ‘OFF’ MQTT payload .

Was your sonoff toggling itself before the additions ?

Yes. It is working exactly as before, the only change I can see is in HA where now there is “Sonoff Test2 Toggle”. That is why I expect sonoff-151 has taken the messane for itself before HA.

The message stays on the MQTT server and is visible to any client that subscribes to the topic.

It might pay to install something like MQTT Lens so you can see exactly what is being published and by which device.

I may be adding some confusion here by not understanding how tasmota operates.

Thanks for help, I will dig into this till I do this, or change Sonof firmware :-).

Do you think ESPEasy is easier to configure with HA ? I’m going to implement all logic inside HA, Sonoff FW is for relay switch and 3-4 sensors (switches).

Not necessarily easier, just that I’m familiar with it.
Also it has its own ‘rules’ section where you can build logic.
It can also talk directly with other ESPeasy devices.

Stuck on problem between Tasmota FW and Home Assistant.
Sonoff configured as below

  • Configure Module: GPIO1 as Switch3, GPIO3 as Switch4, GPIO14 as Switch2
  • Console Commands: “SwitchTopic2 sonoff-151”, “SwitchTopic3 sonoff-151”, “SwitchTopic4 sonoff-151”

Closing pins GPIO02, 01 and 14 give me:

22:20:49 MQT: stat/sonoff-151/RESULT = {"Command":"Unknown"}
22:20:53 MQT: cmnd/sonoff-151/POWER2 = ON
22:20:53 MQT: stat/sonoff-151/RESULT = {"Command":"Unknown"}
22:20:53 MQT: cmnd/sonoff-151/POWER3 = ON
22:20:53 MQT: stat/sonoff-151/RESULT = {"Command":"Unknown"}
22:20:54 MQT: cmnd/sonoff-151/POWER4 = ON
22:20:54 MQT: stat/sonoff-151/RESULT = {"Command":"Unknown"}

What is Command Unknown and what is wrong with it (how to fix it) ?

And how to configure Home Assistant to catch POWER2, POWER3 and POWER4 messages to change the state of another relay, for example from sonoff-152 mentioned above ?

I am also a beginner and still learning…

Have you tried switchmode 1 on the gpio switch?
Maybe using switchmode 4 its expecting a toggle command and not “ON” and “OFF”

For the all the gpio connections - if you are using the TX or RX header pins - have you disabled serial logging?

Yes, my fault - complete list of my Sonoff configuration commands is

  • Configure Module: GPIO1 as Switch3, GPIO3 as Switch4, GPIO14 as Switch2
  • Console Commands: “SwitchTopic2 sonoff-151”, “SwitchTopic3 sonoff-151”, “SwitchTopic4 sonoff-151”
  • Console Commands: “SwitchMode1 4”, “SwitchMode2 4”, “SwitchMode3 4”, “SwitchMode4 4”
  • Console Commands: “SerialLog 0”

Maybe try setting one of them to switchmode 1 to see if it works.
Also shouldn’t each gpio switch have a unique topic?

I found the following info very useful:
https://github.com/arendst/Sonoff-Tasmota/wiki/Understanding-SwitchMode-and-SwitchTopic

If I remember it correctly (tested it 2-3 day ago) SwitchMode 1 works fine for default GPIO01 and GPIO14, the only difference is that SwitchMode 1 is using MQTT instead of direct change to relay state. SwitchMode 1 requires holding the button to keep the relay closed, what I want to get is one press to turn on, next one to turn off. Taht is why I need SwitchMode 4 - it is working exactrly as I want.

Also unique topics for switches are working fine, no problem to configure GPIO01 as “cmnd/sonoff-151_switch3”, GPIO03 as “cmnd/sonoff-151_switch4” (or “cmnd/sonoff-151/switch3”, “cmnd/sonoff-151/switch4”), just wanted to have POWER2/3/4 for distinguish them - but maybe you are right becasue I can’t remember “Command Unknow” error in this case. Can’t check this right now, I have to install properly MQTT in Hassbian - there is something wrong in my mosquitto installation. Hass.io for some reason is not stable on Raspbery Pi 2 (hangs after day or two).

Thanks for help and your ideas imsyB :-).

Finally got Sonoff part working fine, the proper command set

  • Configuration -> Configure Module: GPIO14 as “10 Switch”, GPIO1 as “11 Switch3”, GPIO02 as “12 Switch4”
  • Console Commands: SerialLog 0
  • Console Commands: SwitchMode1 2, SwitchMode2 2, SwitchMode3 2, SwitchMode4 2
  • Console Commands: SwitchTopic sonoff-151-SWT

The MQTT published messages are:

21:58:04 MQT: stat/sonoff-151/RESULT = {"POWER":"ON"}
21:58:04 MQT: stat/sonoff-151/POWER = ON
21:58:06 MQT: stat/sonoff-151/RESULT = {"POWER":"OFF"}
21:58:06 MQT: stat/sonoff-151/POWER = OFF
21:58:07 MQT: cmnd/sonoff-151-SWT/POWER2 = ON
21:58:09 MQT: cmnd/sonoff-151-SWT/POWER2 = OFF
21:58:10 MQT: cmnd/sonoff-151-SWT/POWER3 = ON
21:58:10 MQT: cmnd/sonoff-151-SWT/POWER3 = OFF
21:58:11 MQT: cmnd/sonoff-151-SWT/POWER4 = ON
21:58:13 MQT: cmnd/sonoff-151-SWT/POWER4 = OFF

First 4 lines above are related to GPIO00, the other to GPIO14/01/03 as extra three switches.

Finaly changed SwitchMode to 2 because I can (propably) detect long-click using Home Assistant.

My problem was related to using as SwitchTopic “sonoff-151/switch” or “sonoff-151” - as it was default topic closing and releasing GPIOs result was { “Command” : “Unknown” }.