apivaral
(Alejandro Pivaral)
July 4, 2021, 12:12am
1
Hi,
I have this input number in configuration:
input_number:
denon_level_center:
name: Level Center
min: -12
max: 12
step: 0.5
unit_of_measurement: 'dB'
Automations have this that gets the number and sends the correct value:
- id: '1625292799637'
alias: Denon Level Center
description: ''
trigger:
- platform: state
entity_id: input_number.denon_level_center
condition: []
action:
- service: shell_command.denon_level_center
data:
value: "{{ ((states('input_number.denon_level_center')|float*10)+500)|int }}"
mode: single
This should call this Shell Command, it works fine if I enter the value manually, but doesn’t get the value from automations, service, data, value:
denon_level_center: echo -e PSCLV {{ value }} | nc 192.168.1.218 23
What’s wrong with automations or shell command? Why not passing value?
koying
(Chris B)
July 4, 2021, 8:41am
2
You can’t pass variables to shell_command, only to scripts.
Why not just
denon_level_center: "echo -e PSCLV {{ ((states('input_number.denon_level_center')|float*10)+500)|int }} | nc 192.168.1.218 23"
1 Like
apivaral
(Alejandro Pivaral)
July 6, 2021, 7:29am
3
You code seems perfect! But no idea why this isn’t working, I can send the code manually, but jinja2 template not working in shell commands, it works in automations, but not in shell commands, any idea why?
koying
(Chris B)
July 6, 2021, 7:41am
4
I don’t really understand what you mean by that…
apivaral
(Alejandro Pivaral)
July 6, 2021, 7:43am
5
That code works in automations, I tried sending a push to my mobile and it works perfectly.
But in shell_command it doesn’t work correctly, if I change the jinja2 template to just a number 500 it works fine.
apivaral
(Alejandro Pivaral)
July 6, 2021, 7:46am
6
I think I found the trouble, the PSCLV 500 doesn’t work, but “PSCLV 500” works, how do I add double quotes “ to jinja template? Seems like I only need to add that.
Already tried this and doesn’t work:
denon_level_center_set: echo -e "PSCLV {{ ((states('input_number.denon_level_center')|float*10)+500)|int }}" | nc 192.168.1.8 23
But this does work:
denon_level_center_set: echo -e "PSCLV 500" | nc 192.168.1.8 23
Can’t get dynamic values working in shell commands, have been trying for hours.
koying
(Chris B)
July 6, 2021, 8:22am
7
Try
denon_level_center: 'echo -e "PSCLV {{ ((states(''input_number.denon_level_center'')|float*10)+500)|int }}" | nc 192.168.1.218 23'
apivaral
(Alejandro Pivaral)
July 6, 2021, 9:06am
8
Sorry, I tested again and this works: (without the quotes)
denon_level_center_set: echo PSCLV 500 | nc 192.168.1.8 23
The line you sent, should work like, but it’s not working:
denon_level_center_set: "echo PSCLV {{ ((states('input_number.denon_level_center')|float*10)+500)|int }} | nc 192.168.1.8 23"
Any other idea of what I can try?
koying
(Chris B)
July 6, 2021, 10:31am
9
I’ve edited the shell command
EDIT: Edited again to remove the (apparently) unneeded double-quotes
Sand
July 6, 2021, 10:37am
10
If you put the command on a new line you can at least avoid the outer quotes.
shell_command:
denon_level_center_set: >-
your_command
1 Like
koying
(Chris B)
July 6, 2021, 10:40am
11
Also, set you HA in debug to capture the stdout/stderr of the shell command.
1 Like
apivaral
(Alejandro Pivaral)
July 6, 2021, 6:07pm
12
This one works perfect:
denon_level_center_reset: >-
echo PSCLV 500 | nc 192.168.1.8 23
This one doesn’t work:
denon_level_center_set: >-
echo PSCLV {{ ((states('input_number.denon_level_center')|float*10)+500)|int }} | nc 192.168.1.8 23
Logs return this:
2021-07-06 11:59:07 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: echo PSCLV {{ ((states('input_number.denon_level_center')|float*10)+500)|int }} | nc 192.168.1.8 23
, return code: 0: b’PSCLV 510 | nc 192.168.1.8 23\n’
Not sure what that b’ means, final command should be echo PSCLV 510 | nc 192.168.1.8 23
koying
(Chris B)
July 6, 2021, 7:15pm
13
Strange. It seems the pipe is passed as such to the echo when using templates.
apivaral
(Alejandro Pivaral)
July 7, 2021, 5:13am
14
Anyone haves an idea why this is happening? Thanks in advance!
koying
(Chris B)
July 7, 2021, 6:38am
15
Without ideas on the root issue, a workaround could be:
Create a simple bash script in, e.g., /config/PSCLV.sh
#!bash
echo PSCLV $1 | nc 192.168.1.8 23
denon_level_center_set: >-
bash /config/PSCLV.sh {{ ((states('input_number.denon_level_center')|float*10)+500)|int }}