Speedtest server ID

I was able to get this to work…didn’t realize there are a few other differences with HassOS.
Here is what I did:

  • The Python3 path is different /usr/local/bin/python3. Edit the first line of both the launch_speed_test.sh and the speedtest-cli-2ha.py to have the path /usr/local/bin/python3
  • Path to the speedtest files is /config/shell_commands/ so change the 2nd line in the launch_speed_test.sh file to account for this and in the speedtest-cli-2ha.py change the SPEEDTEST_PATH to /config/shell_commands/speedtest.bin
  • The URL to reach homeassistant can NOT be http://homeassistant.local. However http://homeassistant works fine.
1 Like

Appreciate the help!

I made the changes, however when I run manually I receive the error:

./speedtest-cli-2ha.py: bad interpreter: /usr/local/bin/python3: no such file or directory

I looked and my python3 is located at:

/usr/bin/python3

Oh that’s interesting! As a check, did you go inside the homeassistant container from hassos command line to find your python3 path?

I’m using the SSH & Web Terminal add on and navigating from there. I believe that’s the homeassistant container right?

In my case the ssh/web terminal takes me to the ssh addon container and in my case this container doesn’t have any python code

Update: Problem solved!

I wasn’t able to ever get the .sh file to execute from HA for some reason, but I was able to call the .py direct and it works great.

I kept your suggested changes and the shell_command path that finally worked for me is:

shell_command: #Starting 0.114, command timesout after 60s.
  launch_speedtest_cli: "/usr/local/bin/python3 /config/shell_commands/speedtest-cli-2ha.py"

Thanks again for sharing and helping me get this going!

Many thanks for your work and inspiration. I’ve made it a bit simpler - only the python script exists which is called directly from HA command_line sensor. It populates the state and additional attributes based on output in json format made by the script. Here is my fork of your repo:

Looking forward to any comments, especially explaining why I should not do it this way :slight_smile:

Glad I could inspire :slight_smile:

Thanks a lot for your help.
Do you have any idea how can i decrease the number of digits after the decimal point ?

Thanks a lot

I have a few questions.

  • Where do you find your HA token which they ask for in speedtest-cli-2ha.py?
  • I can’t find python anywhere when I use ssh in the terminal and go to the different locations. Even doing a “which python” or “python -version” returns that python doesn’t exist. Is it “hidden” as a Home Assistant install and not seen as python the app itself? OR do I need to install it?
    I’m running Home Assistant on a Raspberry Pi 4.

Thanks.

Oh also, @jmart518 you said it wasn’t in usr/local/bin but then you say to use usr/bin.
So where you wrong the first time around?

You need to use | round (0) when defining your states.
I usually just redefine states.

- platform: template
  sensors:
    speedtest_download_rd:
      friendly_name: Speedtest Download Rounded
      unit_of_measurement: Mbit/s
      value_template: "{{ states('sensor.speedtest_download') | round(0) }}"

To generate a long lived access token: Click on your profile in the sidebar (at the very bottom) which will then bring up your profile page. From there, scroll down to the “Long-Lived Access Tokens” and click “Create Token”. Give it a name and it will generate the token that you will need to copy/paste into the script.

As far as how it works for me (I am still on the older version, I noticed today that things may have changed a bit).

I created this shell command in configuration.yaml:

shell_command:
  launch_speedtest_cli: "/usr/local/bin/python3 /config/shell_commands/speedtest-cli-2ha.py"

Where /usr/local/bin/python3 is the path to where python is in my HA instance (HAOS VM on ESXi) and /config/shell_commands/speedtest-cli-2ha.py is the path to the speedtest script.

I then call that shell command using an automation that runs every 30 min:

alias: Speedtest
description: ''
trigger:
  - platform: time_pattern
    minutes: '30'
condition: []
action:
  - service: shell_command.launch_speedtest_cli
    data: {}
mode: single

I have been trying to get this to work using the instructions here but the python script is not running with the following error in the logs:

Logger: homeassistant.components.command_line
Source: components/command_line/__init__.py:41
Integration: command_line (documentation, issues)
First occurred: 07:03:36 (1 occurrences)
Last logged: 07:03:36

