Help with command line senor

I am using the following command line sensor. Format looks odd, it is in system with broken out yaml files.

- platform: command_line
  name: lr_temp
  command: 'ssh [email protected] sudo ./lm75a.sh -f'
  value_template: '{{ value | float-14 | round(1) }}'
  unit_of_measurement: '°F'

When run from the terminal app in hassio I get the following:

/config ssh [email protected] sudo ./lm75a.sh -f
key_load_public: invalid format
80.375

Not sure about the key error, but it works. I have my private key saved as openssh format.

During boot and when the sensor tries to run, I get the following in my log:

2018-01-14 21:21:18 ERROR (SyncWorker_17) [homeassistant.components.sensor.command_line] Command failed: ssh [email protected] sudo ./lm75a.sh -f

What is wrong here?

Thanks Matt

Home assistant is probably executing this as a different user than you.
As a result, the path environment variable may not have the same entries as yours, and the permissions may not be the same.

Start by getting your script to execute as the same user as HA.

So, the terminal of Hassio does not use the same user as Hassio? Can you explain more?

Have you tried adding to the ssh command the full path to the ssh keyfile (e.g. /config/mykey.key) and resolving the key error? Maybe HA is getting the “invalid format” warning as an error.

So, I made this change to the command line and the error on the cli stopped:

- platform: command_line
  name: lr_temp
  command: 'ssh -i .ssh/hassio_pri_openssh_key [email protected] sudo ./lm75a.sh -f'
  value_template: '{{ value | float-14 | round(1) }}'
  unit_of_measurement: '°F'

Output on cli:

 /config ssh -i .ssh/hassio_pri_openssh_key [email protected] sudo ./lm75a.sh -f
82.175

I am still getting this error in the logs. Both of my command line sensors are doing the same thing.

018-01-15 15:31:34 ERROR (SyncWorker_12) [homeassistant.components.sensor.command_line] Command failed: ssh -i .ssh/hassio_pri_openssh_key [email protected] sudo ./lm75a.sh -f

I am using a shell script in Hassio and had to specify the path to the local file starting with /config/

command_on: 'python3 /config/camera_recording.py 6 start'

Where is your key file located? Try to put it in the config directory and change the command to

command: 'ssh -i /config/hassio_pri_openssh_key [email protected] sudo ./lm75a.sh -f'

I don’t know exactly how it works, but due to the docker structure the terminal addon can have different file access paths than HomeAssistant.

Change made to point at exact key location:

  command: 'ssh -i /config/.ssh/hassio_pri_openssh_key [email protected] sudo ./lm75a.sh -f'

Same error just different path…

Command failed: ssh -i /config/.ssh/hassio_pri_openssh_key [email protected] sudo ./lm75a.sh -f

Again, works from the command line. At a loss here, should be working.

I had loads of problems getting anything that had a space in it to run as a command. As an example @namadori 's command beginning with python3 obviously works for him, and there are lots of examples on GitHub with similar, but for me I always always always got an error.

My workaround is to put the command I want to run in a bash script, and then run the bash script (which is a path without a space in it) from HA. So to use your example:

command: 'ssh -i /config/.ssh/hassio_pri_openssh_key [email protected] sudo ./lm75a.sh -f'

Would become

command: /config/bash_scripts/lm75.sh

And then in there…

#!/bin/bash
ssh -i /config/.ssh/hassio_pri_openssh_key [email protected] sudo ./lm75a.sh -f

Points to note:

  • I have no idea why commands with spaces in work for others and not for me, so I don’t know whether I’m unique in this or if multiple users are affected and this might help.
  • Before anyone says “if it’s got a space in it needs to be in quotes” or “you have to put the full paths”, I know, I tried everything including direct copy/pastes from other working configurations, it just plain doesn’t work here.
  • I use normal homeassistant not hass.io, don’t know if that makes a difference
  • I have no idea how this potential solution will work in a command line sensor, as you’d be running one command to run a second so I don’t know what result you’ll get, all of mine with spaces are shell_commands.
1 Like

I really do not know what the problem is… The first post worked for months and then hassio blew up and I put it back into my config and it will not work. The difference was that I split up the config files on the new install and originally I had all the sensors in the configuration.yaml file.

I can only think about something related to the ssh command, maybe the PI address is not included in the known hosts and the authentication fails.
By the way, what is the -f options for? Is it an option of the sh script?

Yes, the -f tells the script to return imperial temperature data. This was working until Hassio blew up and I had to start over. My snapshots only put back the addons and files in the /config directory not the files in /config/*. Really screwed me.

Are you thinking known_hosts on the hassio install or on the remote system? I assume the hassio instance?

I seem to not have a known_hosts file on the server and performing an ssh login to the remote system is not creating one… WTH?

Not sure, but i always set the remote command in quotes.
Just give it a try.

command: 'ssh -i /config/.ssh/hassio_pri_openssh_key [email protected] "sudo ./lm75a.sh -f"'

Anyway, IMO hass.io and command_line things are a bad match.

I got it to load!!!

I had to create an ssh config file and call it in the command. As follows:

ssh -i /config/.ssh/hassio_pri_openssh_key -F /config/.ssh/config [email protected] cat /sys/bus/w1/devices/28-0000052b5166/w1_slave | tail -1 | cut -c30-35

I had to add this option to the config for the host.

StrictHostKeyChecking no
1 Like

So it was related to the Known hosts… glad you solved it :+1:

I am not sure I would say a known_hosts problem since my system is not generating a known_hosts file. Something along those lines, but I did have to build a config file for ssh. I assume I will need to add other hosts over time to it as well.

I mean, the StrictHostKeyChecking no options tells SSH to connect without looking for the host fingerprint in the known_host file. This way you bypass any error related to the fingerprint check.

I can think of two reasons for the previous error: or SSH running in non-interactive mode exits with error if it doesn’t have the host in the known_host file (it can’t ask for confirmation as it does in interactive shells), or you already connected to the same IP but with a different server’s fingerprint saved in the known_hosts.

The question that nobody answered is: in the dockerized environment of Hassio, where is the known_hosts file stored and how to access it???

Good question… there has to be a known_hosts or something that acts like it. I assumed it would be in /config/.ssh like the config file I built. It appears to remember the hosts and keys somewhere.

Eyyyy! This worked, thank you!

I went down this entire thread trying everything along the way and finally got it to work. I was trying to set up a VPN connection verification sensor to another Pi and this thread finally helped me to get it to work. Cheers!