esphome:
  name: thesharp
  platform: ESP8266
  board: esp12e
 
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
 
  ap:
    ssid: "SOS_theSharp"
    password: !secret wifi_password
 
captive_portal:
 
# Enable logging
logger:
  baud_rate: 0 #9600
  #level: DEBUG #기본값
  level: VERBOSE #사용하지 않는 패킷만 출력
  #level: VERY_VERBOSE #전체 패킷 출력 (초기 설정시에 추천)
 
# Enable Home Assistant API
api:
  password: !secret ota_password

ota:
  password: !secret ota_password
 
status_led:
  pin:
    number: GPIO2
    inverted: True

# RS485 Component (for ttl to rs485 module)
#  - esp8266: UART0 (TX: GPIO1, RX: GPIO3)
#  - esp32: UART2 (TX: GPIO17, RX: GPIO16)
rs485:
  baud_rate: 9600 #Required
  data_bits: 8    #Option(default: 8)
  parity: 0       #Option(default: 0)
  stop_bits: 1    #Option(default: 1)
 
  rx_wait: 10     #Option(default: 10ms)  -> 수신 메시지 대기시간 (10ms 미만으로 수신된 메시지만 한 패킷으로 판단)
  tx_wait: 100    #Option(default: 100ms) -> 발신 메시지 Ack 대기시간
  tx_retry_cnt: 3 #Option(default: 3)     -> 발신 메시지 Ack 없을 경우 재시도 횟수
  prefix: [0xF7]  #Option -> 값 세팅시 모든 수신 패킷 Check, 발신 패킷에 Append
  suffix: [0xEE]  #Option -> 값 세팅시 모든 수신 패킷 Check, 발신 패킷에 Append

  checksum: True      #Option(default: False) -> 체크섬 사용여부 (lambda 사용시 세팅 불필요)
  # checksum_lambda: |- #Option -> Default(CheckSum8 Xor) 체크섬 아닐 경우 직접 로직 구현(아래 값은 Default 로직임)
  #   // @param: const uint8_t *data, const unsigned short len
  #   // @return: uint8_t
  #   uint8_t crc = 0xF7; // data 변수에는 prefix 제외되어 있음
  #   for(num_t i=0; i<len; i++)
  #     crc ^= data[i];
  #   return crc;
 
  state_response: #Option -> 값 세팅시 response 패킷 수신 후에 명령 패킷 송신
    data: [0x04]
    offset: 3
 
  packet_monitor: #Option -> 패킷 모니터: Array 없으면 전체 출력, 있을 경우 or 조건 (logger level DEBUG 추천)
    - [0x0B, 0x01, 0x18]
    #- [0x10, 0x01, 0x2a]  # offset: 0
    # - data: [0x19, 0x04, 0x40, 0x23]
      # offset: 2

