WTH isn’t UDP comms integrated in HA?

UDP is an essential comms protocol used by many devices. On my computers I run a python script that listens and sends on certain UDP ports.

As I know it’s easy to include UDP binding and sending in Python I wonder why it isn’t part of the HA integration.

Now I have to send my commands to netcat via a service call to shell command. Which works, sort of. When you have no variables in your string. Or else the shell command breaks when it reaches the pipe symbol.

I’ve been trouble shooting this a week long and gave up. I want to send things like a volume slider to a UDP listener so I can control the volume of that device. That’s not possible now.

It would also be neat if HA could respond to incoming UDP msg’s of course.

It blows my mind that this isn’t integrated and I have to resort to half broken commands.

Probably because mqtt with qos 0 is easier to implement as there is a broker addon.

They are not mutually exclusive.

Lots of professional AVL gear works with UDP because TCP/IP is slower and time is more important than ACK.

As HA is treading into show control waters it would be great if such implementations were to be made. I’m not saying I will be using HA professionally but I do want to use my scripts and ((semi )pro) devices at home :slight_smile:

Receiving syslog for state information is another use of receiving and processing udp.

The current syslog integration is for sending notifications.

I wish I could vote more than once for this!

Lack of a UDP integration seems like an oversight considering there’s an integration for TCP and one for serial communications.

Currently, you can use Node-red to receive UDP and then forward it to Home Assistant via an appropriate service call (or via MQTT). However, a native integration would require less infrastructure (especially when you consider how little code is needed to receive a lightweight protocol like UDP).


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:

  send_smth: "echo -n 'hello world' | nc -u -w1 54321"

This also works (shell_command > send_vol):

"echo -n 'volume 100' | nc -u -w1 65432"


'echo -n "volume 100" | nc -u -w1 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:


  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 54321

And these:


  send_vol: "{{ states('input_text.tekstveld') }}"

Where I have the complete string into the text field.

And all these iterations of send_vol:


'echo -n volume {{ states("input_number.vol_bak") }} | nc -u -w1 54321'

"echo -n 'volume {{ vol_data }}' | nc -u -w1 54321"

'echo -n ''volume {{ vol_data }}'' | nc -u -w1 54321'

'echo -n ''volume {{ (states('input_number.vol_bak').split('.')[0]) }}'' | nc -u -w1 54321'

"echo -n 'volume {{states(''input_number.vol_bak'')}}' | nc -u -w1 54321"

"echo -n 'volume {{ states(''input_text.tekstveld'') }}' | nc -u -w1 54321"


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:


    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:


- id: 'xxxxxxxxxxx'
  alias: Volume mediabak
  - entity_id: input_number.vol_bak
    platform: state
  - service: shell_command.volume_mediabak
      vol_data: "{{ '%s' % states('input_number.vol_bak') }}"
  - service: input_text.set_value
      entity_id: input_text.tekstveld
      value: "{{'%s' % (states('input_number.vol_bak').split('.')[0]) }}"


I have tried amongst others, these, in templates (dev tools), a few of which I knew wouldn’t work but tried just to falsify:


'echo -n "volume {{ states('input_number.vol_bak').split('.')[0] }}" | nc -u -w1 54321'
'echo -n "volume {{ states('input_number.vol_bak').split('.')[0] }}" | nc -u -w1 54321'
"echo -n "volume {{ states('input_number.vol_bak').split('.')[0] }}" | nc -u -w1 54321"
"echo -n ''volume {{'%s' % (states('input_number.vol_bak').split('.')[0]) }}'' | nc -u -w1 54321"
"echo -n ''volume {{ states('input_number.vol_bak').split('.')[0] | int }}'' | nc -u -w1 54321"
"echo -n ''volume {{ states('input_number.vol_bak').split('.')[0] }}'' | nc -u -w1 54321"
"echo -n ''volume {{ states('input_text.tekstveld') }}'' | nc -u -w1 54321"
{{ states("input_number.vol_bak") }}

