How to make virtual switch with uart controlled?

Hi, I have board with uart controlled. How in ESPHome I can make virtual switch with this? Can’t understand.

uart:
  tx_pin: GPIO1
  rx_pin: GPIO3
  baud_rate: 115200
  
switch:

  - platform: uart
    id: turn_on
    name: "Workspace light on"
    data: [0xA0, 0x02, 0x01, 0xA3]
  - platform: uart
    id: turn_off
    name: "Workspace light off"
    data: [0xA0, 0x02, 0x00, 0xA2]

logger:
  hardware_uart: UART1

light:
  - platform: binary
    name: Kitchen workspace
    output: 

Create a template switch and use the turn_on and turn_off action to write to the uart

https://esphome.io/components/switch/template.html

https://esphome.io/components/uart.html#uart-write-action

This will have to be an optimistic switch unless you have some way to get the switch state.

switch:
  - platform: template
    name: "Workspace light"
    optimistic: true
    turn_on_action:
      - uart.write: [0xA0, 0x02, 0x01, 0xA3]
    turn_off_action:
      - uart.write: [0xA0, 0x02, 0x00, 0xA2]
2 Likes

Thank you very much!

1 Like

I’m trying to control an Arduino nano in order to drive some gpio through serial commands but it doesn’t work … probably my Arduino code is not correct … is it possible to have an example of Arduino sketch to be controlled by ESPHOME?
@tom_l

Works perfekt with LC Tech ESP8266 RELAY