# RS485 Device 셋팅 팁
# 1. 상태 확인용(수신) Schema
#   - 대상: device(Required), sub_device(Option), state*
#   - 조건: device and sub_device 일치할때만 해당 Device에 패킷 전달됨
#   - 예시:
#     device:
#       offset: 0 # 수신 패킷 비교 위치 (prefix 제외)
#       data: [0x0b, 0x01, 0x19, 0x04, 0x40, 0x23, 0x00] # hex array
#     #alias schema
#     device: [0x0b, 0x01, 0x19, 0x04, 0x40, 0x23, 0x00] # offset: 0, data: hex array
#
# 2. 명령용(발신) Schema
#   - 대상: command*
#   - 예시:
#     command:
#       data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x21, 0x01, 0x00]  # prefix, checksum, suffix 제외
#       ack: [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x21, 0x01, 0x01]   # prefix, checksum, suffix 제외
#     # Ack 없을 경우
#     command: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x21, 0x01, 0x00]
#     # Template(lambda): command_on, command_off
#     command_on: !lambda |-
#       uint8_t flag = id(balcony).state ? 0x02 : 0x01;
#       return {
#                 {0x0b, 0x01, 0x19, 0x02, 0x40, 0x23, flag, 0x00},
#                 {0x0b, 0x01, 0x19, 0x04, 0x40, 0x23, 0x01, 0x01}
#              };
#     command_off: !lambda |-
#       uint8_t flag = id(balcony).state ? 0x02 : 0x01;
#       return {
#                 {0x0b, 0x01, 0x19, 0x02, 0x40, 0x23, flag, 0x00},
#                 {0x0b, 0x01, 0x19, 0x04, 0x40, 0x23, 0x02, 0x02}
#              };
#     # 상태확인 요청 (Sensor 포함 모든 Device에 사용 가능)
#     command_state: [0x0B, 0x01, 0x19, 0x01, 0x40, 0x23, 0x00, 0x00] #Option
#     update_interval: 30s #Option(default: 60s)
#
# 3. Automation Schema
#   - rs485.write:
#       data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x23, 0x01, 0x00]
#       ack: [0x0b, 0x01, 0x19, 0x04, 0x40, 0x23, 0x01, 0x01]
#   - rs485.write: !lambda return {{0x0b, 0x01, 0x19, 0x02, 0x40, 0x23, 0x01, 0x00}, {0x0b, 0x01, 0x19, 0x04, 0x40, 0x23, 0x01, 0x01}};
#   # Ack 없는 경우
#   - rs485.write: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x23, 0x01, 0x00]
#   - rs485.write: !lambda return {{0x0b, 0x01, 0x19, 0x02, 0x40, 0x23, 0x01, 0x00}, {}};

# RS485 Light(like Binary Light) ###################################
#현대통신
#거실- 3구
#안방- 2구
#방2- 1구
#방3- 2구
#주방- 2구
###########################################################
light:
# [거실1]
#거실상태조회  
#>f70b 0119 0140 1000 00 b5 ee
#<F70D 0119 0440 1000 02 02 02 B4EE <<거실1off 2off 3off 응답    01on 02off
#<F70D 0119 0440 1000 01 01 01 B7EE <<거실1on  2on  3on 응답
#<F70D 0119 0440 1000 01 02 02 B7EE <<거실1on  2off  3off 응답
#<F70D 0119 0440 1000 01 01 02 B4EE <<거실1on  2on  3off 응답
#<F70D 0119 0440 1000 02 01 02 B7EE <<거실1off  2on  3off
#<F70D 0119 0440 1000 02 01 01 B4EE <<거실1off  2on  3on
#<F70D 0119 0440 1000 02 02 01 B7EE <<거실1off  2off  3on

#거실1-1off
#>f70b 0119 0240 1102 00b5 ee
#<f70b 0119 0440 1102 02b1
#거실1-1on
#>f70b 0119 0240 1101 00b6 ee
#<f70b 0119 0440 1101 01b1

  - platform: rs485
    name: "Livingroom1"
    device: [0x0d, 0x01, 0x19, 0x04, 0x40, 0x10, 0x00]
    state_on:
      offset: 7
      data: [0x01]
    state_off:
      offset: 7
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x11, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x11, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x11, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x11, 0x02, 0x02]
    command_state: [0x0b, 0x01, 0x19, 0x01, 0x40, 0x10, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기

# [거실2]
#거실1-2off
#>f70b 0119 0240 1202 00b6 ee
#<f70b 0119 0440 1202 02b2
#거실1-2on
#>f70b 0119 0240 1201 00b5 ee
#<f70b 0119 0440 1201 01b2

  - platform: rs485
    name: "Livingroom2"
    device: [0x0d, 0x01, 0x19, 0x04, 0x40, 0x10, 0x00]
    state_on:
      offset: 8
      data: [0x01]
    state_off:
      offset: 8
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x12, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x12, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x12, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x12, 0x02, 0x02]

