Unable to get a command line sensor working

Hi,
I’m using the Garbage Collection integration and its working well for the normal recurring frequencies. Now I’m trying to get it working for the manual frequency where I can load the dates from a text file. I’m following the instructions on the git page to setup the command line sensor like this:

  - platform: command_line
    name: Import Yard Waste Dates
    command: "sh /shell/import_yard_waste_dates.sh"

Here’s the import_yard_waste_dates.sh file (copied from git but just updated the file name):


#!/bin/bash
new_dates=$(for x in $(cat /shell/yard_waste_dates.txt); do
                if [[ $(date -d $x +"%y%m%d") -ge $(date +"%y%m%d") ]]; then
                    echo $x
                fi
            done)
echo "$new_dates" | head -n20

And here’s the text file:

220619
220710
220724
220807
220828
220918
221002
221016
221030
221113
221120
221127
221211

I see the following error in the logs:
ERROR (SyncWorker_0) [homeassistant.components.command_line] Command failed: sh /shell/import_yard_waste_dates.sh

I can’t find much about how to setup a command line sensor with the SH command. How can I figure out why this is failing?
I’m not sure if any of this matters, but I’m running HA OS in a Proxmox VM.

Thanks.

HA probably does not have access to /shell.

I tried moving the script and txt file to the Config folder and seeing the same issue. Is there some special permissions needed to run these scripts? Thanks.

Running a shell script with “bash-isms” with sh won’t work. Didn’t you try the command?
Replace sh with bash

No I hadn’t tried it. I’m pretty new to this and didn’t know how to debug this further, and this was the command also provided within the Garbage Collection integration. I’ll replace sh with bash and if that doesn’t work, try to find how I can test it independently of the command line sensor.
Thanks for the feedback.

You do not need to specify sh script or bash script if you specify that on the first line of script.

I was finally able to get this working. The path needed to be updated:

  - platform: command_line
    name: Yard Waste Dates
    command: "sh /config/shell/import_yard_waste_dates.sh"

Like I said :slight_smile:

And make the script executable :wink:

True, and relevant for a newbie :slight_smile: