samhunt / homebridge-esphome-ts

This plugin integrates your esphome devices into HomeKit!
GNU General Public License v3.0
2 stars 2 forks source link

Can't trigger ESPhome relay #13

Open ilium007 opened 3 weeks ago

ilium007 commented 3 weeks ago

I have an ESPhome relay (240v plug with button and relay) and whilst I can get it to connect via API (I can see the API connection inthe ESPhome logs) I can't get it to trigger the relay.

Logs from this plugin (repeated over and over)

[6/13/2024, 7:28:56 PM] [@sam-hunt/homebridge-esphome-ts] This plugin threw an error from the characteristic 'On': Unhandled error thrown inside write handler for characteristic: Not connected. See https://homebridge.io/w/JtMGR for more info.
[6/13/2024, 7:28:57 PM] [@sam-hunt/homebridge-esphome-ts] This plugin threw an error from the characteristic 'On': Unhandled error thrown inside write handler for characteristic: Not connected. See https://homebridge.io/w/JtMGR for more info.
[6/13/2024, 7:28:57 PM] [@sam-hunt/homebridge-esphome-ts] This plugin threw an error from the characteristic 'On': Unhandled error thrown inside write handler for characteristic: Not connected. See https://homebridge.io/w/JtMGR for more info.
[6/13/2024, 7:28:57 PM] [@sam-hunt/homebridge-esphome-ts] This plugin threw an error from the characteristic 'On': Unhandled error thrown inside write handler for characteristic: Not connected. See https://homebridge.io/w/JtMGR for more info.
[6/13/2024, 7:28:57 PM] [@sam-hunt/homebridge-esphome-ts] This plugin threw an error from the characteristic 'On': Unhandled error thrown inside write handler for characteristic: Not connected. See https://homebridge.io/w/JtMGR for more info.
[6/13/2024, 7:28:57 PM] [@sam-hunt/homebridge-esphome-ts] This plugin threw an error from the characteristic 'On': Unhandled error thrown inside write handler for characteristic: Not connected. See https://homebridge.io/w/JtMGR for more info.
ilium007 commented 3 weeks ago

Homebridge (plugin) is definitely connecting to esphome device:

[20:24:51][W][api.connection:104]: homebridge-esphome-ts (192.168.120.62): Connection closed
[20:24:56][D][api:102]: Accepted 192.168.120.62
[20:24:56][W][component:237]: Component api took a long time for an operation (53 ms).
[20:24:56][W][component:238]: Components should block for at most 30 ms.
[20:24:56][D][api.connection:1321]: homebridge-esphome-ts (192.168.120.62): Connected successfully
ilium007 commented 3 weeks ago
[6/13/2024, 8:22:24 PM] [@sam-hunt/homebridge-esphome-ts] This plugin threw an error from the characteristic 'On': Unhandled error thrown inside write handler for characteristic: Not connected. See https://homebridge.io/w/JtMGR for more info.
[6/13/2024, 8:22:24 PM] [@sam-hunt/homebridge-esphome-ts] Error: Not connected
    at EsphomeNativeApiConnection.sendCommandMessage (/var/lib/homebridge/node_modules/@sam-hunt/homebridge-esphome-ts/node_modules/@2colors/esphome-native-api/lib/connection.js:175:36)
    at Function.commandService (/var/lib/homebridge/node_modules/@sam-hunt/homebridge-esphome-ts/node_modules/@2colors/esphome-native-api/lib/entities/Light.js:115:20)
    at EsphomeNativeApiConnection.lightCommandService (/var/lib/homebridge/node_modules/@sam-hunt/homebridge-esphome-ts/node_modules/@2colors/esphome-native-api/lib/connection.js:301:24)
    at updateEsp (/var/lib/homebridge/node_modules/@sam-hunt/homebridge-esphome-ts/dist/homebridgeAccessories/switch.js:30:30)
    at On.<anonymous> (/var/lib/homebridge/node_modules/@sam-hunt/homebridge-esphome-ts/dist/homebridgeAccessories/switch.js:16:13)
    at On.emit (node:events:518:28)
    at /var/lib/homebridge/node_modules/homebridge/node_modules/hap-nodejs/src/lib/Characteristic.ts:2596:16
    at new Promise (<anonymous>)
    at On.<anonymous> (/var/lib/homebridge/node_modules/homebridge/node_modules/hap-nodejs/src/lib/Characteristic.ts:2594:14)
    at step (/var/lib/homebridge/node_modules/homebridge/node_modules/tslib/tslib.js:195:27)
[6/13/2024, 8:22:45 PM] [@sam-hunt/homebridge-esphome-ts] This plugin threw an error from the characteristic 'On': Unhandled error thrown inside write handler for characteristic: Not connected. See https://homebridge.io/w/JtMGR for more info.
[6/13/2024, 8:22:45 PM] [@sam-hunt/homebridge-esphome-ts] Error: Not connected
    at EsphomeNativeApiConnection.sendCommandMessage (/var/lib/homebridge/node_modules/@sam-hunt/homebridge-esphome-ts/node_modules/@2colors/esphome-native-api/lib/connection.js:175:36)
    at Function.commandService (/var/lib/homebridge/node_modules/@sam-hunt/homebridge-esphome-ts/node_modules/@2colors/esphome-native-api/lib/entities/Light.js:115:20)
    at EsphomeNativeApiConnection.lightCommandService (/var/lib/homebridge/node_modules/@sam-hunt/homebridge-esphome-ts/node_modules/@2colors/esphome-native-api/lib/connection.js:301:24)
    at updateEsp (/var/lib/homebridge/node_modules/@sam-hunt/homebridge-esphome-ts/dist/homebridgeAccessories/switch.js:30:30)
    at On.<anonymous> (/var/lib/homebridge/node_modules/@sam-hunt/homebridge-esphome-ts/dist/homebridgeAccessories/switch.js:16:13)
    at On.emit (node:events:518:28)
    at /var/lib/homebridge/node_modules/homebridge/node_modules/hap-nodejs/src/lib/Characteristic.ts:2596:16
    at new Promise (<anonymous>)
    at On.<anonymous> (/var/lib/homebridge/node_modules/homebridge/node_modules/hap-nodejs/src/lib/Characteristic.ts:2594:14)
    at step (/var/lib/homebridge/node_modules/homebridge/node_modules/tslib/tslib.js:195:27)
ilium007 commented 3 weeks ago

I should add that this device has an mqtt config on it as well and works perfectly from HomeAssistant.

ilium007 commented 3 weeks ago

This is the ESPhome device config:

substitutions:
  device_name: arleca82734
  name: "Bedroom 3 Plug 0"

esphome:
  name: ${device_name}
  friendly_name: ${name}
  comment: ${name}

  platformio_options:
    board_build.f_flash: 40000000L
    board_build.flash_mode: dio
    board_build.flash_size: 4MB

esp32:
  board: esp32-c3-devkitm-1
  framework:
    #type: arduino
    type: esp-idf

# Enable logging
logger:
#  level: VERBOSE
#  baud_rate: 115200
#  deassert_rts_dtr: true

api:
  encryption:
    key: "xxx"

ota:
  password: "xxx"

#web_server:
#  port: 80

wifi:
  fast_connect: on
  power_save_mode: none
  domain: .xxx

  networks:
    - ssid: !secret wifi_ssid
      password: !secret wifi_password

  use_address: 192.168.120.21

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Arlec-A82734 Fallback Hotspot"
    password: "xxx"

captive_portal:

mqtt:
  discovery_unique_id_generator: mac
  broker: !secret mq_broker_ip
  port: 1883
  username: xxx
  password: xxx
  topic_prefix: home/xxx/indoor/bedroom/3/plug/${device_name}

button:
  - platform: restart
    name: Restart

# PC191HA basic switch operation - button, relay and LED
#
    # button is momentary on - shows "on" in HA except for the moment the button is being pressed
    # LED should have same on/off state as the relay.  
    # there is also a wifi_LED, but it is not seen from outside the case

binary_sensor:    # the button
  - platform: gpio
    pin: 
      number: GPIO21
      inverted: true
    name: button
    device_class: window
    # when button is pressed, toggle the switch on/off
    on_press:
      then:
        - switch.toggle: relay
    internal: True

switch:          # the relay
  - platform: gpio
    pin: GPIO06
    name: Relay
    id: relay
    restore_mode: always off   # default when power is turned on
    icon: mdi:power-socket-au
    # synchronise the LED with the relay
    on_turn_on:
      then:
        - output.turn_on: button_led
    on_turn_off:
      then:
        - output.turn_off: button_led

