Serial Control

I have been attgempting some serial control since a while and this has turned out to be the most exasperating thing on HA by far. Everything else I have mamaged with some hit and miss but the serial control has got me totally foxed.

Problem: I want to control an old lighting system by sending it serial commands. I dont care if i dont get feedback but I would at least like to operate it unidirectionally by sending it serial commands.

Serial gateway: I cannot connect my HA server physically to the serial device. I have to use an IP gateway. I inquired about options but couldnt get many answers on that thread, hence reclined to using a Global Cache IP2SL, albeit grudgingly owing to its high cost.

What I have done so far: I used the Global Cache iTest app. It lets you select the port and enter the command and test the device you want to operate. So by choosing Serial 1 option with port 4999 in iTest and setting the send type to “Mixed” I was able to send Hex strings such as \x00\x0F\x02\x02\x00\xED and successfully operate the device.

Now to figure out some way to send the same command via HA: I went through various threads and some said they accomplished it using a shell command, yet others using telnet etc. But for the life of me, I havent got the device to respond at all.

My configuration.yaml is as below. My IP2SL is at 192.168.0.210 and I am using port 4999 since it worked from itest and also using the same command strings that work in it. Can someone PLEASE tell me what I’m doing wrong? Obviously ignore the device names, they are copied from the examples I used.

shell_command:

    sc_one: /bin/bash -c "echo -e '\x00\x0E\x02\x00\x00\xF0' | nc 192.168.0.210 4999 -i 1

switch:

  - platform: command_line

    scan_interval: 15

    switches:

      kitchen_amp_dsp2_150:

        command_on: 'echo -n -e "\x00\x0E\x02\x00\x00\xF0" | nc 192.168.0.210 4999 && sleep 9s'

        command_off: 'echo -n -e "\xFF\x55\x01\x02" | nc 192.168.1.19 52000  && sleep 2s'

        command_state: 'echo -n -e "\xFF\x55\x01\x70" | nc 192.168.1.19 52000 | grep -c "Power status :On"'

        value_template: '{{ value == "1" }}'

        command_timeout : 10.0

  - platform: telnet

    switches:

      projector:

        resource: 192.168.0.210

        port: 4999

        command_on: "\x00\x0F\x02\x01\x00\xEE"

        command_off: "\x00\x0E\x02\x00\x00\xF0"

        name: "Proj On"

        timeout: 3

Some of the threads and posts I referred are

EDIT: I can see this in the logs


2022-10-21 18:34:20.858 WARNING (MainThread) [homeassistant.helpers.entity] Update of switch.kitchen_amp_dsp2_150 is taking over 10 seconds

2022-10-21 18:34:20.870 ERROR (SyncWorker_10) [homeassistant.components.command_line] Timeout for command: echo -n -e "\xFF\x55\x01\x70" | nc 192.168.1.19 52000 | grep -c "Power status :On"

2022-10-21 18:34:35.860 WARNING (MainThread) [homeassistant.helpers.entity] Update of switch.kitchen_amp_dsp2_150 is taking over 10 seconds

2022-10-21 18:34:35.868 ERROR (SyncWorker_9) [homeassistant.components.command_line] Timeout for command: echo -n -e "\xFF\x55\x01\x70" | nc 192.168.1.19 52000 | grep -c "Power status :On"

2022-10-21 18:34:50.860 WARNING (MainThread) [homeassistant.helpers.entity] Update of switch.kitchen_amp_dsp2_150 is taking over 10 seconds

2022-10-21 18:34:50.865 ERROR (SyncWorker_7) [homeassistant.components.command_line] Timeout for command: echo -n -e "\xFF\x55\x01\x70" | nc 192.168.1.19 52000 | grep -c "Power status :On"

2022-10-21 18:35:05.862 WARNING (MainThread) [homeassistant.helpers.entity] Update of switch.kitchen_amp_dsp2_150 is taking over 10 seconds

2022-10-21 18:35:05.870 ERROR (SyncWorker_2) [homeassistant.components.command_line] Timeout for command: echo -n -e "\xFF\x55\x01\x70" | nc 192.168.1.19 52000 | grep -c "Power status :On"

2022-10-21 18:35:15.916 ERROR (MainThread) [homeassistant.components.shell_command] Timed out running command: `echo -ne "\x00\x0E\x02\x00\x00\xF0" | nc 192.168.0.210 4999 -i 5`, after: 60s

