Dynamic polling interval

Hi guys,

I have a working custom sensor that polls an API every 10 seconds (or at a user-defined interval through the config yaml). I have had a surprising amount of positive feedback. My ask stems from the #1 feature request which is to make the component less chatty. I would like the polling interval to be dynamically set based on the last returned API data.

For example, at initialization, it polls the API every 10 seconds. If the API returns “abc”, it starts polling every 30 seconds. If the API returns “def”, it starts polling every second. If the API returns “xyz”, it starts polling every 10 seconds, etc.

How would I accomplish that?

You can find the repo here.

Thanks!

Have a look at track_time_interval or maybe track_point_in_time (from homeassistant.helpers.event).

I would imagine that you initialise the sensor to update every 10 seconds, and then in your update method you determine if you want to change the tracking interval, and if so, first remove the old listener (when calling track_time_interval its return value is the method you need to call to unsubscribe), and then add a new one with a different interval.