How can I send variables (e.g. sensor data) to a shell script by using shell_command?
I need values of Home Assistant in the shell script. Or is there a better way?
How can I send variables (e.g. sensor data) to a shell script by using shell_command?
I need values of Home Assistant in the shell script. Or is there a better way?
Here is what I am using (not sure this is what you are looking for), in configuration.yaml you create a shell command with your script. Example:
shell_command:
myfox: /bin/bash /config/myfox_ini.sh "{{ arguments }}"
In the automations.yaml, I run the script and pass the arguments to it:
action:
- service: shell_command.myfox
data_template:
arguments: 22171,disarmed
In the script, I read the arguments receivedā¦ Done !
Let me know if you need more or something elseā¦
I think that meets my needs very well. Thank you very much.
I guess you send two variables, which is pretty similar to what I want to do.
Do you mind showing me the code for the shell side as well?
Sureā¦ I am using an ā_iniā script to launch the script (myfox.sh), this is a workaround to the 60 seconds maximum allowed for a script to run within HA.
Here is the script:
#!/bin/bash
#
#
args=$1
args=${args// }
site=${args%%,*}
command=${args#*,}
if [ $site = "22171" ];
then
bash /config/myfox.sh "$1" &>> /config/myfox_22171.log &
else
bash /config/myfox.sh "$1" &>> /config/myfox_84807.log &
fi
Nice.
I tried doing this:
arguments: '{{states('sensor.1')}}, {{states('sensor.2')}}'
Like this the first value should be $1 and the second $2. However the first contains text and spaces:
This is some text 2773
To work, it should be like this:
"This is some text" 2773
But even if I add " to the code it doesnāt seem to work.
arguments: '"{{states('sensor.1')}}", {{states('sensor.2')}}'
I will try something like this. I donāt know if HA or bash has issues with spaces.
First, I replace spaces in HA with underlines.
|replace(' ', '_')
Inside the shell script I do the opposite:
var1=${1//_/ }
Hope this will work.
You should try this (combining quotes and double quotes//remove the comma):
arguments: '{{states("sensor.1")}} {{states("sensor.2")}}'
It is not working either. It is crazy no matter what I do it always store everything in $1:
Result: $1 = This is text 384
This is not working - no output. If I change the $imgstatus with test1.png it works.
Besides of that I still have the problem that $1 contains all the arguments.
#!/bin/sh
status1=$1
status2=$2
if [[ $status1 = *test1* ]]
then
imgstatus = "test1.png"
fi
if [[ $status1 = *test2* ]]
then
imgstatus = "test2.png"
fi
magick composite -compose atop -gravity Center -geometry -600-100 \( $imgstatus -resize 500x500 \) leer.png status.png
Did you try:
shell_command:
myfox: /bin/bash /config/myfox_ini.sh "{{ arguments1 }}" "{{ arguments2 }}"
action:
- service: shell_command.myfox
data_template:
arguments1: 22171
arguments2: disarmed
That works and makes sense, thank you.