Command line sensor problem - multiple JSON sub-attributes?

I’m trying to make a sensor for monitoring internet speed using the official speedtest.net CLI application which can return JSON output.

Here’s an example of the output:
{"type":"result","timestamp":"2020-04-12T02:44:43Z","ping":{"jitter":0.58499999999999996,"latency":13.332000000000001},"download":{"bandwidth":115760816,"bytes":1209207888,"elapsed":10613},"upload":{"bandwidth":6509554,"bytes":23564752,"elapsed":3615},"packetLoss":0,"isp":"Atlantic Broadband","interface":{"internalIp":"192.168.1.221","name":"enp0s10","macAddr":"xxxxxxxxxx","isVpn":false,"externalIp":"x.x.x.x"},"server":{"id":14335,"name":"PBW Communications, LLC","location":"Herndon, VA","country":"United States","host":"speedtest.leadmon.net","port":8080,"ip":"38.103.8.130"},"result":{"id":"xxxxxxxxxxxxx","url":"https://www.speedtest.net/result/c/xxxxxxxxxxxxxxxxxxxx"}}

I’ve made a sensor which partially works, but doesn’t give me the key bits I really want:

sensor:
  - platform: command_line
    name: "Speedtest.net"
    #command: "/config/speedtest-net/speedtest --accept-license --format=json > /config/debug.txt 2>&1"
    command: "/config/speedtest-net/speedtest --accept-license --format=json"
    scan_interval: 3600
    command_timeout: 60
    json_attributes:
      - ping.jitter
      - ping.latency
      - download.bandwidth
      - upload.bandwidth
      - packetLoss
      - isp
      - name
      - location
      - country
      - url
    value_template: "{{ value_json['download.bandwidth'] }} / {{ value_json['upload.bandwidth'] }}"

When I look at it, I don’t see the download/upload speeds (which are the important bits).

First, how do I get it populating the rest of the values?

Second, how do I do the conversion from bytes/sec to Mbit/sec (I need to take the reported bandwidth X*8/1000000)

Change this to:

    value_template: "{{ value_json['download']['bandwidth'] }} / {{ value_json['upload']['bandwidth'] }}"

With regards to json_attributes, I’m pretty sure that only top-level keys are supported, that’s why packetLoss and isp work. None of the other items in your list are top-level keys, and the dot-notiation does not work either.

Huh, that’s annoying. Any idea how I could ultimately break it into separate things to graph then?

Ultimately I’d like to have both a simple “heads up” with the most recent numbers, plus a numeric graph of the past day every hour for download and upload separately.

EDIT: Looking now at making a script to publish it as MQTT and pull it out of there similar to how my thermometer readings are pulled in, sounds like that may give me better flexibility.

Ok, I’ve managed to get most of what I want, I just can’t figure out how to trigger it now.

File is executable:

-rwxr-xr-x    1 root     root         418 Apr 12 14:59 /config/speedtest-net/mqtt_pub.sh

Contents:

#!/bin/bash
broker="192.168.1.221"
username="username_here"
password="password_here"
topic="homeassistant/sensor/speedtest-net"
message="`/config/speedtest-net/speedtest --accept-license --server-id=14335 --format=json`"

mosquitto_pub -h "$broker" -u "$username" -P "$password" -t "$topic" -m "$message"

When I run this from SSH it works flawlessly, pushes the data to MQTT and then a bunch of sensors I wrote pick them up perfectly!

So I tried to make an automation to call the shell command that works when I run it manually:

# Call script which publishes readings to MQTT
shell_command:
  speedtest_net_run_test: '/config/speedtest-net/mqtt_pub.sh'

automation:
  # Run the test on startup, and hourly
  - alias: speedtest_net_run_test
    trigger:
      - platform: homeassistant
        event: start
      - platform: time_pattern
        minutes: 0
    action:
      service: shell_command.speedtest_net_run_test

When the automation triggers, I get error code 127.

I’m out of ideas…I even tried at one point hard coding the full path to the mosquitto_pub command and it still didn’t like it. I even tried simply “touching” a file from my script, and that was fine…seems to be somehow it just really doesn’t want to correctly do the pub?

Maybe this is an easier problem to fix?

EDIT: I’ve finally found this post which tells how to use the console to get to the actual hassio environment (WOW what an undocumented PITA, how does anyone dev anything without knowing the environment?)

So it looks like the mosquieto pub executable is missing somehow? Why? How do I fix that?

EDIT2: I managed to make it run by randomly copying libs and executables into my /config area and forcing LD_LIBRARY_PATH…but this can’t be remotely the right way of doing things. I need to figure out how to make it actually work semi-correctly