Prevent REST integration bursts

Hi all,
I have created 2 REST sensor configurations for shelly cloud devices. Each individual work just fine, but when having both operational, I get an error from Shelly: TOO_MANY_REQUESTS.
The reason :Shelly allows only 1 request per second.
I have used the scan_interval to set both calls to 30 seconds, but the REST Sensor integration generates a burst: the two calls are made immediately one after the other. The two calls are within a second, making the second call fail consistently with the error as a result
The only solution I found so far was to configure them with with different scan_interval, lowering the chances of having them in the same second (eg 29 and 30, resulting in an error every 29/30 calls = 15 minutes). Besides the fact that this still generates errors, this is also not scaleable.
Is it possible to instruct the REST Sensor integration to not have burst but somehow throttle the calls (to max 1/sec)?
Note: I need to use cloud API because these Shelly devices are on a different network than my HA installation.

Have you looked into using the REST integration instead of separate REST Sensors?

My mistake. It is a REST integration. Not a RESTFull integration. Each REST calls has several sensors linked to it, but I need 2 calls because it’s for 2 different Shelly devices.
I updated the title and text.

Another option would be to set up the REST requests as individual REST Commands, then have Trigger-based Template sensors with Time Pattern triggers.

And another option is to disable polling, and then set up an automation to control exactly when to poll.

The docs walk through how to do it:

1 Like

The time pattern trigger has the samular and new issue as the scan_interval, no? I can define seconds:/29 and seconds:/30, and that will indeed trigger twice a minute (but at a slightly inconsistent frequency but this is neglectable for my pupose), but if I wanted to increase the frequency to eg /2 and /3, I run into the same problem again: two calls at second 6,12,18,etc

The Rest integration does not seem to have the 3 dots to disable the automatic polling probably because it was not set up from the UI.

Set up each sensor independently with conditions to avoid executing the command on the same second:

template:
  - triggers:
      - trigger: time_pattern
        seconds: /1
    conditions:
      - "{{ trigger.now.second is odd }}"
    actions:
      - action: rest_command.shelley_endpoint_1
        response_variable: action_response
    sensor:
      - name: 
        state: #YOUR TEMPLATE TO EXTRACT DATA FROM #1

  - triggers:
      - trigger: time_pattern
        seconds: /1
    conditions:
      - "{{ trigger.now.second is even }}"
    actions:
      - action: rest_command.shelley_endpoint_2
        response_variable: action_response
    sensor:
      - name: 
        state: #YOUR TEMPLATE TO EXTRACT DATA FROM #2

And if you want to spread out calls a bit more, you can use math tricks…

For example if a 4 second interval is okay:

# Set triggers to /2 then use the following templates in your conditions

- "{{ trigger.now.second == 0 or trigger.now.second % 4 == 0 }}"

- "{{ trigger.now.second % 2 == 0 and trigger.now.second % 4 != 0}}"
 
1 Like