I want to emphasize that I am no expert. I did get serial commands working using the IP2SL for my use case. You mentioned my post. Looking at what you posted and how it differs from what I did I see a few things that you could try.

My working yaml for creating a telnet switch for each command needed:

switch:
  - platform: telnet
    switches:
        wha_living_room_on:
          resource: 192.168.1.114
          port: 4999
          command_on: "':Z11\x0D'"
          command_off: "':Z11\x0D'"
          timeout: 3

        wha_living_room_off:
          resource: 192.168.1.114
          port: 4999
          command_on: "':Z10\x0D'"
          command_off: "':Z10\x0D'"
          timeout: 3

Notice that each switch is a single command and both the “on” and “off” commands are the same. Also notice the double outside quotes, single inside quotes surrounding the command with the leading colon.

With itest I was able to get these to work, the carriage return in my commands x0D was needed at the end of the commands from my amp’s manual that only included the :Z10 where the amp sees that as zone 1 off, or :Z11 for zone 1 on. But the switch in the yaml has to be the same for both on and off, you just need a second switch for the off command.

Then in my dashboard I use a button card to call the command:

cards:
      - type: custom:button-card
        color_type: icon
        styles:
          icon:
            - color: green
        entity: switch.wha_living_room_on
        show_name: true
        icon: mdi:power
        name: 'ON'

image

I wish I could just tell you exactly how to get it working. I hop the above helps you though. Good luck.

1 Like

I’m still unable to get it to work.

I have tried various combinations, like removing the colon, removing the single inverted commas, addding a \x0D at the end etc but I cant get anything from the IP2SL

Something strange that I also observe is that the IP2SL refuses connection from the iTest after I am done testing with HA. So when I select the Serial 1 option (4999) from iTest it says connecting but returns back to disconnected state. I have to reboot the IP2SL to get iTest to connect back to it.

I wish there was an easier solution to what seems like a very trivial task of getting a serial command across to a device :frowning:

If I were to adopt a wired approach, any suggestions of what I could run from my HA server to the RS485 device?

below is the config, obviously i’m trying out the various syntax options hence on and off commands differ

switch:
  - platform: telnet
    switches:
        wha_living_room_on:
          resource: 192.168.0.210
          port: 4999
          command_on: "\x00\x0E\x02\x00\x00\xF0"
          command_off: "\x00\x0E\x02\x00\x00\xF0"
          timeout: 3

        wha_living_room_off:
          resource: 192.168.0.210
          port: 4999
          command_on: "'\x00\x0E\x02\x00\x00\xF0'"
          command_off: "'\x00\x0E\x02\x00\x00\xF0'"
          timeout: 3

I wrote to global cache support and they are of the opinion that while telnet seems to be the more straightforwarrd approach, maybe it doesnt work in home assistant.

Hence they are recommending a command line workaround. Similar to the last post in the thread below.

In my case by using

command_on: 'echo -n -e "\x00\x0E\x02\x00\x00\xF0" | nc 192.168.0.210 4999 

Can anyone confirm that this would be the correct way? Any other pointers/ syntax suggestions? Do I maintain the same “scan interval”, “sleep”, “command timeout” etc? I already tried it as is in my previous attempt but it didnt work.

Any options other than the global cache device that I could try out for simple generic serial comm

I struggled with this for hours!
Does not work:
echo -ne "\x08\x22\x00\x00\x00\x02\xD4" | nc 10.11.12.120 4999

Does Work:
echo -ne "\x08\x22\x00\x00\x00\x02\xD4" | nc 10.11.12.120 4999 -i 1

Is it better to use the switch: - platform: telnet, is the time out the “-i 1”?

I am entering it as this:

command_line_yaml

  • switch:
    command_on: echo -ne “\x08\x22\x00\x00\x00\x02\xD4” | nc 10.11.12.120 4999 -i 1
    command_off: “”
    name: “Family TV Ex-Link On”
    unique_id: tv_family_ex_link_on
  • switch:
    command_on: echo -ne “\x08\x22\x00\x00\x00\x01\xD5” | nc 10.11.12.120 4999 -i 1
    command_off: “”
    name: “Family TV Ex-Link Off”
    unique_id: tv_family_ex_link_off

It would be better to just have a send option or press

Ideally would be create a single device “Family TV” and have the commands be:

On:
echo -ne “\x08\x22\x00\x00\x00\x02\xD4” | nc 10.11.12.120 4999 -i 1

Off:
echo -ne “\x08\x22\x00\x00\x00\x01\xD5” | nc 10.11.12.120 4999 -i 1