#거실1-3off
#>f70b 0119 0240 1302 00b7 ee
#<f70b 0119 0440 1302 02b3
#거실1-3on
#>f70b 0119 0240 1301 00b4 ee
#<f70b 0119 0440 1301 01b3
  - platform: rs485
    name: "Livingroom3"
    device: [0x0d, 0x01, 0x19, 0x04, 0x40, 0x10, 0x00]
    state_on:
      offset: 9
      data: [0x01]
    state_off:
      offset: 9
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x13, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x13, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x13, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x13, 0x02, 0x02]
 
#[안방1]  #kcw ok
#방1(안방)상태조회  
#>f70b 0119 0140 2000 0085 ee
#<F70c 0119 0440 2000 0202 87EE   <<<응답
#<F70c 0119 0440 2000 0101 87EE

#방1-1off
#>f70b 0119 0240 2102 0085 ee
#<f70b 0119 0440 2102 0281
#방1-1on
#>f70b 0119 0240 2101 0086 ee
#<f70b 0119 0440 2101 0181
  - platform: rs485
    name: "ROOM1 1"
    device: [0x0c, 0x01, 0x19, 0x04, 0x40, 0x20, 0x00]
    state_on:
      offset: 7
      data: [0x01]
    state_off:
      offset: 7
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x21, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x21, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x21, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x21, 0x02, 0x02]
    command_state: [0x0b, 0x01, 0x19, 0x01, 0x40, 0x20, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기
   
#[안방2]  #kcw ok
#방1-2off
#>f70b 0119 0240 2202 0086 ee
#<f70b 0119 0440 2202 0282
#방1-2on
#>f70b 0119 0240 2201 0085 ee
#<f70b 0119 0440 2201 0182
  - platform: rs485
    name: "ROOM1 2"
    device: [0x0c, 0x01, 0x19, 0x04, 0x40, 0x20, 0x00]
    state_on:
      offset: 8
      data: [0x01]
    state_off:
      offset: 8
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x22, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x22, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x22, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x22, 0x02, 0x02]

#[방2]  #kcw ok
#방2상태조회
#>F70B 0119 0140 3000 0095 EE
#<F70B 0119 0440 3000 0292 EE
#<F70B 0119 0440 3000 0191 EE
  - platform: rs485
    name: "ROOM2"
    device: [0x0b, 0x01, 0x19, 0x04, 0x40]
    sub_device:
      offset: 5
      data: [0x30]
    state_on:
      offset: 7
      data: [0x01]
    state_off:
      offset: 7
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x31, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x31, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x31, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x31, 0x02, 0x02]
    command_state: [0x0B, 0x01, 0x19, 0x01, 0x40, 0x30, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기
 
#[방3]  #kcw ok
#방3 상태조회
#>F70B 0119 0140 4000 00E5 EE
#<F70B 0119 0440 4000 02E2 EE
#<F70B 0119 0440 4000 01E1 EE
  - platform: rs485
    name: "ROOM3"
    device: [0x0b, 0x01, 0x19, 0x04, 0x40]
    sub_device:
      offset: 5
      data: [0x40]
    state_on:
      offset: 7
      data: [0x01]
    state_off:
      offset: 7
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x41, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x41, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x41, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x41, 0x02, 0x02]
    command_state: [0x0B, 0x01, 0x19, 0x01, 0x40, 0x40, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기

# [주방1]  #kcw ok
#>f70b 0119 0140 5000 00f5
#<F70c 0119 0440 5000 0202F7EE
#<F70c 0119 0440 5000 0101F7EE

#주방1-off
#>f70b 0119 0240 5102 00f5 ee
#<f70b 0119 0440 5102 02f1
#주방1-on
#>f70b 0119 0240 5101 00f6 ee
#<f70b 0119 0440 5101 01f1
  - platform: rs485
    name: "Kitchen1"
    device: [0x0c, 0x01, 0x19, 0x04, 0x40]
    sub_device:
      offset: 5
      data: [0x50]
    state_on:
      offset: 7
      data: [0x01]
    state_off:
      offset: 7
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x51, 0x01, 0x00]
      ack: [0x0b, 0x01, 0x19, 0x04, 0x40, 0x51, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x51, 0x02, 0x00]
      ack: [0x0b, 0x01, 0x19, 0x04, 0x40, 0x51, 0x02, 0x02]
    command_state: [0x0B, 0x01, 0x19, 0x01, 0x40, 0x50, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기

# [주방2]  #kcw ok
#주방2-off
#>f70b 0119 0240 5202 00f6 ee
#<f70b 0119 0440 5202 02f2
#주방2-on
#>f70b 0119 0240 5201 00f5 ee
#<f70b 0119 0440 5201 01f2
  - platform: rs485
    name: "Kitchen2"
    device: [0x0c, 0x01, 0x19, 0x04, 0x40]
    sub_device:
      offset: 5
      data: [0x50]
    state_on:
      offset: 8
      data: [0x01]
    state_off:
      offset: 8
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x52, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x52, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x19, 0x02, 0x40, 0x52, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x19, 0x04, 0x40, 0x52, 0x02, 0x02]
# RS485 Light(like Binary Light) ###################################


