Get sensor data from shell script with continous output (expect + SSH)

Hey Home Assistant Community,

Quick background to what I’m trying to do:
I have a managed network switch that allows SSH connections and provides a limited shell with commands to monitor the device. Unfortunately it is only possible to retrieve some data this way, which is not possible to fetch over SNMP (like PoE power consumption per port, in this case).
So I wrote a bash script to connect to the switch via SSH, run the command to print the data on the switch and parse it to JSON on a 2 second interval.
The script opens the SSH session once, then runs the command on the switch repeatedly without closing the SSH session (I use a expect script to do this). I do it this way instead of re-connecting on every interval, because when connecting to the switch you can notice that it takes 1 to 3 seconds to even authenticate via SSH, so I keep the session open and just run the command repeatedly on the switch once logged in.

This is where we get to the issue: I want to create a command_line sensor from the data retrieved by my script (JSON array with information about each 8 ports). Looking at the documentation for command_line, I see that Home Assistant has it’s own interval to run the script, but this is exactly what I’m trying to avoid. I basically just want to execute my script once which should keep running until HA stops, then “tail” it’s output and load it as sensors for each of the 8 ethernet ports.

How I understand it is, that using command_line it’s not possible to constantly tail the output of a running script and avoid it’s own interval (correct me if I’m wrong, because this would be optimal).

So my other idea would be to create some kind of automation that runs a shell_script on HA startup, then modify my script to output the data to something like /tmp/switch-data.json and using command_line and it’s interval option to just read this file every 2 seconds using HA’s own interval and parse it as sensors.

Am I on the right path? Is there a more optimal solution to this problem?

Thank you in advance! :slight_smile:

You are correct that using a command line sensor to invoke your long running script isn’t going to work. If you’re outputting the data to a file, you could use a file sensor. It doesn’t support a scan_interval configuration, but you can set up an automation to run on a time pattern to call update_entity to get it to refresh.

You could also consider using the REST API to push the data into HA. You wouldn’t need to define or configure this sensor ahead of time, just sending a POST to /api/states/<entity_id> will create the entity if it doesn’t already exist.

Good Day,

Did you get your code sorted?

I created a bash script to turn on/off network ports on my Cisco switch using “ssh” and “expect” (turn on/off POE indoor cameras). The script works fine on a separate Debian system. But I want to put the script on HomeAssistant and create buttons/automations with the script. My HA is running on a dedicated machine as HA OS.

My first issue is that the terminal does not have the “expect” command and I’m not sure how to install it or how to go about running the script on HA OS. Then next is to integrate the script into automations and buttons.

Is this too dissimilar of a shell script issue from your original post? Any pointers in the direction I should go will be helpful. Thanks!