Command failed: python3 /config/shell_commands/speedtest-cli-2ha.py

My sensor entry is set up as follows:

  - platform: command_line
    name: Speedtest Ping
    command: "python3 /config/shell_commands/speedtest-cli-2ha.py"
    value_template: "{{ value_json.ping | round(1) }}"
    command_timeout: 30
    scan_interval: 1800
    unit_of_measurement: ms
    json_attributes:
      - 'download'
      - 'upload'
      - 'server_name'
      - 'isp'

I have also tried replacing python3 in the command above with /usr/local/bin/python3, python3.8 and python3.9 with the same results.

If I run ./speedtest-cli-2ha.py from the command line with debugging switched on I get:

[core-ssh shell_commands]$ ./speedtest-cli-2ha.py
./speedtest-cli-2ha.py: line 4: import: command not found
./speedtest-cli-2ha.py: line 5: import: command not found
./speedtest-cli-2ha.py: line 6: import: command not found
./speedtest-cli-2ha.py: line 7: import: command not found
./speedtest-cli-2ha.py: line 8: import: command not found
./speedtest-cli-2ha.py: line 11: SPEEDTEST_SERVERID: command not found
./speedtest-cli-2ha.py: line 12: SPEEDTEST_PATH: command not found
./speedtest-cli-2ha.py: line 15: DEBUG: command not found
./speedtest-cli-2ha.py: line 16: CONSOLE: command not found
./speedtest-cli-2ha.py: line 18: syntax error near unexpected token `('
./speedtest-cli-2ha.py: line 18: `_LOGGER = logging.getLogger(__name__)'

Can anyone help please?

1 Like

Oh !! Thanks a lot, its perfect !

I had to play around with this for quite some time until I got it running. The command line you have (python3) works for me. What I had to change was set the path to the speedtest binary in the py file:
SPEEDTEST_PATH = '/config/shell_commands/speedtest.bin'
You should also remove the serverid that’s entered in the script or replace it by a suitable one.

I’m running this in a VirtualBox image, so the hass.io I can reach via ssh doesn’t have python accessible from the commandline, but it works from the sensor without problems.

What I also had to look up as a HA newbie was that the commandline sensor had to go unter a section sensor: in configuration.yaml, and the template sensor under a section template:.

1 Like

Thanks very much for the suggestion, I have change the SPEEDTEST_PATH to match your suggestion and I’ve double checked the server ID but it is still not working. I have got the config yaml layout right as I already have other command line and template sensors that work.

I’m still scratching my head on this one :frowning:

I’ve figured out the problem - the server ID that I had chosen from this list doesn’t seem to work, if I leave the server ID blank and let it get chosen automatically then it’s working fine :slight_smile:

If you want a specific server, you can run speedtest-cli from the commandline, I think there is some option to show server ids…

Hello

I’m newbie in HA and installed speedtest add-on via UI. It works.
I want to change the default server which is not displayed in the list list and the 1h-cycle of test.

where can I change the server_ID ? where can I find it on HA UI ? I don’t see this in module configurtation. I presume this in in a specific file, but where can I find it ?

I read also:

By default, a speed test will be run every hour. You can disable polling using system options and use the update_entity service to automate the speed test frequency.

which is weird for me: where can I find the step-by-step to configure this and become more HA user :slight_smile:

Thank you

1 Like

To turn off Polling
UI->Settings->Devices & Services
Find the Speed Test Integration
click on the 3-dots
click on System Options
You should see Enable polling for updates

Changing the Server-id not in the UI pull down list,
I’m not sure you can.
You could take a chance and change it n the the /config/.store/core.config_entries but be aware that these files are really not suppose to be changed, so I don’t recommend it.

Instead, I suggest going through this thread above and also this thread, where the speedtest-cli is used, and there you should be able to specify the server-id yourself. The speedtest-cli is essentially a binary executable that is run outside of HA and returns bandwidth/delay results. There are different ways that the speedtest-cli can be used with HA, and you’ll see in these threads a few ways to do it.