Command_line switch templating

Hi all!

I am trying to set up a command line switch that provides an input value like this:

  - platform: command_line
    switches:
      pool_target_temp:
        command_on: |
          echo '{"command":"SetParamList","messageID":"1234","objectList":[{"objnam":"B1101","params":{"LOTMP":" {{ ((states.input_number.poolsetpointtemp) | int) }} "}}]}' | nc 192.168.xxxxxx 6681

It should set the input_number.poolsetpointtemp value into the command above. But it does not.

This works to set it to 61:

echo '{"command":"SetParamList","messageID":"1234","objectList":[{"objnam":"B1101","params":{"LOTMP":"61"}}]}' | nc 192.168.3.112 6681

Any idea how to make it work?

Thanks!

" {{ ((states.input_number.poolsetpointtemp) | int) }} "

Try adding .state and remove the spaces after the 1st " and before the last " :
“{{ ((states.input_number.poolsetpointtemp.state) | int) }}”

No, does not work either.

Same question, just yesterday.

You should use this format for your template to prevent errors if the input number is uninitialised:

{{ states('input_number.poolsetpointtemp') | int }}
shell_command:

  pool_setpoint_activate: printf '{"command":"SetParamList","messageID":"1234","objectList":[{"objnam":"B1101","params":{"LOTMP":"{{ states('input_number.poolsetpointtemp') | int }}"}}]}' | nc 192.168.xxx.xxx 6681

Is not working.

shell_command:

  pool_setpoint_activate: printf '{"command":"SetParamList","messageID":"1234","objectList":[{"objnam":"B1101","params":{"LOTMP":"86"}}]}' | nc 192.168.xxx.xxx 6681

That works. But not with the variable in it.

It’s likely to be a problem with the single quotes in the template interfering with the single quotes outside the shell command. Try it using this:

pool_setpoint_activate: printf '{"command":"SetParamList","messageID":"1234","objectList":[{"objnam":"B1101","params":{"LOTMP":"{{ states(''input_number.poolsetpointtemp'') | int }}"]}' | nc 192.168.xxx.xxx 6681

No, not working either.

I just edited my post. Try again.

Just did, sorry, still does not work. Is there some way to see the actual output?

The developer tools template editor.

Looking fine in the template editor, with just (“input_number.poolsetpointtemp”) (not ’ ').
But it does not work, unfortunately.

Can you show the output?

Sure:

echo ‘{“command”:“SetParamList”,“messageID”:“1234”,“objectList”:[{“objnam”:“B1101”,“params”:{“LOTMP”:“86”}}]}’ | nc 192.168.3.112 6681

I hope i save the day on this one. I had a nightmare of a time the past year trying to figure something out , that I know nothing about.

  temp_value2: /bin/bash -c "echo '{{ value }}' > /dev/ttyACM0"
  temp_value3: /bin/bash -c "echo '{{ states.input_text.usbtext.state }}' > /dev/ttyACM0"
  temp_value4: /bin/bash -c "echo '{{ value }}'  >> /dev/ttyACM0"

  temp_io1: /bin/bash -c "echo -n '{{ states.input_text.usbtext.state }}' >> /dev/ttyACM1"
  temp_io2: /bin/bash -c "echo -e '{{ states.input_text.usbtext.state }}' >> /dev/ttyACM1"
  temp_io3: /bin/bash -c "echo -e -n '{{states('input_text.usbtext')}}'>> /dev/ttyACM1"

see that /bin/bash before everything? that was what i was missing, while trying every variation possible, I had to make an arduino with lcd at one point to see exactly what was being received because the logging process… for a 2 year newb, is the worst. template editor always showed correct, however what was sent, was far from it.

hope that helps!

1 Like

Thanks. Unfortunately no, no change whatsoever. Any other ideas where the problem might be?

does you’re device need to have the new line character sent with the command? like it receives your json but isn’t receiving a “enter” character?

i totally understand the frustration in trying a million things and not wanting to show examples of what works and what didn’t, but when looking at your post of what works and what didn’t, i can only say from experience that the echo commands , shell commands and high quotation usage, you’re in for a doozy.

does your command work without the template code , but still with the /bin/bash stuff?

It works like this:

printf ‘{“command”:“SetParamList”,“messageID”:“1234”,“objectList”:[{“objnam”:“B1101”,“params”:{“LOTMP”:“86”}}]}’ | nc 192.168.xxx.xxx 6681

That successfully sets to 86 Farenheit.

when i googled ‘echo vs printf’ it basically says taht echo defaults by adding a new line character at the end of the string.

another 5 hours wasted down the blackhole and im back at the /bin/bash part. if I knew more about the device you were sending to and the syntax it receives in, or had code to verify what works and could copy/paste the additional template into it myself, Im of no use.

Best of luck, would love to hear what works for you.