How to disable PIR with Tasmota rule while MQTT unavailable

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 :slight_smile:

Any suggestions how I could get rid of flickering lights during MQTT unavailibility?

I think I found the solution that suits me.

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.

Just for the record:

I had the same issue, that my PIR was switching the relay when my MQTT broker was not available. This fixed it for me:

rule1
rule1 on switch2#state=1 do publish PIR ON endon
rule1 on
1 Like

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.

Hi everyone, I Found the solution !!!

FIRST AND IMPORTANT use last Tasmota (Developement 6.4.1.10) It contain MEMx#State triggers

I use 3 RULES

Rule1 on Mqtt#Disconnected do mem1 off endon

Rule2 on mem1#state=off do backlog rule1 0; rule3 1; gpio3 0 endon on mem1#state=on do backlog rule1 1; rule3 0; gpio3 10 endon

Rule3 on Mqtt#Connected do mem1 on endon

What these 3 rules does :

  • on mqtt disconected it set Mem1 to “off” (rule1)

  • on Mem1=“off” it deactivate rule1, activate rule3 and set gpio3 to 0 (rule2 first part)

  • on mqtt connected it set Mem1 to “on” (rule3)

  • on Mem1=“on” it activate rule1, deactivate rule3 and set gpio3 to 10 (rule2 second part)

tested working

Important :

You have to set rule 1 and 2 on while you are connected to mqtt server a beginning .

Rule1 1

Rule2 1

After that you can disconnect your mqtt server to test and then reconnect it.

You’ll see that few second after disconnection gpio3 is at none
And few seconds after reconnection it’s back to switch10

2 Likes

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.

It still reboot the device on gpio change…

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

Good i had noticed that but in my case some of my PIR are not used for light but to trigger automation in Home Assistant.

does this restarts sonoff? while performing thus rules?

@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)

Hello

These 3 rules does exactly watch you want to perform.

The restart is a consequence of desactivating pir

If your pir is connected to gpio14 change gpio3 in the rule to gpio14

The trick with mem1 is to avoid bootloop

is that typo error or is it right gpio3 to 10 ?

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!