{{ states("input_number.vol_bak").split('.')[0] }}

 "echo -n 'volume {{ states('vol_data') }}' | nc -u -w1 54321"
 "echo -n 'volume {{ vol_data }}' | nc -u -w1 54321"

The test template outcome was:


'echo -n "volume 80" | nc -u -w1 54321'
'echo -n "volume 80" | nc -u -w1 54321'
"echo -n "volume 80" | nc -u -w1 54321"
"echo -n ''volume 80'' | nc -u -w1 54321"
"echo -n ''volume 80'' | nc -u -w1 54321"
"echo -n ''volume 80'' | nc -u -w1 54321"
"echo -n ''volume 80'' | nc -u -w1 54321"


 "echo -n 'volume unknown' | nc -u -w1 54321"
 "echo -n 'volume ' | nc -u -w1 54321"

So testing and testing I found the root cause of these not working:


"echo -n 'volume {{ (states('input_number.vol_bak').split('.')[0]) }}'  | nc -u -w1 65432"
"echo -n ''volume {{ (states('input_number.vol_bak').split('.')[0]) }}''  | nc -u -w1 65432"
"echo -n \'volume {{ (states('input_number.vol_bak').split('.')[0]) }}\'  | nc -u -w1 65432"
"echo -n 'volume {{ (states(''input_number.vol_bak'').split(''.'')[0]) }}'  | nc -u -w1 65432"
"echo -n 'volume {{ (states('input_number.vol_bak').split('.')[0]) }}'  | nc -u -w1 65432"
"echo -n ""volume {{ (states('input_number.vol_bak').split('.')[0]) }}""  | nc -u -w1 65432"
"echo -n \"volume {{ (states('input_number.vol_bak').split('.')[0]) }}\"  | nc -u -w1 65432"

Is this error:


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 65432`, return code: 0:
b'volume 47 | nc -u -w1 65432'

Which couldn’t be helped by using triple quotes either, because:


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:


"echo -n {{'\"volume ' + (states('input_number.vol_bak').split('.')[0]) + '\"' }} | nc -u -w1 65432"

Which leads to


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 65432`, return code: 0:
b'volume 79 | nc -u -w1 65432'

So then a double check if the automation var will not work:


"echo -n {{'\"volume ' + vol_data + '\"' }} | nc -u -w1 65432"



2020-07-30 22:24:17 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `echo -n {{'"volume ' + vol_data + '"' }} | nc -u -w1 65432`, return code: 0:
b'volume 68 | nc -u -w1 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:


- id: '1234567891011'
  alias: Volume mediabak
  - entity_id: input_number.vol_bak
    platform: state
  - service: input_text.set_value
      entity_id: input_text.tekstveld
      value: 'echo -n volume {{ states("input_number.vol_bak").split(".")[0] }} | nc -u -w1 65432'
  - service: shell_command.volume_mediabak
      vol_data: "{{ states('input_text.tekstveld') }}"
  mode: single


  volume_mediabak: "echo -n {{vol_data}}"

You see what I did there? Double echo :slight_smile:


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 65432'

So try this again without the extra echo:

  volume_mediabak: "{{vol_data}}"


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.

Could you write this python and make it a custom component as a stepping stone to a core PR?
Even a python_script approach would be better than all the quoting and escaping above.

Install Node RED. That was my solution and I haven’t looked back since. :slight_smile:

Is this issue related to pipe and send netcat? I also have a TCP server, I am sending tcp commands too, with the value of an input slider ( brightness )

I was also struggling with the piping and jinja, but I solved it eventually

I’m not sure you know that udp doesn’t guarantee packet delivery nor their original order. I wouldn’t use it for mission critical home automation purposes.

We are talking about transport layer. MQTT is using tcp with ensures delivery or reports timeout instead (which is not case for udp)

i do it like this , my .py file does the actual netcat command

