SSH command line sensor

Hi guys,

I’m trying to make a command line sensor in HA, but it keeps failing. Im trying to reach my mac mini from HA running on raspberry pi 3 v0.103.6. I want to check my disk space on the harddrives as i use my mac mini as a plex server, and want to see how much space is left for movies.
When im logged in to HA through ssh (root), i can reach my mac mini without password, and i get a succesful response when running the df -h command.

Command line sensor:

sensor:
- platform: command_line
  name: 'WD My Passport'
  command: "ssh -i /root/.ssh/id_rsa -o StrictHostKeyChecking=no -q username@ip-adress df -h | awk '/\/dev\/disk3s2/ {print $5}' | grep -o -E '[0-9]+'"
  unit_of_measurement: '%'
  scan_interval: 60

When i run the command in terminal i get the response: 98
Which means there is used 98% diskspace on my disk, which is correct.
But in HA after restart it says ‘unknown’…

Log says:
ERROR (SyncWorker_15) [homeassistant.components.command_line.sensor] Command failed: 'then the command here'

Thanks a lot!

If this is hassio or home assistant in Docker, /root/.ssh is rebuilt on restart or upgrades. You should store your keys in the mounted directory, aka, /config

Im using a bash script to read values and send them to mqtt broker.
Created MQTT Sensors to read the mqtt values

Maybe you have to change some things, i got this script from internet and was already good for my needs.

change mqtthost, user, password and topic to your needs

sudo apt-get install build-essential python-dev python-pip
sudo pip install paho-mqtt
sudo pip install psutil
sudo reboot

cd /home/pi/

nano systemdata.py

#References: Thanks to these resoruces!
#http://www.isendev.com/app/entry/39
#https://pythonhosted.org/psutil/

import sys, psutil, datetime, paho.mqtt.client as mqtt, json
from time import gmtime, strftime
import os

def getVoltage():
        try:
                res1 = os.popen('vcgencmd measure_volts core').readline()
                tmp2 = res1.replace("volt=","")
                tmp2 = tmp2.replace("V","")
                tmp2 = tmp2.replace("\n","")
                return tmp2
        except:
                return 0

def getCPUtemperature():
        try:
                res = os.popen('vcgencmd measure_temp').readline()
                tmp1 = res.replace("temp=","")
                tmp1 = tmp1.replace("'","")
                tmp1 = tmp1.replace("C","")
                #print tmp1
                return tmp1
        except:
                return 0

def bytes2human(n):
# http://code.activestate.com/recipes/577972-disk-usage/
    symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
    prefix = {}
    for i, s in enumerate(symbols):
        prefix[s] = 1 << (i+1)*10
    for s in reversed(symbols):
        if n >= prefix[s]:
            value = float(n) / prefix[s]
            return '%.1f%s' % (value, s)
    return "%sB" % n


temp1 = int(float(getCPUtemperature()))
cputemp=temp1
#cputemp = 9.0/5.0*temp1+32

voltage = getVoltage()

currtime = strftime("%H:%M:%S - %d-%m-%Y")

boottime = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%H:%M:%S - %d-%m-%Y")

topic = "YOUR TOPIC YOU WANT USE"

cpupercent = psutil.cpu_percent(interval=1)
vmem = psutil.virtual_memory().percent
diskusage =  psutil.disk_usage('/').percent
disktotal = bytes2human( psutil.disk_usage('/').total )

payload = { 'datetimedatacollected': currtime,
 'cpuusage': cpupercent, 'boottime': boottime,
 'virtualmem': vmem, 'diskusage': diskusage,
 'volt': voltage, 'cputemp': cputemp, 'disktotal': disktotal }

mqtthost = "HOST IP"
mqttuser = "USERNAME"
mqttpwd = "PASSWORD"

client = mqtt.Client()
client.username_pw_set(mqttuser, mqttpwd)
client.connect(mqtthost, 1883, 60)

payload_json = json.dumps(payload)

print (payload_json)

persistant_data = True

client.publish(topic, payload_json, 0, persistant_data)
client.disconnect()

sys.exit()


sudo crontab -e
*/5 * * * * python /home/pi/systemdata.py

What is the best way to do that? Actually i have tried it but it came with some warning so i thought is wasnt right. Something about putting the private key too open, if you know what i mean…

This won’t work with HassIO.

Put your key in the /config directory(in a subdirectory if you like)

Hoping to achieve something similar has got me considering going down this MQTT rabbit hole. (I’ve got a zig-a-zig-ah device on the way to open up access to more zigbee devices, so I’ll have mosquito up and running, may as well put it to use). You say this won’t work with HassIO and I wonder what the reason for that is? Have things changed now that there is a mosquito add-on (my assumption)? Or is there something else I should be aware of before pursuing this specific avenue?