Issues with speedtest automation

Hi guys,

have the following automation

alias: Speedtest
description: Speedtest @ 1am
trigger:
  - at: '01:00:00'
    platform: time
condition: []
action:
  - service: speedtestdotnet.speedtest
mode: single

This runs fine when clicking “run actions” and a speedtest is performed, when left to run on its own it fails with the below error and I have no idea why

Unexpected error fetching speedtestdotnet data: Unable to connect to servers to test latency.
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/speedtest.py", line 1489, in get_best_server
    fastest = sorted(results.keys())[0]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 151, in async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 139, in _async_update_data
    return await self.update_method()
  File "/usr/src/homeassistant/homeassistant/components/speedtestdotnet/__init__.py", line 174, in async_update
    return await self.hass.async_add_executor_job(self.update_data)
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/speedtestdotnet/__init__.py", line 162, in update_data
    self.api.get_best_server()
  File "/usr/local/lib/python3.8/site-packages/speedtest.py", line 1491, in get_best_server
    raise SpeedtestBestServerFailure('Unable to connect to servers to '
speedtest.SpeedtestBestServerFailure: Unable to connect to servers to test latency.

Anyone have any ideas?

Have you tried setting automatic updates in the integration?
Having said that this has been flakey lately I have found. 2021.3.4 seems better and my issues started in 2021.3.3

You can also try changing the schedule so it doesn’t run on the hour (for example, change the trigger to run at 01:02:00). I’ve found that running right on the hour causes the automated job to fail, presumably because there’s too many people doing the same thing.

In the integration you set an update interval and for me it seems to not run on an even minute however if you use an automation probably a good idea.
I haven’t seen any issues for the last fed days now…

Just created an account here as this is the first place where i find this problem mentioned. I am running speedtest.py automated from crontab on raspberry. I see the same error as the OP, but only if triggered by CRON. Running speedtest manually (either speedtest.py, or from the script pushing this into a google spreadsheet (https://github.com/frdmn/google-speedtest-chart)) works just fine - every single time.
Last time it ran well was 8 March, 20:00 CET - i can only see manual runs since then.

i have no idea how to troubleshoot though…

Permissions maybe with cron? Permissions can be tricky. Always trip me up.

speedtest.py is executing just fine (even from chrome) - just like in the OP.

my guess is, that either populating the server list, pinging servers or sorting by ping fails when triggered from cron (hence the index [0] points to nothing)

Not sure how to find out if populating the server list works when triggered from cron - or how this could be affected by being run by cron.

Has it ever worked? Running from the command line and working and not running from cron is textbook permissions (in my experience)…

yes, it worked just fine for a couple of weeks - and i only realized that it had stopped because the chart did not update any more. No touch to the system that i can think of.

In the actual speedtest.py file, there’s a DEBUG flag you can change from False to True and that should dump all the info when it runs, including server selection and whether the list was empty. I’d definitely check permissions as @DavidFW1960 suggested, and otherwise play around with scheduling offsets if you haven’t already (just mentioning again in case you missed my earlier comment).

same issue happens here: it runs well for several days, and than stop delivering data, with error message same as above…

From the line of “IndexError: list index out of range”, I guess, it’s a bug.

No clue.

Thanks @niraj

I have tried a random time and had one successful scheduled speedtest today, will monitor to see if I see any other issues

I have similar issues. No problems until very recently. For example I restarted HA about 20 hours ago. With no changes, it stopped working 12 hours ago (Config=AutoDetect and refresh every 30 minutes)

forcing an update by calling the service from the GUI a) ran, b) seems to have unlocked it.

Yeah when it fails that’s it unless you manually call the service and it seems to be a new thing

looking at https://github.com/home-assistant/core/tree/dev/homeassistant/components/speedtestdotnet there has been no significant change in the last 5 months?

Yeah but HA has changed so something in 2021.3.x has caused it I think…

Hello,
I have the same issue, I had the default configuration with SpeedTest, I had set the server for test and the graph has worked after.
:wink:

Right now I cannot select any German servers. They disappeared from the list. All countries with starting with D, E, F, G are missing. Anybody can confirm this?
Thanks

Solved. The issue was that speedtest uses port 8080 to create the server list and I had another service using the same port.