###################################
# RS485 Fan
fan:
#F70B 012B 0140 1100 0086 EE  >>성태조회 MainTouch
#F70C 012B 0440 1100 0207 81EE  <<응답상태끔
#F70C 012B 0440 1100 0101 84EE  <<응답상태약
#F70C 012B 0440 1100 0103 86EE  <<응답상태중
#F70C 012B 0440 1100 0107 82EE  <<응답상태강

#환기 off cmd
#>f70b 012b 0240 1102 0087ee
#<f70c 012b 0441 2b04 4011 00020781ee
#환기 on cmd
#>f70b 012b 0242 1101 0086ee
#<f70c 012b 0441 2b04 4011 00010184ee

#환기 on 3단 cmd
#>f70b 012b 0242 1107 0080ee
#<f70c 012b 0441 2b04 4011 0001 0782ee
#환기 on 2단 cmd
#>f70b 012b 0242 1103 0084ee
#<f70c 012b 0441 2b04 4011 0001 0386ee
#환기 on 1단 cmd
#>f70b 012b 0242 1101 0086ee
#<f70c 012b 0441 2b04 4011 0001 0184ee
  - platform: rs485
    name: "Ventilation"
    device: [0x0c, 0x01, 0x2b, 0x04, 0x40, 0x11, 0x00]
    state_on:
      offset: 7
      data: [0x01]
    state_off:
      offset: 7
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x2b, 0x02, 0x42, 0x11, 0x01, 0x00]
      ack:  [0x0c, 0x01, 0x2b, 0x04, 0x41, 0x2b, 0x04, 0x40, 0x11, 0x00, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x2b, 0x02, 0x40, 0x11, 0x02, 0x00]
      ack: [0x0c, 0x01, 0x2b, 0x04, 0x40, 0x2b, 0x04, 0x40, 0x11, 0x00, 0x02]
    speed: #Option(high, medium, low) -> 없으면 Binary Fan
      high:
        state:
          offset: 7
          data: [0x01, 0x07]
        command:
          data: [0x0b, 0x01, 0x2b, 0x02, 0x42, 0x11, 0x07, 0x00]
          ack:  [0x0c, 0x01, 0x2b, 0x04, 0x41, 0x2b, 0x04, 0x11, 0x00, 0x01, 0x07]
      medium:
        state:
          offset: 7
          data: [0x01, 0x03]
        command:
          data: [0x0b, 0x01, 0x2b, 0x02, 0x42, 0x11, 0x03, 0x00]
          ack: [0x0c, 0x01, 0x2b, 0x04, 0x41, 0x2b, 0x04, 0x11, 0x00, 0x01, 0x03]
      low:
        state:
          offset: 7
          data: [0x01, 0x01]
        command:
          data: [0x0b, 0x01, 0x2b, 0x02, 0x42, 0x11, 0x01, 0x00]
          ack: [0x0c, 0x01, 0x2b, 0x04, 0x41, 0x2b, 0x04, 0x11, 0x00, 0x01, 0x01]
    command_state: [0x0B, 0x01, 0x2B, 0x01, 0x40, 0x11, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기         
###################################
switch:
# 거실1 콘센트
  - platform: rs485
    name: "Livingroom Power Socket 1"
    icon: "mdi:power-socket-eu"
    device: [0x1C, 0x01, 0x1F, 0x04, 0x40, 0x10, 0x00]
    state_on:
      offset: 8
      data: [0x01]
    state_off:
      offset: 8
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x11, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x11, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x11, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x11, 0x02, 0x02]
    command_state: [0x0B, 0x01, 0x1F, 0x01, 0x40, 0x10, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기 
 
  # 거실2 콘센트
  - platform: rs485
    name: "Livingroom Power Socket 2"
    icon: "mdi:power-socket-eu"
    device: [0x1C, 0x01, 0x1F, 0x04, 0x40, 0x10, 0x00]
    state_on:
      offset: 17
      data: [0x01]
    state_off:
      offset: 17
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x12, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x12, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x12, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x12, 0x02, 0x02]

