Command_line sensor code not working

I created a command_line sensor that returns the current time and date and publish three mqtt message every time it update its content. The sensor is updating with the new date and time every minute as intended but it is not publishing the mqtt messages. When I connect to my Hassio (v0.57.2 on Rpi3) over ssh and run the script, everything works fine. There is no error message on the home assistant log. Do you guys know what is the problem? I created the sensor as an easy way to run some bash script every minute.

sensor:

- platform: command_line
  name: Envoy
  command: 'sh /config/scripts/check.sh '

my check.sh file has the following content:

#!/bin/bash

data=$(curl -s 'http://192.168.0.107/production.json')
prod=$(echo ${data} | jq '.production')
production=$(echo ${prod} | jq '.[1] | .wNow')

cons=$(echo ${data} | jq '.consumption')
consumption=$(echo ${cons} | jq '.[0] | .wNow')

net=$(echo ${cons} | jq '.[1] | .wNow')

mosquitto_pub -h 192.168.0.2 -t envoy/solar/production -m $production
mosquitto_pub -h 192.168.0.2 -t envoy/solar/consumption -m $consumption
mosquitto_pub -h 192.168.0.2 -t envoy/solar/net -m $net

date

First, confirm that you are in fact trying to run a script that lives at /config/scripts and not ~/.homeassistant/config/scritps

Second, it’s likely the user environment for hass is different from the user environment in your interactive session. Typically there are things in your $PATH environment variable that do not exist for the hass user.

The easiest way to troubleshoot this is to pipe errors from your commands to a known file location.
You can do that like this (I’m assuming here that /config/scripts is a path that exists on your machine).

- platform: command_line
  name: Envoy
  command: 'sh /config/scripts/check.sh >> /config/scripts/check.log 2>&1'

This will send all the output from your commands to the log file (stdout and stderr)

If you see “command not found” errors, you can either modify the $PATH environment variable for the hass user, or do what I did and hard-code the path to all of your commands.

So for example

data=$(curl -s 'http://192.168.0.107/production.json')_

becomes

data=$(/usr/bin/curl -s 'http://192.168.0.107/production.json')

Try changing your command line sensor to:

    - platform: command_line
      name: Envoy
      command: 'bash /config/scripts/check.sh'

I’ve had problems in the past with that.

Thank you for the quick reply guys. I have tried both suggested solutions but unfortunately I am getting the following errors on the check.log

/config/scripts/check.sh: line 4: /usr/bin/jq: No such file or directory
/config/scripts/check.sh: line 5: /usr/bin/jq: No such file or directory
/config/scripts/check.sh: line 7: /usr/bin/jq: No such file or directory
/config/scripts/check.sh: line 8: /usr/bin/jq: No such file or directory
/config/scripts/check.sh: line 10: /usr/bin/jq: No such file or directory
/config/scripts/check.sh: line 12: /usr/bin/mosquitto_pub: No such file or directory
/config/scripts/check.sh: line 13: /usr/bin/mosquitto_pub: No such file or directory
/config/scripts/check.sh: line 14: /usr/bin/mosquitto_pub: No such file or directory
Tue Nov 14 21:31:57 AEDT 2017
/config/scripts/check.sh: line 4: /usr/bin/jq: No such file or directory
/config/scripts/check.sh: line 5: /usr/bin/jq: No such file or directory
/config/scripts/check.sh: line 7: /usr/bin/jq: No such file or directory
/config/scripts/check.sh: line 8: /usr/bin/jq: No such file or directory
/config/scripts/check.sh: line 10: /usr/bin/jq: No such file or directory
/config/scripts/check.sh: line 12: /usr/bin/mosquitto_pub: No such file or directory
/config/scripts/check.sh: line 13: /usr/bin/mosquitto_pub: No such file or directory
/config/scripts/check.sh: line 14: /usr/bin/mosquitto_pub: No such file or directory
Tue Nov 14 21:32:59 AEDT 2017

The command curl, date and echo are recognised but mosquitto_pub and jq are not.
I used the full path /usr/bin returned by the command which.
Just to clarify mosquitto_pub and jq are installed in the machine. The script runs fine on the ssh.

Any more suggestions?

So, based on treno’s suggestion I replaced my check.sh script for the following:

#!/bin/bash

echo $PATH >> /config/scripts/check.log 2>&1
which jq >> /config/scripts/check.log 2>&1
which mosquitto_pub >> /config/scripts/check.log 2>&1
ls /usr/bin >> /config/scripts/check.log 2>&1

I can confirm now that jq and mosquitto_pub are no installed on the sandbox enviroment used by command_line component. I think my only option now is create a add-on.

P.S.: The $PATH environment variable is exactly the same of the ssh shell

With a little bit of workaround and tries, I manage to get the script working. I had to copy the mosquitto_pub, jq and its libraries to my /config/scripts folder and then when executing the script copy the libraries back to the /usr/lib folder. I had to put a condition to check if the file already exist to avoiding copying the file every loop. I also added the /config/scripts to the variable $PATH to avoid having to use full path on the script.

That is my final script:

#!/bin/bash

PATH=$PATH:/config/scripts/bin

if [ ! -f /usr/lib/libmosquitto.so.1 ]; then
    cp /config/scripts/bin/*.so* /usr/lib
fi

data=$(curl -s 'http://192.168.0.107/production.json')
prod=$(echo ${data} | jq '.production')
production=$(echo ${prod} | jq '.[1] | .wNow')

cons=$(echo ${data} | jq '.consumption')
consumption=$(echo ${cons} | jq '.[0] | .wNow')

net=$(echo ${cons} | jq '.[1] | .wNow')

mosquitto_pub -h 192.168.0.2 -t envoy/solar/production -m $production
mosquitto_pub -h 192.168.0.2 -t envoy/solar/consumption -m $consumption
mosquitto_pub -h 192.168.0.2 -t envoy/solar/net -m $net

date

Thank you guys for the assistance. If anyone has a better solution I am still open for sugestions.