Background
I have Sonoff Basic flashed with Tasmota firmware with togglable wall switch connected as GPIO14 Switch1 and PIR sensor attached as GPIO3 Switch2. SwitchTopic is set to 1 so that PIR does not control relay directly but via MQTT instead.
Issue outline
Problem appears when Sonoff cannot access MQTT broker (usually during Hassio reboot in my case) - light is constantly flashing - PIR starts to control relay directly. This is due to the Tasmota fallback behaviour that sets SwitchTopic to 0 when MQTT broken is not available.
IMHO PIR Switch2 should not affect relay directy but according to this issue that is how it is designed and only option is to configure something via Tasmota rules.
Only way I could think of was to disable PIR for the period while MQTT unavailable a la
rule1 on Mqtt#Disconnected do backlog gpio3 0 endon on Mqtt#Connected do backlog gpio3 10 endon
Unfortunately changing gpios involves Sonoff reboot and which causes bootloop with this rule e.g. upon startup Mqtt#Connected happens which sets gpio3 10 and reboots -> upon startup Mqtt#Connected happens which sets gpio3 10 and reboots -> and party goes on
Any suggestions how I could get rid of flickering lights during MQTT unavailibility?
I defined relay2 (command gpio4 22) prevent it from affecting relay1 directly when SwitchTopic 0.
And added a rule that holds logic how PIR controls relay1 in addition to wall switch:
rule1 on switch2#state=1 do backlog power1 on endon on switch2#state=0 do ruletimer1 300 endon on rules#timer=1 do power1 off endon
While I am not too happy store part of the logic elsewhere except Home Assistant, in this case there are huge benefits:
it works faster then via MQTT;
it fully works even if MQTT broker is unavailable at some moment.
During the time my rule for controlling PIR has evolved to this:
on switch2#state=1 do backlog power1 on; ruletimer1 10; publish stat/sonoff10/motion %value% endon on switch2#state=0 do delay 1 endon on rules#timer=1 do backlog ruletimer2 300; publish stat/sonoff10/motion 0 endon on rules#timer=2 do power1 off endon
Benefits:
no need to define relay2 which allows to use Wemo emulation (can handle only single relay);
motion state is reported via MQTT to Home Assistant and state is not “flickering”;
logic is on board itself making behaviour faster and works even when MQTT is unavailable.
I am curious doesn’t set gpio automatically invoke restart anymore?
It was like that some time ago so if that is still true, then you can end up in boot loop.
BUT since before reboot I deactivate rule1 and activate rule 3 I avoid that boot loop…
Because the device is waiting for mqtt reconnection only
After reconnection I reactivate rule 1 and deactivate rule3 so the device is waiting only for mqtt loss
Actually you don’t have to disable PIR while MQTT broker not available - PIR can be still on and work as expected with rule posted above - you can simplify that rule if you don’t want to report motion.
Something like that will turn off your light 5 minutes after last motion and MQTT broker connectivity will have no effect on relay:
on switch2#state=1 do backlog power1 on; ruletimer1 300 endon on switch2#state=0 do delay endon on rules#timer=1 do power1 off endon
@pplante@inutilis will someone help me out in this i’m new to tasmota? what i’m trying to do is
1… unlink PIR which is connected to GPIO14 with relay
2… PIR should only send MQTT if sonoff basic is connected to mqtt broker and do nothing when mqtt is not connected
3… sonoff can’t be restarted in rules because i have PowerOnState 1 (which will turn on relay)
Hi! I have setting a generic module with 2 switch and one relay. Switch1 on relay 1 and switch 2 indipendent. She work, but when I turn off the server mqtt, relay 2 work on relay 1. Can you Help me!
Here is my solution to this. What you have here I think is too complicated, involves a lot of memory writing, and prone to failure due to getting stuck in wierd modes if the power goes out.
I split the relay configuration, added a fake relay, and had the PIR swirch activate that fake relay. Look at the template, you will see what I mean.
See Sonoff Slampher and Sonoff PIR Integration
I used a slampher for the relay device, but any RF activated sonoff would work much the same way.
Also see Sonoff Slampher and PIR Coordination of 2 Separate Units for a more involved 2 relay 2 PIR configuration I have on my basement stairs.
Fantastic solution, works like a charm.
I have 2 PIR on a sonoff, respectively on GPIO1 and GPIO3, and set your 3 rules, however switching gpio3 to 10 (my switch2) and gpio1 to 11 (my switch3).
Thank you for sharing!