Shell command, struggling with quotes

hi, i need to convert a shell command from a curl to a netcad

old one, that worked dimmer1old, needs to be dimmer1new
i know the command works, because lightstest also works
but dimmer1new, is a bit more complicated because i need to get the state of an input number

basicly , after the “\x” , there is a number of 2 decimals adedd (example 99)
the dimmer1old was a test AFAF450099 (where 99 is the state of the input number)

# dimmer1old: "curl -k http://ip.php?hex=AFAF4500{{ '%02.f'%((states('input_number.dimmer1') | float / 255 ) * 90) }}" 
# dimmer1new: echo -e "\xAF\xAF\x45\x00\x{{ '%02.f'%((states('input_number.dimmer1') | float / 255 ) * 90) }}" | nc 192.168.0.10 1001

# lightstest: echo -e "xAF\xAF\x53\x1E\x01" | nc 192.168.0.10 1001

Do you need to "http:// the ip address at the end of “newstring”?

You can’t use a pipe in a shell_command that contains a template. From the doc page:

The commands can be dynamic, using templates to insert values for arguments. When using templates, shell_command runs in a more secure environment which doesn’t allow any shell helpers like automatically expanding the home dir ~ or using pipe symbols to run multiple commands.

ok, let me explain, i have at home some lights and dimmers, i need to send an HEX string to my IP controller
the hex string is representing a light or a dimmer , so sending 01 a the end is turning the ligt on, sending 00 is turning it off, and sending 00-90 , is representing a brightness value , so i work with switches for lights, and input_number for dimmers

before i created a PHP script, that i place on a webserver, that sends the string to my controller, but there is a small delay, also, if webserver is down, i cant turn on/off lights anymore , so i want the php/webserver complete removed, …
so now i do it directly from HA, by sending those values with a shell command like NC

i have converted the lights already succesful, its just only the dimmer, i need a variable from the input number, it was working with the http script, but now i just struggle with the quotes
at the end of the echo -e “\xAF\xAF\x45\x00\x” … here needs to be the varable 00-99 , ending with a "

i hope its a bit clear now

#working old (not used anymore)
command_on: 'curl -k "http://ip/php.php?AFAF531D01"'
#working old (not used anymore)
command_off: 'curl -k "http://ip/php.php?hex=AFAF531D00"'
#working old (still using)  
dimmer1: "curl -k http://ip/php.php?hex=AFAF4500{{ '%02.f'%((states('input_number.dimmer1') | float / 255 ) * 90) }}"

#working      
command_on: echo -e "\xAF\xAF\x53\x1D\x01" | nc 192.168.0.10 1001
#working      
command_off: echo -e "\xAF\xAF\x53\x1D\x00" | nc 192.168.0.10 1001
#not working ....
dimmer1 :  echo -e "\xAF\xAF\x45\x00\x{{ '%02.f'%((states('input_number.dimmer1') | float / 255 ) * 90) }}" | nc 192.168.0.10 1001

I understand what you’re saying, but what I’m saying is, if you use a shell pipe in your shell_command, that won’t work if the shell_command contains a Jinja template, which your non-working dimmer1 shell_command does.

When you say “not working”, it’s usually helpful to explain how it’s not working. E.g., are there any errors?

My guess would be, if you need to have one command’s output piped into another command, and you need to use a Jinja template, then you’ll need to put the two commands in a shell script which you call from your HA shell_command, passing the shell script the result of the Jinja template.

Ah ok well , I don’t know the error, I don’t have control or output from that IP controller…
Ok , makes sense now… So a Jinja template is this stuff… {{ ‘%02.f’%((states(‘input_number.dimmer1’) | float / 255 ) * 90) }}. ??

Ok, gonna make a python script then , so this should work then … gonna pass it as an variable in a script
Dimmer: python3 script.py {{ '%02.f'%((states('input_number.dimmer1') | float / 255 ) * 90) }}

1 Like