# 안방1 콘센트    
  - platform: rs485
    name: "ROOM1 Power Socket 1"
    icon: "mdi:power-socket-eu"
    device: [0x25, 0x01, 0x1F, 0x04, 0x40, 0x20, 0x00]
    state_on:
      offset: 8
      data: [0x01]
    state_off:
      offset: 8
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x21, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x21, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x21, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x21, 0x02, 0x02]
    command_state: [0x0B, 0x01, 0x1F, 0x01, 0x40, 0x20, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기 
 
 
  # 안방2 콘센트
  - platform: rs485
    name: "ROOM1 Power Socket 2"
    icon: "mdi:power-socket-eu"
    device: [0x25, 0x01, 0x1F, 0x04, 0x40, 0x20, 0x00]
    state_on:
      offset: 17
      data: [0x01]
    state_off:
      offset: 17
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x22, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x22, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x22, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x22, 0x02, 0x02]
 
   # 안방3 콘센트
  - platform: rs485
    name: "ROOM1 Power Socket 3"
    icon: "mdi:power-socket-eu"
    device: [0x25, 0x01, 0x1F, 0x04, 0x40, 0x20, 0x00]
    state_on:
      offset: 26
      data: [0x01]
    state_off:
      offset: 26
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x23, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x23, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x23, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x23, 0x02, 0x02]
 
 
  # 방2 콘센트    
  - platform: rs485
    name: "ROOM2 Power Socket 1"
    icon: "mdi:power-socket-eu"
    device: [0x1C, 0x01, 0x1F, 0x04, 0x40, 0x30, 0x00]
    state_on:
      offset: 8
      data: [0x01]
    state_off:
      offset: 8
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x31, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x31, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x31, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x31, 0x02, 0x02]
    command_state: [0x0B, 0x01, 0x1F, 0x01, 0x40, 0x30, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기 
    
  - platform: rs485
    name: "ROOM2 Power Socket 2"
    icon: "mdi:power-socket-eu"
    device: [0x1C, 0x01, 0x1F, 0x04, 0x40, 0x30, 0x00]
    state_on:
      offset: 17
      data: [0x01]
    state_off:
      offset: 17
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x32, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x32, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x32, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x32, 0x02, 0x02]
 
   # 방3 콘센트    
  - platform: rs485
    name: "ROOM3 Power Socket 1"
    icon: "mdi:power-socket-eu"
    device: [0x1C, 0x01, 0x1F, 0x04, 0x40, 0x40, 0x00]
    state_on:
      offset: 8
      data: [0x01]
    state_off:
      offset: 8
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x41, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x41, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x41, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x41, 0x02, 0x02]
    command_state: [0x0B, 0x01, 0x1F, 0x01, 0x40, 0x40, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기 
  
  - platform: rs485
    name: "ROOM3 Power Socket 2"
    icon: "mdi:power-socket-eu"
    device: [0x1C, 0x01, 0x1F, 0x04, 0x40, 0x40, 0x00]
    state_on:
      offset: 17
      data: [0x01]
    state_off:
      offset: 17
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x42, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x42, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x42, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x42, 0x02, 0x02]
     
   # 주방 콘센트    
  - platform: rs485
    name: "Kitchen Power Socket 1"
    icon: "mdi:power-socket-eu"
    device: [0x1C, 0x01, 0x1F, 0x04, 0x40, 0x50, 0x00]
    state_on:
      offset: 8
      data: [0x01]
    state_off:
      offset: 8
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x51, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x51, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x51, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x51, 0x02, 0x02]
    command_state: [0x0B, 0x01, 0x1F, 0x01, 0x40, 0x50, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기 
    
  - platform: rs485
    name: "Kitchen Power Socket 2"
    icon: "mdi:power-socket-eu"
    device: [0x1C, 0x01, 0x1F, 0x04, 0x40, 0x50, 0x00]
    state_on:
      offset: 17
      data: [0x01]
    state_off:
      offset: 17
      data: [0x02]
    command_on:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x52, 0x01, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x52, 0x01, 0x01]
    command_off:
      data: [0x0b, 0x01, 0x1f, 0x02, 0x40, 0x52, 0x02, 0x00]
      ack:  [0x0b, 0x01, 0x1f, 0x04, 0x40, 0x52, 0x02, 0x02]
      
