Controlling a step relay

Hi there,
I would like to prompt you with this issue.
by using a sonoff pro I would like to activate a step relay controlling a light bulb.
This mean that the sono will give a single pulse to the relay that will alternatively switch between on and off.

the sonoff is obviously talking with HA. The point is that when I send the puls to the relay, HA see correctly the pulse. This mean that for HA the sonoff-light is on and then off, but the light bulb is ON after the first pulse and off aftera secondo pulse.

Is there a way to say to HA that the status of the bulb change after a single pulse from the sonoff??
thanks in advance

Want to give some more specifics?
Maybe some HA code or sonoff configuration?
Relay model?
Is the sonoff hacked?


no coding yet.

sonoff running tasmota.
relay is this one (manufacturer : bticino model: fp2a/24)
mqtt broker on running on a rpi3

I think the point is to “teach” HA that when he receive a pulse the first time from that specific sonoff, means stay ON, the second pulse means stay OFF and thant again, the third pulse will be stay ON, the 4th pulse stay off.

I found this exciting video.

That’s part of the solution. pushbuttons to control the sonoff.
Then the sonoff will controll the relay,
HA needs to know whther the light is on or off.

I would do it on the sonoff and not with HA as mqtt / wifi is a little slow sometimes.

How about creating a rule on the sonoff that shuts off (toggle) the sonoff and sends alternating mqtt topics.

  1. Execute several commands when a Timer expires
    The default Timer1…16 functionality allows for controlling one output to either off, on, toggle or blink. When rules are enabled the blink option will be replaced by rule functionality allowing much more flexibility.


Sonoff 4CH

Sonoff-Tasmota v5.12.0l or up with define USE_RULES enabled
Configure timer5 for rule execution when activated: timer5 {“Arm”:1,“Mode”:0,“Time”:“16:00”,“Days”:“1111111”,“Repeat”:1,“Action”:3}

on clock#timer=5 do backlog power2 on;power1 off;power3 2 endon

When the timer expires the rule kicks in and set Power1 to OFF, Power2 to ON and Toggles Power3
If you want to have blink functionality define a rule like on clock#timer=5 do power 3 endon

Thanks for the hint.
I need to study badly.

Once again I want to ask if there is a way to keep on the HA switch (that controll the relay) when sonoff gives a ON-and-then-OFF istruction.

Hi there, I think the key word here is " sends alternate mqtt topics".

How to do it?

I was also thinking about counting the numebrs of time a button is pressed. If it’s a odd number the status must be ON if it’s a even number the status must be off. So is there away to correlate the number of time a button is pressed and the status message that the sonoff send to the mqtt brocker?

some small upgrade.
firste let’s forget about the mqtt approach.

i create an automation that add 1 to a counter component (if its numeric state is 0) when the sonoff goes from off to on (or to on to off, because the pulse off on off) and another automation that subtract 1 to the counter when the sonoff is pulsed and the counter numeric status is 1.

Basically the counter is reporting the state of the bulb. first puls = 1 = on, second pulse =0 =off.

the numeric state of the counter can activate another template switch wich could reflect the state of the bulb.

all of this mean that when ypu turno on or off the light from the real button everything is ok.

But here is the problem: the template switch wich is mirroring the state of the counter and cosequently of the bulb, is programmed to turn on/ turn off the sonoff switch (pulse). so basically I follow into a neverending loop.
the point is that there is only one actuator (the sonoff button) that in my programming act as a source of imput and at the same time as actuator of imputs from another source (tempate switch). this mean that activating the sonoff by the template switch start a loop.
how to break the loop?

finally solved.
the solution is not very elegant but it’s working.
it’s a complicated solution for an easy task.

real electronic: bulb controled by 3 pushbutton activating an intching relay.

entity used:

  • mqtt switch on a tasmotized sonoff. pulsetime 5 (0.5s). when you turni it on, it satys on for 0.5s and then turn off. (this because it’s controlling a real relay.) The sonoff will be controlled by existing pushbutton.
  • input boolean this will be the real light switch on the frontend in hassio.

I then created 4 different automation:

1. UI_ON: when turning on the light from ha (input boolean switch) first turn off all the following automation then activate the mqtt switch (that turn on the light) and then turn ON the automation that was turned off.

2. UI_OFF: when turning off the light from ha (input boolean) first turn off all theother automation then activate the mqtt switch (that turn off the light) and then turn ON the automation that was turned off.

3. PUSHBUTTON_ON: (this is to turn on the light from the real button on the sonoff) when pressed first turn off all the automations, than switch ON the input boolean, then turn ON the automation. In this way I can reflect the status of the bulb on ha.

4. PUSHBUTTON _OFF: (this is to turn off the light from the real button on the sonoff) when pressed first turn off al the automations, than switch OFF the input boolean, then turn ON the automation. In this way I can reflect the status of the bulb on ha.

that’s it. the point was to turn off the automation to avoid loops.
I’m still serching a more elegant solution because for every bulb i need 4 automation and 2 switch (mqtt and a boolean).
Hope this can be helpful (and clear).