How to Limit HTTP (POST/GET) Request?


#1

I want Rule Machine to execute an HTTP GET URL whenever my motion detector detects motion. Is it possible to limit how often this executes? For instance, I only want it to run once a minute. Any tips?

I am using a Raspberry Pi as a dashboard and want the screen to turn on whenever the motion sensor detects motion. I wrote a quick script in flask that runs on the Pi and wakes up the screen whenever it receives the GET request. But I want to reduce the number of requests I send or else I will be overloading the Hub and spamming the network.


#2

Pause the rule for x-minutes and then reactivate it?


#3

So I set the motion sensor(s) (possibly multiple) as a trigger and then have it wait 60 seconds after sending the GET command? Is this the best-way to do it (most efficient).
Is pause different than waiting?


#4

I'm super new to hubitat so I don't know if a rule can resume itself or not, but maybe try

Send http request
Pause rule
Resume rule with a delay of 1min

I guess the most efficient way is to change the parameters in the sensor itself to be active longer and then use a trigger instead of a rule. (Then it will just run once when it changes to active from inactive).


#5

You could use a virtual switch that resets to off (momentary switch?) then Use that switch as a restriction in Rule Machine.


#6

Do you want it to send at least every minute when it is active? I'm not 100% clear on what you are trying to achieve.


#7

I want to send it at most once per minute. For instance, if I walk into a room - my motion sensor will report several motion events. But I want to submit the GET request at most once per minute when motion is present.

If there isn't any motion then nothing should be sent.


#8

You didn't answer my question. Let me ask it a different way.

You motion sensor goes active, it sends the request. You motion sensor remains active (without going inactive) for 10 minutes. Do you want 10 more get requests?
Motion sensors don't report every motion they detect. They have timeouts that if no motion is detected, after the timeout, it reports as inactive. Only when it goes inactive, can it then go back to active. So, if you do a Trigger based on active and the motion sensor is active for 10 minutes you will get one GET request.


#9

So yes - it should send 10 requests.

So something like this (just not sure the best way to implement this in rule machine)

flag = 0
if (motion or door_opens):
    if (flag == 0):
        Send Get Request
        flag = 1

if (flag == 1 for at least one minute):
    flag = 0

Or as @broberg suggested - perhaps something like this could work and be easier to implement?

if (motion or door_opens):
        Send Get Request
        sleep(60)

#11

Yeah, that's gonna be a real PITA to code and seems pretty silly, IMHO. Once you tell whatever system the motion sensor is active, it should assume that it still is until you tell it that it isn't. It shouldn't need reminding.


#12

One could probably use the "repeat action" to send more requests then one on trigger.

But why not use the monitor off timer on the pi? Say when triggered by the http request it stays on for, say 5 minutes. Then you set your motion sensor to stay active for 5 minutes on motion. Then using a rule you set a repeat action every 6 minutes as long as motion is true.

Btw,I would love to see the script for the wake on http, got a Pi running MagicMirror2 and wouldlike to wake it witha a http request :grin:


#13

This is all that the Pi is doing. I am using XScreenSaver which I have set to sleep the screen in 1 minute of inactivity. The command "xscreensaver-command -deactivate" basically simulates activity (i.e mouse movement). I basically have this command execute whenever the page :5000/wake is execute.

from flask import Flask, request
import os

app = Flask(__name__)

@app.route("/wake", methods=['GET'])
def wake():
    os.system("xscreensaver-command -deactivate")
    return "Awake"


if __name__=='__main__':
    app.run(host='0.0.0.0')

#14

Wouldn't it be easier just to tell the Pi when to wake the screen up and then when to turn it off rather than telling it to constantly stay on. Seems like it would be more reliable to me. Then you could guarantee when it would turn off, not just guess at the minute. For example, maybe it's tied to a light also, so it turns off immediately if a light in the room turned off.


#15

The Pi currently turns off if nobody is using it after a minute (like a screensaver). Having the screen turn on when motion is present or a door opens works fine - the problem is with the amount of messages being sent.

I plan on using a wired motion sensor integrated with Hubitat via Konnected. So when motion is present the Konnected board will send a message to Hubitat which will send the GET request to the Pi. The problem is that I can send hundreds of messages in a given hour. And with multiple Pi's this can increase substantially. I basically want to limit the amount of messages sent once per minute not for functionality rather for efficiency.

Having the Pi turn on when motion is present and turn off when motion is no longer present would work fine as well but it does not resolve the issue with the amount of messages being sent. And I wouldn't want the screen to turn off if motion stops and someone is using the screen.

Light switch might work for the main keypad but I am not sure about the bedroom. I still want the keypad to illuminate at night time without having to turn on the lights. I do plan on having brightness tied to the light switch though.

I am having the condo currently renovated so I still have to install and setup everything once its ready. I am slowly getting components and setting things up.


#16

Maybe this would work for your purpose


#17

I set it up according to @broberg's screenshot but it doesn't seem to be resending the GET command every 15 seconds while motion is active. Currently - while motion remains active it doesn't resend every 15 seconds but if motion becomes inactive and then active again it will resend. Any tips on how to resolve this?

Here is a video of what's going on

As you can see when the motion sensor initially becomes active the screen turns on. I keep the motion sensor active but it doesn't resend - eventually the screen goes to sleep and I let the motion sensor become inactive. Once the motion sensor becomes inactive and active again then it resends.

It seems that rule machine isn't resending the get command as specified - if so could this be a bug @bravenel @bobbyD - any assistance on this would be much appreciated!


#18

That's because the repeat has to be before the actions you want to repeat.


#19

It works - thanks @bravenel & @broberg. Is it also possible to limit the number of GET request I send? So the hub doesn't send more then once every 25 seconds (or any specified seconds amount)?

I am worried that I might overload the hub by having it tied into the motion sensor. If the motion continually goes active/inactive this can add up.

Would adding a delay (for 25 seconds) after the last GET request do the job? Or is there a better way to do this?


#20

What is the timeout of the motion sensor? It won't go from True to False until the sensor times out. So, if you had a Iris V2 for example, you wouldn't get the false until there was no motion for 30 seconds. What you might want to do is put a delay on the false with a cancel on truth change. That way, you could build in your own longer timeout to keep your rule reporting that motion is active.


#21

Yes, you can repeat n times and then stop.