Add the official speedtest cli

Thank you, I also found the built in integration to be unreliable after I switched to gigabit fiber.
Here is my solution:

sensors.yaml:

# SpeedTest.net CLI
# https://www.speedtest.net/apps/cli
# Template sensors are in configuration.yaml
# Download and test, use binary matching platform, e.g. for x86_64
# wget -qO- https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-x86_64.tgz | tar xvz
# ./speedtest --accept-license --accept-gdpr
# Copy binary to desired HA config folder
# mkdir -p /config/3rdparty/speedtest
# cp ./speedtest /config/3rdparty/speedtest/
- platform: command_line
  name: "SpeedTest CLI Data"
  unique_id: speedtest_cli_data
  # Use the path as configured on your system
  command: "/config/3rdparty/speedtest/speedtest --format=json --accept-license --accept-gdpr"
  # Every 4 hours, 60 * 60 * 4 = 14400
  scan_interval: 14400
  command_timeout: 60
  # Summarize results to stay below string limit and convert to JSON
  value_template: >-
    {{ 
      { 
        "ping": value_json.ping.latency, 
        "download": value_json.download.bandwidth, 
        "upload": value_json.upload.bandwidth 
      }
      | to_json 
    }}

configuration.yaml:

# SpeedTest.net CLI
# https://www.speedtest.net/apps/cli
# Command_line sensor is in sensors.yaml
template:
  - sensor:
    - name: 'SpeedTest CLI Ping'
      unique_id: speedtest_cli_ping
      icon: mdi:speedometer
      # TIME_MILLISECONDS: Final = "ms"
      unit_of_measurement: ms
      state_class: measurement
      state: "{{ (states('sensor.speedtest_cli_data') | from_json).ping | round(2) }}"
    - name: 'SpeedTest CLI Download'
      unique_id: speedtest_cli_download
      icon: mdi:speedometer
      # DATA_RATE_MEGABITS_PER_SECOND: Final = "Mbit/s"
      unit_of_measurement: Mbit/s
      state_class: measurement
      state: "{{ ((states('sensor.speedtest_cli_data') | from_json).download * 8 / 1000 / 1000) | round(2) }}"
    - name: 'SpeedTest CLI Upload'
      unique_id: speedtest_cli_upload
      icon: mdi:speedometer
      unit_of_measurement: Mbit/s
      state_class: measurement
      state: "{{ ((states('sensor.speedtest_cli_data') | from_json).upload * 8 / 1000 / 1000) | round(2) }}"
12 Likes