TCP commands for Ethernet relay

ok, here is my config, its a lot, probably you need to read it 10 x times :slight_smile:

so the because you can controll lights/switch outside HA? i wanted to know also the state of a light/switch/cover in my HA offcours,
thats why i poll every 10 secs my dobiss, so i can change the value_template

#sensor
  - platform: command_line
    command: shell_command.dobissreceive-
    name: prog
    scan_interval: 15

=>
dobissreceive-: python3 /config/python_scripts/dobissreceive.py

as you can see, i send to messages, the string is limited, so at the end i have 1 sensor, that contains everything in my house
the second string the ffffff… , is not used, but you need to send it, it needs complete bits, also the begin part is also the same : ED63300000000000000000000000AFAF
then it begins like 4205 , 420a , … so 42 is relays B in my case, 05 is the 6th output, as you can see work in hex here!!
so i will receive 00 or 01 if something is on / off
so now you already know states of lights/dimmers/covers/ …

in the .py file:

#!/usr/bin/python3
import socket
import codecs
import binascii

host = '192.168.0.10'
port = 1001
message = binascii.a2b_hex ("ED63300000000000000000000000AFAF4205420a410041014102410341044105410B42024304430A430B4408440943074306440A4500450145024503410A4106")

mySocket = socket.socket()
mySocket.connect((host,port))
mySocket.send(message)

data = mySocket.recv(1024)
data = binascii.hexlify(data).decode()

message2 = binascii.a2b_hex ("ED63300000000000000000000000AFAF42084207420943004206420442034400440144024403440444054406440743084309430243034305FFFFFFFFFFFFFFFF")
mySocket.send(message2)

data2 = mySocket.recv(1024)
data2 = binascii.hexlify(data2).decode()

print (data + '--' + data2)


mySocket.close()

second part, easy part, control a light/switch :


- platform: template
  lights:
         
    keukeneiland:
      friendly_name: "Keuken Eiland"
      value_template: "{{ states.sensor.prog.state[12:14] == '01' }}"
      turn_on:
        - service: shell_command.dobiss
          data_template:
            module: 41
            id: 04
            power: 01
        - service: shell_command.dobiss_state
          data_template:
            power: "on"
            name: "Keuken Eiland"
            icon: "mdi:lightbulb-on"
            api: "light.keukeneiland"
      turn_off:
        - service: shell_command.dobiss
          data_template:
            module: 41
            id: 04
            power: 00
        - service: shell_command.dobiss_state
          data_template:
            power: "off"
            name: "Keuken Eiland"
            icon: "mdi:lightbulb-on"
            api: "light.keukeneiland"

you see, when i turn on a light, i send 2 services
1 service is a shell_command, this is new, i made variables, here, so every light/switch code looks the same, only the module/id/power values change here…
so the shell_command.dobiss looks like :

=>
dobiss: bash /config/python_scripts/dobiss.sh {{module}} {{id}} {{power}}

my bash script looks like, notice the $1 , $2 , $3 , those are my variables

#!/bin/bash
#dobiss 43 01 01  => dobiss: bash /config/dobiss.sh {{module}} {{id}} {{power}} 
echo -e "\xED\x43\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAF\xAF\x"$1"\x"$2"\x"$3"" | nc 192.168.0.10 1001

so this should now turn on a light :slight_smile:
for my second service, you see i do a curl, this is optional, if you dont do it, your light icon will only be changed aftre 10 seconds, base on the state sensorso i do a curl, so i change the icon instantly
also here i work now with variables, before i had for every light a shellcommand
so dobiss_state looks like :

dobiss_state: "curl -k -X POST -H \"Authorization: Bearer YOURBEARERHERE\" -d '{\"state\": \"{{power}}\",\"attributes\": {\"assumed_state\": true,\"friendly_name\": \"{{name}}\",\"icon\": \"{{icon}}\"}}' https://127.0.0.1:8123/api/states/{{api}}"

