What is the maximum scan_interval that I can use?

I want to set as large of a scan_interval as possible and then only update the sensor right before it is needed in an automation by using homeassistant.update_entity. The reason is because the more times the sensor below is updated, the more quickly the cookie expires. (The cookie is needed by alexa_remote_control.sh which is called from alexa_wrapper.py).

  - platform: command_line
    name: Last Alexa
    command: 'python3 /config/alexa/alexa_wrapper.py {{ "-lastalexa" }}'
    scan_interval: 3600

It is currently set to 1 hour, but if I could I would set it to 24 hours or more or better yet wouldnā€™t let it automatically scan at all.

Does anyone know the maximum scan_interval or is there a number that stops it from scanning automatically altogether?

The short answer might be to try it and see :slight_smile:

I used to have a Rest sensor with a scan_interval: 21600 which worked without issue.

1 Like

I have one with 86400 (24 hours) which seems to work.
Although having said that, the reason I use 86400 is because I only want the sensor to update when I say so which means I donā€™t actually know if the 86400 works. But it doesnā€™t break it! :slight_smile:

1 Like

Yeah, Iā€™m not sure how to test. I can set it to a large number and wait and see how many days it took to expire the cookie but I cannot use that to calculate what is the maximum scan_interval. Yesterday, I tried a value of 315360000 which is 10 years and I did not get an error on boot up, but Iā€™m still not sure if that is a valid value for scan_interval. I feel like knowing what the maximum scan_interval can be, could be of value for everyone, but I think only a programmer that knows that code can tell us.

1 Like

So I believe the units are in seconds. And much of the code from what I can understand (I am not a Python developer), uses a library called ā€˜timedeltaā€™ for initialization, and various units can be used for that, for the sensors and devices HA supports (minutes, seconds, days etc).

But I think for the interval listeners HA sets up, every n seconds (scan_interval), it just grabs the current utc time, and adds the interval to it. So 31536000 secs for the year.

Iā€™ll get back to you in a year and let you know if it worked :wink:

Well thenā€¦?

1 Like

Thereā€™s still no magic number that makes it ā€œmanual onlyā€ if thatā€™s what youā€™re wondering. I think I had one set to a week (604800) at one point but then I realized it was trying to update the sensor every time HA restarted which kind of defeated the point.

If you donā€™t update and donā€™t often make changes to your HA which require a reboot you could try and see what happens if you go with massive numbers. But if you do either of those things not much point to going beyond a day or two.

1 Like

If it uses a 64bit unsigned integer to store the seconds:

18,446,744,073,709,551,615 seconds.

or,

586,549,402,018 years, 3 weeks, 3 days, 15 hours, 30 minutes, 7 seconds

Halve that for a signed 64bit integer.

For 32 bit unsigned int: 4,294,967,295 seconds or 136.19 years, or 68 years for a signed int.

For a 16 bit unsigned int: 65,536 seconds which is only about 18 hours and we know it is bigger than that.

So you should be safe with 50 years (signed 32 bit is bigger).

Famous last words: "no one needs an update interval longer than 50 years. "

3 Likes