Speedtest server ID

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

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.