Multiple sensors from same command_line script

Hi,
I am pretty new to Home Assistant.

I’m trying to get system status from my Unifi Security Gateway and Unifi Switch by creating a Python script that logs in to the device and gives me temperature, uptime, free memory, cpu usage.

Now I don’t want to login to the device too many times and instead i want to use one script that gives me all of the information with one run.

Getting the script to work I will manage myself but I am having trouble finding information how to implement this in Home Assistant.

To use Python scripts it seems that command_line platform should be used.
But having something like in the documentation would only result in one sensor with one value?

sensor:
  - platform: command_line
    name: Brightness
    command: "python3 /path/to/script/arest-value.py"
    unit_of_measurement: "°C"

Recap:

  • I would like the end result to be either 1 sensor with multiple attributes or several sensors.
  • I want to be able to do this with running the script only once per device. I.e. Running the script will output all information.

Thankful for any tips!

Not sure if you got any answers so far… I was having a similar issue and solved it using the template sensor. Here are all the details:

HA Version 0.37

Script Syntax - nice and simple; return a ‘:’ separated set of attributes
getdata.py:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

if __name__ == '__main__':
	 print("2:3:4")

Then in the configuration.yaml

sensor:
  - platform: command_line
    name: getdata
    command: "python3 /path/to/script/getdata.py"

  - platform: template
    sensors:
      firstvalue:
       value_template: "{% set list = states.sensor.getdata.state.split(':') %} {{ list[0] }}"
       friendly_name: 'First'
      secondvalue:
       value_template: "{% set list = states.sensor.getdata.state.split(':') %} {{ list[1] }}"
       friendly_name: 'Second'
      thirdvalue:
       value_template: "{% set list = states.sensor.getdata.state.split(':') %} {{ list[2] }}"
       friendly_name: 'Third'

*note… I found that template sensor names cannot be capitalized

Now you will have three sensors that you can reference for other automatons. I’m sure this is not the only way to do this or the best way but it’s a quick hack to at your solution.

Hope it helps

3 Likes

Hi,
Thank you for your reply.
I ended up making a cronjob which ssh:ed to my router and saved the information i wanted to a file and then i added sensors which got the information from the file:

###########################################
#    Unifi Router Status
###########################################

- platform: command_line
  name: unifi_router_uptime
  command: "sed -n 's/uptime: //p' /home/homeassistant/unifi_router_status.log"

- platform: command_line
  name: unifi_router_temp
  command: "sed -n 's/temp: //p' /home/homeassistant/unifi_router_status.log"
  unit_of_measurement: "°C"

- platform: command_line
  name: unifi_router_memusage
  command: "sed -n 's/mem: //p' /home/homeassistant/unifi_router_status.log"
  unit_of_measurement: "%"

- platform: command_line
  name: unifi_router_cpuload
  command: "sed -n 's/cpu: //p' /home/homeassistant/unifi_router_status.log"

To improve this i would rather have one sensor with multiple attributes showing uptime/memusage etc.