Update interval of resful sensor

Hey everybody,

I need to change the update interval of a rest sensor.
I tried scan_interval:600 in caonfig.yaml and setting MIN_TIME_BETWEEN_UPDATES in components/sensor/rest.py but that doesn’t make any differece at all. Requestst are being sent every 1 or 2 Minutes
What am I doing wrong here?

I’m planning for some in home location tracking via wifi fingerprinting and until I manage to write a component for FIND I want to use the REST sensor, so same problem for me.

I opened an issue here: https://github.com/home-assistant/home-assistant/issues/1851

Well then I need to find a workaround until this is getting fixed.

I recently stumbled upon FIND as well. How is it working for you so far?

Haven’t yet integrated it with HA (well, and won’t until this either gets fixed or I/someone writes the component) but it looks pretty impressive :slight_smile:

I live in a multi-tenant apartment and it can, after a short (1-2s) delay, recognize if I’m in my bedroom, the floor or my living room.

That sounds pretty good. Can’t wait to give it a try.
Gotta sort out my update issue first, though

Okay, it works with copying the rest.py component into /custom_components/sensor/ changing in the config for the sensor setting scan_interval=1

- platform: rest
  resource: http://cwagnerpc.cw:8003/location/home/cwagner
  value_template: '{{ value_json.location }}'
  method: GET
  name: REST GET sensor
  scan_interval: 1
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=1)

It does indeed looks like the rest sensor is hard coded to be polled max once per minute. The component should be updated to not use throttle but define a scan interval of 60 seconds. A PR to fix this is welcome.

Wrote a script which gets the data via cron, so resolved for me :slight_smile:

I plan to write a component for this anyway, so could you clarify something for me. please?
A Sensor inherits EntitiyObject, and that has the SCAN_INTERVAL property. So if I set that updates are being made in that time interval. Or is there more to it?

FWIW, balloob merged my PR removing the Throttle decorator from rest sensors so once that’s in the release (or you are running dev) the scan interval property will work for rest sensors :slight_smile:

removing @Throttle(MIN…) did it?
I’m a python noob, so I’m still trying to understand how all this works

Yes. The standard scan interval is set to 30s, throttle was set to 60s, so updates usually came once a minute, probably sometimes only once every 90s (also a python noob so I’m not sure about the 90 :D). With the PR you don’t need my fix from yesterday as the PR does what the fix did without you having to copy the built-in component.

1 Like

OK, gonne give it a try later, thx

Why not let the user define the scan interval? There are several providers that limit the API usage and polling them every minute may not be possible. Can we have the default as 60 seconds and give the user an option to specify scan_interval?

1 Like

Im with Arsaboo on this one. Right now, im sending three rest queries to my online thermostat every 30 seconds. I could see it happening them cutting it me off for requesting that info since their API isn’t exactly open yet. Especially if I want to pull more specific data. The interval could easily be several minutes, b/c temperatures don’t change that quickly anyway.

1 Like

I have not tried this but would this work?

diff --git a/homeassistant/components/sensor/rest.py b/homeassistant/components/sensor/rest.py
index 16b50f8..1ed981e 100644
--- a/homeassistant/components/sensor/rest.py
+++ b/homeassistant/components/sensor/rest.py
@@ -17,6 +17,7 @@ from homeassistant.const import (
 CONF_PASSWORD, CONF_AUTHENTICATION, HTTP_BASIC_AUTHENTICATION,
 HTTP_DIGEST_AUTHENTICATION, CONF_HEADERS)
 from homeassistant.helpers.entity import Entity
+from homeassistant.util import Throttle
 import homeassistant.helpers.config_validation as cv

 _LOGGER = logging.getLogger(__name__)
@@ -24,6 +25,7 @@ _LOGGER = logging.getLogger(__name__)
 DEFAULT_METHOD = 'GET'
 DEFAULT_NAME = 'REST Sensor'
 DEFAULT_VERIFY_SSL = True
+CONF_UPDATE_INTERVAL = 'update_interval'

 PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
 vol.Required(CONF_RESOURCE): cv.url,
@@ -38,6 +40,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
 vol.Optional(CONF_USERNAME): cv.string,
 vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
 vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
+    vol.Optional(CONF_UPDATE_INTERVAL, default=timedelta(seconds=60)): (
+        vol.All(cv.time_period, cv.positive_timedelta)),
+
 })


@@ -63,7 +68,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
         auth = HTTPBasicAuth(username, password)
 else:
     auth = None
-    rest = RestData(method, resource, auth, headers, payload, verify_ssl)
+    rest = RestData(method, resource, auth, headers, payload, verify_ssl,
+                interval=config.get(CONF_UPDATE_INTERVAL))
 rest.update()

 if rest.data is None:
@@ -118,13 +124,17 @@ class RestSensor(Entity):
 class RestData(object):
 """Class for handling the data retrieval."""

-    def __init__(self, method, resource, auth, headers, data, verify_ssl):
+    def __init__(self, method, resource, auth, headers, data, verify_ssl, interval):
     """Initialize the data object."""
     self._request = requests.Request(
         method, resource, headers=headers, auth=auth, data=data).prepare()
     self._verify_ssl = verify_ssl
     self.data = None

+        # Apply throttling to methods using configured interval
+        self.update = Throttle(interval)(self._update)
+
+
 def update(self):
     """Get the latest data from REST service with provided method."""
     try:

I’m in a similar situation. I only want a RESTful sensor to update when I call a script. Is this possible?

Did you have any luck with this? I’m in a similar situation where I want to limit my REST sensor call to between certain times (I have a max for 20 API calls per day for free)

Did this ever go live?
I would love to get updates from some of my rest sensors more frequently than once a minute and some less.