Commandline sensor without output

Hello,
I’m running HA on my RPi3 for days now and I realised the speedtest component does not work well for me (getting pings over 1000 on my Pi - so it’s useless).

I started to play around with command line ping and wrote a script I want to add instead of speedtest ping.
The script outputs the ms without decimal. Script isn’t best solution yet, but I don’t usually use bash - sorry.

It works if I run it as homeassistant user on my Pi, but in my webinterface it stays emty.

ping.sh

#!/bin/bash
pingresult=$(ping -c 4 8.8.8.8)
PING=$(echo "$pingresult" | grep  min/avg/max/mdev | cut -d " " -f 4 | cut -d "." -f 1)
echo $PING

sensor.yaml:

- platform: command_line
  name: PingPi
  command: "/home/homeassistant/scripts/speedtest/ping.sh"
  unit_of_measurement: "ms"
  scan_interval: 300

I don’t unterstand why it works in ssh but not in HA

I played with output to tmp file.
HA created it but the file was empty. Running it as HA user on pi filled the file. - Rights were the same.

Do you have some ideas for me? Thanks!

Try:

sensor:
  - platform: command_line
    command: 'ping -c 1 8.8.8.8 | grep  min/avg/max/mdev | cut -d " " -f 4 | cut -d "." -f 1'
    unit_of_measurement: "ms"
    name: Ping test

This works for me.

1 Like

Seems to be the way better method to get the ping.
Saidly it fails.

17-02-03 15:51:23 homeassistant.components.sensor.command_line: Command failed: ping -c 1 8.8.8.8 | grep  min/avg/max/mdev | cut -d " " -f 4 | cut -d "." -f 1

Running it as homeassistant user on pi works perfectly again. Tried adding sudo but it still fails.
Already tried this fix, but didn’t work