Rebooting a router automatically by sending a keepalive to a Tasmota switch

1,693 views
Skip to first unread message

Pila

unread,
Nov 27, 2019, 8:29:48 AM11/27/19
to sonof...@googlegroups.com
I want Tasmota Sonoff 4ch to automatically restart the router should something go wrong. Program running at my router is checking all is well every 5 minutes. If my program works, everything is 100% under control. But if for whatever reason it gets stuck (routers had a memory leak with one firmware taking 4-6 months to develop a problem)...

I need Tasmota to monitor if my monitoring program works. The Wifi#Disconnected rule or delegating a decision to a third device may fail to correctly identify the problem. Since I just started reading on Rules, I can not write exact commands here, but an idea.

My program from the router can somehow directly reset (http) the timerX on the connected Sonoff every 5 minutes. A rule at that Sonoff would check at intervals, say Time#Minute|15, if the timerX is being older than 15 minutes, and if so, cycle the Power to the router (backlog Power2 off; sleep 15; Power2 on).

I believe that is possible to do. My question is: Should I expect stability problems with the Tasmota if I keep reseting the timerX on the Sonoff with a http command every 5 minutes from my router? Sonoff itself is on an UPS and is basically never restarted. Or this is a viable idea?

My idea is something like this:

Router sends every 5 minutes http command to set:

RuleTimer1 900

and in the Tasmota, I would do something like:

Rule3 On Rules#Timer=1 Do backlog Power2 off; sleep 15; Power2 on
Rule3 1

As I said, I just started reading on Rules, this above code may be way off.

Michael Ingraham

unread,
Nov 27, 2019, 8:45:37 AM11/27/19
to TasmotaUsers
My recommendation would be to have your router send an `Event` command as a heartbeat and a `RuleTimer` rather than a Timer.

On the Tasmota device:
  • Use the Event to trigger a Rule. The rule resets the `RuleTimer`
  • A second rule is triggered when the Rule#Timer expires (the first rule was not triggered by the heartbeat). This rule executes `Backlog Power1 OFF; Delay 20; Power1 ON` (for a 2 second delay.

There should be no instability caused by sending a message every 5 minutes to trigger a rule.

Mike

Pila

unread,
Nov 27, 2019, 9:17:47 AM11/27/19
to TasmotaUsers

OK, thanks, I am off trying to make this work. So far, I am not triggering the rule by RuleTimer sucessfully. Oh, and I corrected my misuse of a shell sleep command in my original example :)

My recommendation would be to have your router send an `Event` command as a heartbeat and a `RuleTimer` rather than a Timer.

On the Tasmota device:
  • Use the Event to trigger a Rule. The rule resets the `RuleTimer`
  • A second rule is triggered when the Rule#Timer expires (the first rule was not triggered by the heartbeat). This rule executes `Backlog Power1 OFF; Delay 20; Power1 ON` (for a 2 second delay.


OK, that is the most important here.
 
There should be no instability caused by sending a message every 5 minutes to trigger a rule.


Means I can reasonably expect stability. That can only experince answer and I am new at Tasmota. Thanks
 
Mike

Pila

unread,
Nov 27, 2019, 10:38:25 AM11/27/19
to TasmotaUsers
There is a great reason to ask people who know and are kind enough to help - it is the cheapest school :)

Following your great advice, after a meal and a glass of Ivan Dolac 2010, I immediatelly got a solution:

Rule1 On Event#Router Do RuleTimer1 19 Endon On Rules#Timer=1 Do Backlog Power2 Off; Delay 20; Power2 On Endon
Rule1 1

And my router will be sending regular heartbeats:
Event Router

with my router program executing:
Timings need to be adjusted in above commands, this is testing only. Many thanks for your kind help! This is a solution I needed!

What puzzles me? I am unable to achieve seemingly the same by:

Rule1 On Rules#Timer=1 Do Backlog Power2 Off; Delay 20; Power2 On
Rule1 1

and trying to trigger it with
RuleTimer1 9

Why this second version does not work? If it was not for your help, I would still be stuck on above!

Thanks again! This is very important issue to me, as my systems msut work unmanned 24/7 and I can be 8 hours drive away!

Pila

unread,
Nov 27, 2019, 11:01:38 AM11/27/19
to TasmotaUsers
Just in case some non-programmer wants to use this solution: above is not complete! After a router restart, one must initialize the loop again. This initialisation time must be long enough to cover the time router will need to return to full function and execute its program. Without the initialisation, it could sit there and do nothing forever! If too short, it may end up restarting permanently.

So, a full and correct version of the Tasmota router heartbeat reboot commands is:

Rule1 On Event#Router Do RuleTimer1 19 Endon On Rules#Timer=1 Do Backlog Power2 Off; Delay 20; Power2 On; RuleTimer1 22 Endon

This second number (22 in my command) must be adjusted accordingly: rooter boot time + desired extra time for its program to start working + some reserve. Likely - it should be larger for rooter boot time than the first number (19 in my example)

That is why my router sends a heartbeat once every 5 minutes but Tasmota restarts a router only after a heartbeat was missing at least 3 times - or after bit more than 15 minutes.

For my routers, numbers will most likely be: 930 (15,5 minutes - instead of 19) and 1100 (bit over 18 min - instead of 22) for initialisation.

Michael Ingraham

unread,
Dec 2, 2019, 9:26:06 AM12/2/19
to TasmotaUsers
Pila,

RuleTimer1 N

should work as the heartbeat from the router to reset the rule timer. In any case, you have a working solution... so all is well.

Mike

Pila

unread,
Dec 3, 2019, 4:24:44 AM12/3/19
to TasmotaUsers
What would be the best way to initialise a Timer here, so I am certain that while working automatically, it is allways initialised. I modified the initialisation from above, since I think this may be better:
On Power2#state=1 Do RuleTimer2 1110 Endon
Point is: after a router is cycled Off and back On, prime the RuleTimer2 so if the router never sends a heartbeat after it was restarted, it will be cycled again.

Normally, Power2 is always On and PowerOnState=3 but I think I will change to: PowerOnState=1. Any of the following scenarios should trip/check Power2 to On and initialise my RuleTimer2:

- after Tasmota executs the rule to kill and restore power to my router
- when I manualy restart a Sonoff
- when Sonoff returns from a completely unplugged state
- restart because something went wrong and a watchdog cycled Sonoff

If I turn the rule (I mean the complete set from above, not just this segment) manually Off and back On, RuleTimer2 will not arm itself automatically - good for temporary disabling it until a heartbeat starts it again.

I think there is no scenario #5 where initialisationon should be executed. Am I right?

Now I feel I am mostly comfortable with the rules. But, some things need experienced mind :)

Pila

unread,
Dec 3, 2019, 4:41:47 AM12/3/19
to TasmotaUsers


RuleTimer1 N

should work as the heartbeat from the router to reset the rule timer.

Since I am learning to use Tasmota, I do not allow for "not working and it should"...

I retested:
Rule2 On Rules#Timer=4 Do Backlog Power4 Toggle Endon

Rule2 1

RuleTimer4 10

curl
-s http://192.168.2.40/cm?cmnd=RuleTimer4%2010

And it works as expected. I must have been doing something wrong originally. Or it was that glass of wine :)) Thanks for making me retest this.

Michael Ingraham

unread,
Dec 3, 2019, 8:12:12 AM12/3/19
to TasmotaUsers
"not working and it should" was my gentle way of saying - "you're doing something wrong" ;-)
Reply all
Reply to author
Forward
0 new messages