Alright, because I’m normally typing on a phone I have now transitioned to a computer and will explain why this bothers me. For people on Tweakers this wil be kind of a cross post (not a literal copy but the same errors addressed). I’m sorry for that.
So. The workaround for sending UDP works. Kinda. This works:
shell_command:
send_smth: "echo -n 'hello world' | nc -u -w1 192.168.123.1 54321"
This also works (shell_command > send_vol):
"echo -n 'volume 100' | nc -u -w1 192.168.12.34 65432"
and
'echo -n "volume 100" | nc -u -w1 192.168.12.34 65432'
So far so good. But then try to send a slider state and trouble begins.
I have tried so many workarounds by putting the slider value in an input_text or what not so this will hopefully work but the thing is that when combining the string and the formatting there is no way to escape the pipe ("|") which breaks the command.
A few options I have tried (most of them I already tossed out but these are still commented out in my script as there were reeaally close to the solution but no cigar:
[1]
shell_command:
send_vol: 'echo -n {{ states("input_text.tekstveld") }}'
where input_text.textveld is a string containing the slider value as a string and the remaining command; | nc -u -w1 192.168.123.1 54321
And these:
[2]
shell_command:
send_vol: "{{ states('input_text.tekstveld') }}"
Where I have the complete string into the text field.
And all these iterations of send_vol:
[3]
'echo -n volume {{ states("input_number.vol_bak") }} | nc -u -w1 192.168.123.1 54321'
"echo -n 'volume {{ vol_data }}' | nc -u -w1 192.168.123.1 54321"
'echo -n ''volume {{ vol_data }}'' | nc -u -w1 192.168.123.1 54321'
'echo -n ''volume {{ (states('input_number.vol_bak').split('.')[0]) }}'' | nc -u -w1 192.168.123.1 54321'
"echo -n 'volume {{states(''input_number.vol_bak'')}}' | nc -u -w1 192.168.123.1 54321"
"echo -n 'volume {{ states(''input_text.tekstveld'') }}' | nc -u -w1 192.168.12.34 54321"
Where:
vol_data = the slider value split and set to txt string
input_number.vol_bak = the actual slider defined in automations.yaml which is defined as follows:
[4]
input_number:
vol_bak:
name: "Volume Mediabak"
initial: 0
min: 0
max: 100
step: 1
input_text.tekstveld = only the value in this instance ([3]) as seen in the automations.yaml during that phase as:
[5]
- id: 'xxxxxxxxxxx'
alias: Volume mediabak
trigger:
- entity_id: input_number.vol_bak
platform: state
action:
- service: shell_command.volume_mediabak
data_template:
vol_data: "{{ '%s' % states('input_number.vol_bak') }}"
- service: input_text.set_value
data_template:
entity_id: input_text.tekstveld
value: "{{'%s' % (states('input_number.vol_bak').split('.')[0]) }}"
FROM HERE ON I WILL GO BACK IN TIME TO MY BACK TRACKING THE ISSUE ON ANOTHER FORUM:
I have tried amongst others, these, in templates (dev tools), a few of which I knew wouldn’t work but tried just to falsify:
[6a]
'echo -n "volume {{ states('input_number.vol_bak').split('.')[0] }}" | nc -u -w1 192.168.12.34 54321'
'echo -n "volume {{ states('input_number.vol_bak').split('.')[0] }}" | nc -u -w1 192.168.12.34 54321'
"echo -n "volume {{ states('input_number.vol_bak').split('.')[0] }}" | nc -u -w1 192.168.12.34 54321"
"echo -n ''volume {{'%s' % (states('input_number.vol_bak').split('.')[0]) }}'' | nc -u -w1 192.168.12.34 54321"
"echo -n ''volume {{ states('input_number.vol_bak').split('.')[0] | int }}'' | nc -u -w1 192.168.12.34 54321"
"echo -n ''volume {{ states('input_number.vol_bak').split('.')[0] }}'' | nc -u -w1 192.168.12.34 54321"
"echo -n ''volume {{ states('input_text.tekstveld') }}'' | nc -u -w1 192.168.12.34 54321"
{{ states("input_number.vol_bak") }}
{{ states("input_number.vol_bak").split('.')[0] }}
"echo -n 'volume {{ states('vol_data') }}' | nc -u -w1 192.168.12.34 54321"
"echo -n 'volume {{ vol_data }}' | nc -u -w1 192.168.12.34 54321"
The test template outcome was:
[6b]
'echo -n "volume 80" | nc -u -w1 192.168.12.34 54321'
'echo -n "volume 80" | nc -u -w1 192.168.12.34 54321'
"echo -n "volume 80" | nc -u -w1 192.168.12.34 54321"
"echo -n ''volume 80'' | nc -u -w1 192.168.12.34 54321"
"echo -n ''volume 80'' | nc -u -w1 192.168.12.34 54321"
"echo -n ''volume 80'' | nc -u -w1 192.168.12.34 54321"
"echo -n ''volume 80'' | nc -u -w1 192.168.12.34 54321"
80.0
80
"echo -n 'volume unknown' | nc -u -w1 192.168.12.34 54321"
"echo -n 'volume ' | nc -u -w1 192.168.12.34 54321"
So testing and testing I found the root cause of these not working:
[7a]
"echo -n 'volume {{ (states('input_number.vol_bak').split('.')[0]) }}' | nc -u -w1 192.168.12.34 65432"
"echo -n ''volume {{ (states('input_number.vol_bak').split('.')[0]) }}'' | nc -u -w1 192.168.12.34 65432"
"echo -n \'volume {{ (states('input_number.vol_bak').split('.')[0]) }}\' | nc -u -w1 192.168.12.34 65432"
"echo -n 'volume {{ (states(''input_number.vol_bak'').split(''.'')[0]) }}' | nc -u -w1 192.168.12.34 65432"
"echo -n 'volume {{ (states('input_number.vol_bak').split('.')[0]) }}' | nc -u -w1 192.168.12.34 65432"
"echo -n ""volume {{ (states('input_number.vol_bak').split('.')[0]) }}"" | nc -u -w1 192.168.12.34 65432"
"echo -n \"volume {{ (states('input_number.vol_bak').split('.')[0]) }}\" | nc -u -w1 192.168.12.34 65432"
Is this error:
[7b]
2020-07-30 21:44:22 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `echo -n 'volume {{ (states('input_number.vol_bak').split('.')[0]) }}' | nc -u -w1 192.168.12.34 65432`, return code: 0:
b'volume 47 | nc -u -w1 192.168.12.34 65432'
Which couldn’t be helped by using triple quotes either, because:
[7c]
Error loading /config/configuration.yaml: while parsing a block mapping
in "/config/configuration.yaml", line 51, column 3
expected <block end>, but found '<scalar>'
in "/config/configuration.yaml", line 57, column 22
Or even using escapes in front of the quotes (’ {data}’):
Error loading /config/configuration.yaml: while scanning a double-quoted scalar
in "/config/configuration.yaml", line 57, column 20
found unknown escape character "'"
in "/config/configuration.yaml", line 57, column 30
So I tried the quotes in formatting too:
[8a]
"echo -n {{'\"volume ' + (states('input_number.vol_bak').split('.')[0]) + '\"' }} | nc -u -w1 192.168.12.34 65432"
Which leads to
[8b]
2020-07-30 22:14:40 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `echo -n 'volume {{ (states('input_number.vol_bak').split('.')[0]) }}' | nc -u -w1 192.168.12.34 65432`, return code: 0:
b'volume 79 | nc -u -w1 192.168.12.34 65432'
So then a double check if the automation var will not work:
[9a]
"echo -n {{'\"volume ' + vol_data + '\"' }} | nc -u -w1 192.168.12.34 65432"
Nope:
[9b]
2020-07-30 22:24:17 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `echo -n {{'"volume ' + vol_data + '"' }} | nc -u -w1 192.168.12.34 65432`, return code: 0:
b'volume 68 | nc -u -w1 192.168.12.34 65432'
So then I checked all my previous Errors and suddenly I saw the light that the real issue is parsing. You actually see that shell command is echoing, right? It’s just not piping but actually echoing back the string to HA!
An extra check of this, now using vol_data completely parsed and ready to be sent out as a string:
Automations.yaml
- id: '1234567891011'
alias: Volume mediabak
trigger:
- entity_id: input_number.vol_bak
platform: state
action:
- service: input_text.set_value
data_template:
entity_id: input_text.tekstveld
value: 'echo -n volume {{ states("input_number.vol_bak").split(".")[0] }} | nc -u -w1 192.168.12.34 65432'
- service: shell_command.volume_mediabak
data_template:
vol_data: "{{ states('input_text.tekstveld') }}"
mode: single
configuration.yaml
shell_command:
volume_mediabak: "echo -n {{vol_data}}"
You see what I did there? Double echo
Gives:
2020-07-31 12:30:14 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `echo -n {{vol_data}}`, return code: 0:
b'echo -n volume 18 | nc -u -w1 192.168.12.34 65432'
So try this again without the extra echo:
shell_command:
volume_mediabak: "{{vol_data}}"
Gives:
2020-07-31 12:31:41 DEBUG (MainThread) [homeassistant.components.shell_command] Stderr of command: `{{vol_data}}`, return code: 127:
b'/bin/sh: {{vol_data}}: not found\n'
2020-07-31 12:31:41 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `{{vol_data}}`, return code: 127
NoneType: None
Close, but no cigar.
So there we are. I have been wasting weeks of my time to accomplish something that is ridiculously simple to make in python. It’s no more than three lines of code. But this ‘workaround’ with netcat blows serious chunks.