Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the load on met.no servers, yr.no sensor #12435

Merged
merged 14 commits into from
Feb 17, 2018
Merged

Reduce the load on met.no servers, yr.no sensor #12435

merged 14 commits into from
Feb 17, 2018

Conversation

Danielhiversen
Copy link
Member

@Danielhiversen Danielhiversen commented Feb 15, 2018

Description:

Reduce the load on met.no servers, yr.no sensor

Try to spread the load more.
Each user will at initialization of the sensor get a random time (min:sec), and will only ask for new data at that time

Related issue (if applicable): fixes #12425

Example entry for configuration.yaml (if applicable):

sensor:
 - platform: yr
   monitored_conditions:
     - temperature

Checklist:

  • The code change is tested and works locally.

except (asyncio.TimeoutError, aiohttp.ClientError) as err:
try_again(err)
"""Retry in at least 20 minutes."""
minutes = 15 + randrange(5)
Copy link
Contributor

@thijsdejong thijsdejong Feb 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not 'at least 20 minutes'. This is '15 to 19 minutes'.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will update the comment

hass, weather.async_update, minute=randrange(1, 10),
second=randrange(0, 59))
yield from weather.async_update()
async_track_utc_time_change(hass, weather.async_update, second=0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not on the hour. This is called every minute.

Copy link
Member

@balloob balloob Feb 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read the code, looks like just the comment is stale.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is very inefficient. Instead of calling update every minute, we should just keep track when we want to fetch data, then when data is fetched, we should call the update method.

I think that it should be something like this:

def async_setup_platform(…):
    async_call_later(hass, 5, weather.update_data)

class YrData(object):
    @asyncio.coroutine
    def update_data(self):
        try:
            yield from fetching_data()
            yield from updating_devices()
            async_call_later(hass, random(X), self.update_data)
        except …:
            async_call_later(hass, random(15), self.update_data)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, it seems like you're doing this already. So this line can be removed

Copy link
Member Author

@Danielhiversen Danielhiversen Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to call updating_devices() more often and more regular than fetching_data(), but every minute is probably overkill.

When we fetch the data we get the forecast for several hours:
Eg: 10:00, 11:00, 12:00, 13:00, 14:00,
So at 10:31 we want to show the 11:00 forecast as that is closest in time.

We still want to fetch new data each hour, to make sure we have the newest and best forecast.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. In a perfect world we would set a timer for exactly that time to push an update.

@balloob balloob added this to the 0.63.3 milestone Feb 15, 2018
async_track_utc_time_change(hass, weather.updating_devices, minute=31)
# wait 1 minute after start up before we fetch data,
# to avoid several restarts to fetch new data.
async_call_later(hass, 60, weather.fetching_data)
Copy link
Member Author

@Danielhiversen Danielhiversen Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this?
This show the weather info as unknown for the first minute.
It will reduce the load on their servers, but the user experience will be lower?

Copy link
Member Author

@Danielhiversen Danielhiversen Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also makes the tests fail

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also just call it directly. The impact on their servers should be minimal as this is a one time call

Copy link
Contributor

@dgomes dgomes Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about using the last value in recorder ? (like input_*)

If someone is testing/configuring and keeps restarting, he will keep hammering the endpoint

@balloob balloob merged commit eaba3b3 into dev Feb 17, 2018
@balloob balloob deleted the yr branch February 17, 2018 21:33
balloob pushed a commit that referenced this pull request Feb 17, 2018
* Spread the load more for the yr.no sensor

* Spread the load more for the yr.no sensor

* Spread the load more for the yr.no sensor

* Spread the load more for the yr.no sensor

* Spread the load more for the yr.no sensor

* Spread the load more for the yr.no sensor

* Spread the load more for the yr.no sensor

* Spread the load more for the yr.no sensor

* Spread the load more for the yr.no sensor

* fix comment

* Spread the load more for the yr.no sensor

* Spread the load more for the yr.no sensor

* Spread the load more for the yr.no sensor

* Update yr.py
@balloob balloob mentioned this pull request Feb 17, 2018
@home-assistant home-assistant locked and limited conversation to collaborators May 29, 2018
@ghost ghost removed the platform: sensor.yr label Mar 21, 2019
@ghost ghost added the integration: yr label Mar 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Heavy requests to YR.no weather API
5 participants