Command works in HA terminal, doesn’t as command line sensor

I’m trying to make a command-line sensor. This command will check if my vpn tunnel can ping to the outside world. It’s working as it should from the HA terminal but not as a sensor. What am I doing wrong?
This is the code:

- platform: command_line
  name: "VPN Tunnel Status"
  command: ssh -i /config/.ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] iocage exec wgvpn ping -c 1 google.com &> /dev/null && echo "Online" || echo "Offline"
  scan_interval: 90
  #scan_interval: 43200 #every 12 hours
  value_template: "{{ value }}"

When checking this new sensor in Developer Tools the state of this sensor will always be Offline. I think it has something to do with the quotation marks. Looked up some examples in this forum but couldn’t find a working solution. Any ideas?

Yeah, 1st idea would be to post your logs.

Argh! What a rookie mistake for not checking the logs. I looked at the logs but didn’t find any messages related to this sensor. Another thing I tried: turning on the debug log. No weird stuff there.

logger:
  default: debug

What I do have are SSH errors on the server where the VPN tunnel is running. Something with a wrong password. Fixing that could be the solution for this.

Found a solution. I had to figure out first why the command didn’t work. Someone posted a helpful tip here, The suggestion was to append 2> /config/command.log to the end of the ssh command in order to write the output from the command to a file. Which got me this error:

Permission denied, please try again.
[email protected]: Permission denied (publickey,password).

Some error with ssh keys for sure.
And then I stumbled upon this post from user @CentralCommand:

Finally understood why my code wasn’t working.
This is the solution:

ssh -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] -i /config/.ssh/id_rsa 'iocage exec wgvpn ping -c 1 google.com &> /dev/null && echo "Online" || echo "Offline"'
2 Likes