# RS485 Climate
climate:
  # [거실 난방] 0x11
  # 상태 요청: 0xF7, 0x0B, 0x01, 0x18, 0x01, 0x45, 0x11, 0x00, 0x00, 0xB0, 0xEE
  # 켜짐 상태: 0xF7, 0x0D, 0x01, 0x18, 0x04, 0x45, 0x11, 0x00, (0x01, 0x1B, 0x17), 0xBE, 0xEE (상태, 현재온도, 설정온도)
  # 꺼짐 상태: 0xF7, 0x0D, 0x01, 0x18, 0x04, 0x45, 0x11, 0x00, (0x04, 0x1B, 0x17), 0xBB, 0xEE (상태, 현재온도, 설정온도)
  # 외출 상태: 0xF7, 0x0D, 0x01, 0x18, 0x04, 0x45, 0x11, 0x00, (0x07, 0x1B, 0x17), 0xB9, 0xEE
  # 켜짐 명령: 0xF7, 0x0B, 0x01, 0x18, 0x02, 0x46, 0x11, 0x01, 0x00, 0xB1, 0xEE
  #      ACK: 0xF7, 0x0D, 0x01, 0x18, 0x04, 0x46, 0x11, 0x01, 0x01, 0x1B, 0x17, 0xBC, 0xEE
  # 꺼짐 명령: 0xF7, 0x0B, 0x01, 0x18, 0x02, 0x46, 0x11, 0x04, 0x00, 0xB4, 0xEE
  #      ACK: 0xF7, 0x0D, 0x01, 0x18, 0x04, 0x46, 0x11, 0x04, 0x04, 0x1B, 0x17, 0xBC, 0xEE
  # 온도 조절: 0xF7, 0x0B, 0x01, 0x18, 0x02, 0x45, 0x11, (0x18), 0x00, 0xA7, 0xEE (온도 24도 설정)
  #      ACK: 0xF7, 0x0D, 0x01, 0x18, 0x04, 0x45, 0x11, (0x18), 0x01, (0x1A, 0x18), 0xA8, 0xEE
  - platform: rs485
    name: "Livingroom Heater"
    visual:
      min_temperature: 5 °C
      max_temperature: 40 °C
      temperature_step: 1 °C
    device: [0x0D, 0x01, 0x18, 0x04, 0x45, 0x11, 0x00]
    state_current: #Required (현재온도 State, RS485 Sensor 설정 참고, sensor:로 대체 가능)
      offset: 8
      length: 1
      precision: 0
    state_target: #Required (설정온도 State)
      offset: 9
      length: 1
      precision: 0
    state_off: #Required (끄기 상태)
      offset: 7
      data: [0x04]
    state_heat: #Option (난방모드, 냉방모드: state_cool, 자동모드: state_auto)
      offset: 7
      data: [0x01]
    state_away: #Option (외출모드)
      offset: 7
      data: [0x07]
    command_off: #Required (끄기 명령)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x11, 0x04, 0x00]
      ack: [0x0D, 0x01, 0x18, 0x04, 0x46, 0x11, 0x04, 0x04]
    command_heat: #Option (난방모드 켜기)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x11, 0x01, 0x00]
      ack: [0x0D, 0x01, 0x18, 0x04, 0x46, 0x11, 0x01, 0x01]
    command_away: #Option (외출 켜기)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x11, 0x07, 0x00]
      ack: [0x0D, 0x01, 0x18, 0x04, 0x46, 0x11, 0x07, 0x07]
    command_temperature: !lambda |-  #Required (온도 조절)
      // @param: const float x
      return {
               {0x0B, 0x01, 0x18, 0x02, 0x45, 0x11, (uint8_t)x, 0x00},
               {0x0D, 0x01, 0x18, 0x04, 0x45, 0x11, (uint8_t)x, 0x01}
            };
    command_state: [0x0B, 0x01, 0x18, 0x01, 0x45, 0x11, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기

  - platform: rs485
    name: "room1 Heater"
    visual:
      min_temperature: 5 °C
      max_temperature: 40 °C
      temperature_step: 1 °C
    device: [0x0D, 0x01, 0x18, 0x04, 0x45, 0x12, 0x00]
    state_current: #Required (현재온도 State, RS485 Sensor 설정 참고, sensor:로 대체 가능)
      offset: 8
      length: 1
      precision: 0
    state_target: #Required (설정온도 State)
      offset: 9
      length: 1
      precision: 0
    state_off: #Required (끄기 상태)
      offset: 7
      data: [0x04]
    state_heat: #Option (난방모드, 냉방모드: state_cool, 자동모드: state_auto)
      offset: 7
      data: [0x01]
    state_away: #Option (외출모드)
      offset: 7
      data: [0x07]
    command_off: #Required (끄기 명령)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x12, 0x04, 0x00]
      ack:  [0x0D, 0x01, 0x18, 0x04, 0x46, 0x12, 0x04, 0x04]
    command_heat: #Option (난방모드 켜기)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x12, 0x01, 0x00]
      ack:  [0x0D, 0x01, 0x18, 0x04, 0x46, 0x12, 0x01, 0x01]
    command_away: #Option (외출 켜기)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x12, 0x07, 0x00]
      ack:  [0x0D, 0x01, 0x18, 0x04, 0x46, 0x12, 0x07, 0x07]
    command_temperature: !lambda |-  #Required (온도 조절)
      // @param: const float x
      return {
               {0x0B, 0x01, 0x18, 0x02, 0x45, 0x12, (uint8_t)x, 0x00},
                {0x0D, 0x01, 0x18, 0x04, 0x45, 0x12, (uint8_t)x, 0x01}
            };
    command_state: [0x0B, 0x01, 0x18, 0x01, 0x45, 0x12, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기

  - platform: rs485
    name: "room2 Heater"
    visual:
      min_temperature: 5 °C
      max_temperature: 40 °C
      temperature_step: 1 °C
    device: [0x0D, 0x01, 0x18, 0x04, 0x45, 0x13, 0x00]
    state_current: #Required (현재온도 State, RS485 Sensor 설정 참고, sensor:로 대체 가능)
      offset: 8
      length: 1
      precision: 0
    state_target: #Required (설정온도 State)
      offset: 9
      length: 1
      precision: 0
    state_off: #Required (끄기 상태)
      offset: 7
      data: [0x04]
    state_heat: #Option (난방모드, 냉방모드: state_cool, 자동모드: state_auto)
      offset: 7
      data: [0x01]
    state_away: #Option (외출모드)
      offset: 7
      data: [0x07]
    command_off: #Required (끄기 명령)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x13, 0x04, 0x00]
      ack:  [0x0D, 0x01, 0x18, 0x04, 0x46, 0x13, 0x04, 0x04]
    command_heat: #Option (난방모드 켜기)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x13, 0x01, 0x00]
      ack:  [0x0D, 0x01, 0x18, 0x04, 0x46, 0x13, 0x01, 0x01]
    command_away: #Option (외출 켜기)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x13, 0x07, 0x00]
      ack:  [0x0D, 0x01, 0x18, 0x04, 0x46, 0x13, 0x07, 0x07]
    command_temperature: !lambda |-  #Required (온도 조절)
      // @param: const float x
      return {
               {0x0B, 0x01, 0x18, 0x02, 0x45, 0x13, (uint8_t)x, 0x00},
               {0x0D, 0x01, 0x18, 0x04, 0x45, 0x13, (uint8_t)x, 0x01}
            };
    command_state: [0x0B, 0x01, 0x18, 0x01, 0x45, 0x13, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기

  - platform: rs485
    name: "room3 Heater"
    visual:
      min_temperature: 5 °C
      max_temperature: 40 °C
      temperature_step: 1 °C
    device: [0x0D, 0x01, 0x18, 0x04, 0x45, 0x14, 0x00]
    state_current: #Required (현재온도 State, RS485 Sensor 설정 참고, sensor:로 대체 가능)
      offset: 8
      length: 1
      precision: 0
    state_target: #Required (설정온도 State)
      offset: 9
      length: 1
      precision: 0
    state_off: #Required (끄기 상태)
      offset: 7
      data: [0x04]
    state_heat: #Option (난방모드, 냉방모드: state_cool, 자동모드: state_auto)
      offset: 7
      data: [0x01]
    state_away: #Option (외출모드)
      offset: 7
      data: [0x07]
    command_off: #Required (끄기 명령)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x14, 0x04, 0x00]
      ack:  [0x0D, 0x01, 0x18, 0x04, 0x46, 0x14, 0x04, 0x04]
    command_heat: #Option (난방모드 켜기)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x14, 0x01, 0x00]
      ack:  [0x0D, 0x01, 0x18, 0x04, 0x46, 0x14, 0x01, 0x01]
    command_away: #Option (외출 켜기)
      data: [0x0B, 0x01, 0x18, 0x02, 0x46, 0x14, 0x07, 0x00]
      ack:  [0x0D, 0x01, 0x18, 0x04, 0x46, 0x14, 0x07, 0x07]
    command_temperature: !lambda |-  #Required (온도 조절)
      // @param: const float x
      return {
               {0x0B, 0x01, 0x18, 0x02, 0x45, 0x14, (uint8_t)x, 0x00},
               {0x0D, 0x01, 0x18, 0x04, 0x45, 0x14, (uint8_t)x, 0x01}
             };
    command_state: [0x0B, 0x01, 0x18, 0x01, 0x45, 0x14, 0x00, 0x00] # 요청은 한곳에서만
    update_interval: 30s # 상태요청 주기
   
#엘리베이터 층수 정보###################################
sensor:
  - platform: rs485
    id: EV
    device: [0x0d, 0x01, 0x34, 0x01, 0x41, 0x10, 0x00, 0x02]
    lambda: !lambda |-
      if(len < 9) return {};
       char buf[3];
       sprintf(buf, "%02X ", data[8]);
       if(buf[0] == 'B')
        return atof(&buf[1]) * -1;
       else
        return atof(buf);
    on_value:
      then:
        - text_sensor.template.publish:
            id: EV_TEXT
            state: !lambda |-
              char result[10];
              if(x > 0) sprintf(result, "%1.0f층", x);
              else sprintf(result, "B%1.0f", x);
              return result;
 
text_sensor:
  - platform: template
    name: EV
    id: EV_TEXT