dimmer-: python3 /config/python_scripts/dimmer.py 00 {{ '%02.f'%((((states('input_number.dimmer1') | float / 255 ) * 90) // 10) * 10) }}

Would you mind to give more details how you proceeded ? as I’m killing myself trying to get it to work with shell_command :frowning:
Thanks :slight_smile:

What do you want to know? I posted an example above ? :slight_smile:

Well I’m confused by the code you posted ! It looks to be what is going in HA yaml but then what is the content of the py file to create to go with it ?


Ah, thats just a netcat command, I just pass 2 arguments in it, otherwise I need for every command a different she’ll, so easier to work with arguments

ah oki and yep I understand and agree with eveything in one file :wink: Do you mind to share the content of your py file so I’m sure I’m doing the whole thing properly ? :slight_smile:

Hmm, not sure of I still do it like that , was an old post :slight_smile:

But what do you want to send? Because you don’t need a py at all, just send directly a netcat , see some replys above for examples

ah damn I understand :frowning:

The netcat solution is not working at all, I’m killing myself since two weeks having tried bunch of “solutions” with Netcat but none of them works or even are accepted by HA :sob: Side note: I need to pass two arguments in string sent at remote device I want to control !

Well, show your config?

Sure no problem here it is :wink: One of my last versions but not working too as I HA tries to interprete symbols in the string instead of just sending it as it is :frowning:

- id: '1653115397185'
  alias: Volume VU+
  description: 'VU+ Volume'
  - platform: state
    - input_number.zektor_volume_vu
  condition: []
  - service: input_number.set_value
      entity_id: input_number.zektor_volume_vu
      value: 'echo -n ^VPZ @7, {{ states("input_number.zektor_volume_vu") | int}}$ | nc -u -w1 50005'
  mode: single

Also tried that but same, not working (although the whole command line is working perfectly in a terminal in linux but not in hassio :frowning: ) and generates an enigmatic code 1 error in logs but could not find any explanations about it :sob:


  zektor_vu_tr: '/bin/echo -e "^SZ @14, 5$" | nc -t -w 1 50005'
  zektor_sky1_tr: '/bin/echo -e "^SZ @14, 1$" | nc -t -w 1 50005'
  zektor_sky2_tr: '/bin/echo -e "^SZ @14, 2$" | nc -t -w 1 50005'
  zektor_sky3_tr: '/bin/echo -e "^SZ @14, 3$" | nc -t -w 1 50005'
  zektor_sky4_tr: '/bin/echo -e "^SZ @14, 4$" | nc -t -w 1 50005'
  zektor_atvmr_tr: '/bin/echo -e "^SZ @14, 6$" | nc -t -w 1 50005'
  zektor_atvmrs_tr: '/bin/echo -e "^SZ @14, 7$" | nc -t -w 1 50005'
  zektor_red3601_tr: '/bin/echo -e "^SZ @14, 8$" | nc -t -w 1 50005'
  zektor_red3602_tr: '/bin/echo -e "^[email protected] 14, 9$" | nc -t -w 1 50005'
  zektor_mag_tr: '/bin/echo -e "^SZ @14, 10$" | nc -t -w 1 50005'
  zektor_bluesound_tr: '/bin/echo -e "^SZ @14, 11$" | nc -t -w 1 50005'

Error in logs:

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:115
Integration: Shell Command ([documentation](https://www.home-assistant.io/integrations/shell_command), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+shell_command%22))
First occurred: 14:56:26 (6 occurrences)
Last logged: 14:56:48

* Error running command: `/bin/echo -e "^SZ @14, 6$" | nc -t -w 1 50005`, return code: 1
* Error running command: `/bin/echo -e "^SZ @14, 7$" | nc -t -w 1 50005`, return code: 1
* Error running command: `/bin/echo -e "^SZ @14, 11$" | nc -t -w 1 50005`, return code: 1
* Error running command: `/bin/echo -e "^SZ @14, 10$" | nc -t -w 1 50005`, return code: 1
* Error running command: `/bin/echo -e "^SZ @14, 8$" | nc -t -w 1 50005`, return code: 1