now, you also see a value template, this is is needed if you control a light outside HA, so when 10 sec scan_interval passes, this will change the light icon also
so if => [12:14] == ‘01’ => means character 12 to 14 , are 2 decimals, in that string you see in the sensor, if its 00 , light s off, if its 01 , light is on

second part, dimmers, not gonna comment here, just look at my code :slight_smile:


    dimmer1:
      friendly_name: "Eethoek Raam"
      value_template: "{{ states.sensor.prog.state[36:38] != '00' }}" 
      level_template: "{{ ( states('sensor.Prog')[36:38] | int(base=16) / 90 * 255 ) | int }}"

      turn_on:
        - service: input_number.set_value
          data:
            entity_id: input_number.dimmer1
            value: 255
        - service: shell_command.dimmer1
        - service: shell_command.state_dimmer1_on
      turn_off:
        - service: input_number.set_value
          data:
            entity_id: input_number.dimmer1
            value: 0
        - service: shell_command.dimmer1
        - service: shell_command.state_dimmer1_off
      set_level:
        - service: input_number.set_value
          data_template:
            entity_id: input_number.dimmer1
            value: "{{ brightness }}"
        - service: shell_command.dimmer1
        - service: shell_command.state_dimmer1_on
#shell commands:		
dimmer1: python3 /config/python_scripts/dimmer.py {{ '%02.f'%((((states('input_number.dimmer1') | float / 255 ) * 90) // 10) * 10) }}		
state_dimmer1_on: "curl  -k -X POST -H \"Authorization: Bearer BEARERHERE\" -d '{\"state\": \"on\",\"attributes\": {\"brightness\": {{states('input_number.dimmer1')}}, \"assumed_state\": true,\"friendly_name\": \"Eethoek Raam\",\"icon\": \"mdi:spotlight\", \"supported_features\": 1}}' https://127.0.0.1:8123/api/states/light.dimmer1"
state_dimmer1_off: "curl  -k -X POST -H \"Authorization: Bearer BEARERHERE\" -d '{\"state\": \"off\",\"attributes\": {\"assumed_state\": true,\"friendly_name\": \"Eethoek Raam\",\"icon\": \"mdi:spotlight\", \"supported_features\": 1}}' https://127.0.0.1:8123/api/states/light.dimmer1"
#python file dimmer.py
#!/usr/bin/python3
import socket
import codecs
import binascii
import sys
## python3 dimmer.py 00-03 00-90
host = '192.168.0.10'
port = 1001
message = "ED43310000000000000000000000AFAF4510" + sys.argv[2]
string = binascii.a2b_hex (message)
mySocket = socket.socket()
mySocket.connect((host,port))
mySocket.send(string)
mySocket.close()
#input number
dimmer1:
    name: Eethoek Raam
    initial: 0
    min: 0
    max: 255
    step: 1

now, last part , screens/covers

- platform: template
  covers:
    google_eetkamer:
      friendly_name: Eetkamer
      value_template: "{{ is_state('input_boolean.eetkamer', 'on' ) }}"
      open_cover:
        - service: cover.open_cover
          entity_id: cover.eetkamer
      close_cover:
        - service: cover.close_cover
          entity_id: cover.eetkamer
      stop_cover:
        - service: cover.stop_cover
          entity_id: cover.eetkamer
		  
- platform: command_line
  covers:
    cover1:
      friendly_name: Eetkamer
      command_open: shell_command.screen1_open
      command_close: shell_command.screen1_close
      command_stop: shell_command.screens_stop
	  
#shell command
screen1_open: echo -e "\xED\x43\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAF\xAF\x53\x13\x01" | nc 192.168.0.10 1001
screen1_close: echo -e "\xED\x43\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAF\xAF\x53\x14\x01" | nc 192.168.0.10 1001
screen2_open: echo -e "\xED\x43\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAF\xAF\x53\x15\x01" | nc 192.168.0.10 1001

#input boolean, to remeber the state after a HA restart !!


input_boolean:
  eetkamer:
    name: Eetkamer
1 Like