output:        # the light in the button
  - platform: gpio
    id: button_led
    pin: GPIO04

# wifi status LED
status_led:
  pin: GPIO20

#
# PC191HA sensors - power monitoring and wifi signal
#

text_sensor:
  - platform: version
    name: ESPhome Version

  - platform: wifi_info
    ip_address:
      name: IP

  #  Creates a sensor of the uptime of the device, in formatted days, hours, minutes and seconds
  - platform: template
    name: Device Uptime
    entity_category: diagnostic
    lambda: |-
      int seconds = (id(uptime_seconds).state);
      int days = seconds / (24 * 3600);
      seconds = seconds % (24 * 3600);
      int hours = seconds / 3600;
      seconds = seconds % 3600;
      int minutes = seconds /  60;
      seconds = seconds % 60;
      if ( days > 3650 ) {
        return { "Starting up" };
      } else if ( days ) {
        return { (esphome::to_string(days) +"d " + esphome::to_string(hours) +"h " + esphome::to_string(minutes) +"m "+ esphome::to_string(seconds) +"s").c_str() };
      } else if ( hours ) {
        return { (esphome::to_string(hours) +"h " + esphome::to_string(minutes) +"m "+ esphome::to_string(seconds) +"s").c_str() };
      } else if ( minutes ) {
        return { (esphome::to_string(minutes) +"m "+ esphome::to_string(seconds) +"s").c_str() };
      } else {
        return { (esphome::to_string(seconds) +"s").c_str() };
      }
    icon: mdi:clock-start

sensor:
  - platform: uptime
    id: uptime_seconds

  - platform: wifi_signal         # report wi-fi signal strength from this end
    name: WiFi Signal
    update_interval: 30s    # how often to report wifi signal strength

    # PC191HA includes a BL0937 chip for measuring power consumption
    #     and BL0937 is a variation of hlw8012, but using inverted SEL pin functionality
  - platform: hlw8012
    model: BL0937     # note that the model must be specified to use special calculation parameters
    sel_pin:          # I believe that cf_pin reports either Voltage or Current depending on this select pin
      inverted: true  # determine whether true reports Voltage
      number: GPIO05
    cf_pin:           # current or voltage (ele_pin: 7)
      inverted: false  # the logic of BL0937 is opposite from HLW8012
      number: GPIO07
    cf1_pin:          #  Power (vi_pin: 8)
      inverted: false  # the logic of BL0937 is opposite from HLW8012
      number: GPIO03

    update_interval: 30s      # How often to measure and report values

    # PC191HA measures and returns Voltage OR Current according to the value of sel_pin,  
    #   but it can change the value of sel_pin periodically
    initial_mode: "VOLTAGE"       # reports VOLTAGE or CURRENT
    change_mode_every: 4          # how many times to report before swapping between
        #   reporting Voltage or Current. Note that the first value reported should be ignored as inaccurate

    # Adjust according to the actual resistor values on board to calibrate the specific unit
    voltage_divider:  1550     # LOWER VALUE GIVES LOWER VOLTAGE
    current_resistor: 0.0009  # HIGHER VALUE GIVES LOWER WATTAGE

    #
    # how the power monitoring values are returned to ESPHome
    #

    voltage:
      name: Voltage
      id: voltage
      unit_of_measurement: V
      accuracy_decimals: 1
      filters:
        - skip_initial: 2
    power:
      name: Power
      id: power
      unit_of_measurement: W
      accuracy_decimals: 2
      filters:
        - skip_initial: 2

    # power should simply be current x voltage -- except that the pc191ha doesn't follow that formula.  
    # Setting current_resistor to give an accurate Amperage does NOT also give the correct Wattage
    # so here I calculate current from power and voltage

  - platform: template  
    name: Current
    id: current
    unit_of_measurement: A
    accuracy_decimals: 2
    update_interval: "30s"
    lambda: |-
      return (id(power).state / id(voltage).state);
    filters:  
      - skip_initial: 2
ilium007 commented 3 weeks ago

This is the Homebridge plugin config:

{
    "devices": [
        {
            "host": "192.168.120.21",
            "port": 6053,
            "encryptionKey": "xxx",
            "excludedNames": [
                "IP",
                "Voltage",
                "Power",
                "ESPhome Version",
                "Device Uptime"
            ]
        }
    ],
    "debug": true,
    "platform": "esphome"
}