Check for existence of file using shell_command

How can I get and use a return of a shell script like this?

find -f /path/file

Not sure if this helps you, see example below. I use bash variables to capture the output of commands and the create return value by ‘echoing’ a JSON string for Home Assistant command line sensor:

Good hunting!

# bash script that is called by HA command line sensor

#!/bin/bash
# network-bandwidth.sh
# 202010161015

INTERVAL="1"  # update interval in seconds

if [ -z "$1" ]; then
        echo
        echo usage: $0 [network-interface]
        echo
        echo e.g. $0 eth0
        echo
        exit
fi

IF=$1

R1=`cat /sys/class/net/$1/statistics/rx_bytes`
T1=`cat /sys/class/net/$1/statistics/tx_bytes`
sleep $INTERVAL
R2=`cat /sys/class/net/$1/statistics/rx_bytes`
T2=`cat /sys/class/net/$1/statistics/tx_bytes`
TBPS=`expr $T2 - $T1`
RBPS=`expr $R2 - $R1`
TKBPS=`expr $TBPS / 1024`
RKBPS=`expr $RBPS / 1024`

#echo $TBPS
#echo $RBPS
#echo $TKBPS
#echo $RKBPS


#echo -e "{\"Hello\"}\n"
#echo "{\"timestamp\": $(date '+%Y%m%d%H%M%S'), \"interface\": \"$1\", \"speed\": {\"out\": $TKBPS, \"in\": $RKBPS}, \"units\": \"kB/s\"}"
echo "{\"timestamp\": $(date '+%Y%m%d%H%M%S'), \"interface\": \"${1}\", \"out\": \"${TKBPS}\", \"in\": \"${RKBPS}\", \"units\": \"kB/s\"}"

# HA sensor setup

# network bandwidth
  - platform: command_line
    name: Network Bandwidth
#    command: "shell_scripts/network-bandwidth.sh enp12s0f0" # builtin ethernet on macmini2012, flaky
    command: "shell_scripts/network-bandwidth.sh enxd0374516f37c" # usb ethernet on macmini2012
#    command: "shell_scripts/network-bandwidth.sh eth0"
#    commmand: "shell_scripts/network-bandwidth.sh wlan0"
    unit_of_measurement: "kB/s"
    value_template: "{{ value_json }}"
#    json_attributes_topic: '["timestamp","interface","in","out","units"]'
    json_attributes: 
      - "timestamp"
      - "interface"
      - "in"
      - "out"
      - "units"
  - platform: template
    sensors:
      transmission_down_speed_kbps:
        friendly_name: "Transmission Down Speed"
        unit_of_measurement: 'kB/s'
        value_template: "{{ state_attr('sensor.network_bandwidth', 'in') }}"

      transmission_up_speed_kbps:
        friendly_name: "Transmission Up Speed"
        unit_of_measurement: 'kB/s'
        value_template: "{{ state_attr('sensor.network_bandwidth', 'out') }}"

1 Like

Very helpful, I will try that.

Thanks

I am still figuring out, how to simplify this for my case:

test.sh

FILE=/path/to/file
if [ -f "$FILE" ]; then
    echo "on"
else 
    echo "off"
fi
 - platform: command_line
    name: FileExist
    interval: 60
    command: "scripts/test.sh"
    value_template: "{{ ??? }}"

Is an interval even possible or has the script to run in loop?
How do I get the echo as a value?

I think the frequency option for how often the command line sensor runs in HA is :

scan_interval : <number of seconds>

If both script and .yaml setup correctly, after restarting HA, on the developer page you should see a sensor name ‘FileExist’ with values of : “on”/“off”/“unknown”

I use the ‘Check Configuration’ button after I make any changes to my yaml files, the restart and check the home assistant log file for issues.

In my example that I showed, I get a JSON string back in the raw ‘Network Bandwidth’ sensor, then I use two template sensors to extract out integer numbers as the values for the ‘Transmission Down Speed’ and ‘Transmission Up Speed’ sensors.

1 Like

Thanks for the additional details.

 - platform: command_line
    name: FileExist
    scan_interval: 60
    command: "scripts/test.sh"
    value_template: "{{ value }}"