Energy consumption and Arlec Grid Connect (Tuya) smart plug

got it, thanks for the clarification!

Can anyone give me the state of play for local control of a PC191HA? This one I’ve got is 1.1.17 so apparently OTA doesn’t work any more, but I’m supes unfamilly

Nothing has changed, you need to open up as described above. I recently bought some series 3 plugs and converted all 4.

1 Like

Nightmare fuel, ty

Anyone else struggle with the Series 3 versions occasionally getting stuck unable to boot properly? I assume a brownout or something, but it’s so intermittent (once every week or so) that it’s very hard to debug.

It will still say it’s connected on my router settings, but is unavailable in HA. I’ve tried various things like enabling safemode and reboot timeout, as well as disabling the webserver and a few other things to try save memory. I also tried a debug sensor for reboot reason but this seems to not elicit anything useful.

################################################################################
# Substitution Variables
################################################################################
substitutions:
  device_internal_name: bedroom_fish_tank_light
  device_friendly_name: Bedroom Fish Tank Light
  device_wifi_name: FishTankLight
  device_ip_address: 192.168.68.72
  device_sampling_time: 5s



################################################################################
# Globals
################################################################################
globals: ##to set default reboot behavior
  - id: wifi_connection
    type: bool
    restore_value: no
    initial_value: "false"



################################################################################
# Board Configuration
################################################################################
esphome:
  name: ${device_internal_name}
  friendly_name: ${device_friendly_name}

bk72xx:
  board: cb2s
  framework:
    version: dev #1.7.0



################################################################################
# Enable logging
################################################################################
logger:
  baud_rate: 0  # disable serial uart logging



################################################################################
# Enable Home Assistant API
################################################################################
api:
  reboot_timeout: 0s



################################################################################
# OTA
################################################################################
ota:
  - platform: esphome

safe_mode:
  disabled: false
  reboot_timeout: 10min
  num_attempts: 3



################################################################################
# WiFi
################################################################################
wifi:
  ssid: !secret wifi_ssid
  domain: !secret wifi_domain
  password: !secret wifi_password

  reboot_timeout: 10min
  power_save_mode: NONE

  manual_ip:
    static_ip: $device_ip_address
    gateway: !secret gateway
    subnet: !secret subnet_mask
  
#   ap: # Enable fallback hotspot (captive portal) in case wifi connection fails
#     ssid: ${device_wifi_name}
#     password: !secret fallback_hotspot_password
# captive_portal:



################################################################################
# Web Server
################################################################################
# web_server:
#   port: 80
#   version: 2
#   include_internal: true
#   local: true



################################################################################
# Time
################################################################################
time:
  - platform: homeassistant
    id: homeassistant_time

debug:

#
# 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

  #-------------------------------------------------------------------------------
  # ESP32 Internal Binary Sensors
  #-------------------------------------------------------------------------------
  
  # ESP Status
  - platform: status
    name: "Status"
    id: ${device_internal_name}_status
    device_class: connectivity
    entity_category: "diagnostic"
    disabled_by_default: true       # Shows entity in HA, but disabled by default 

  - platform: gpio
    pin: P11
    name: ${device_friendly_name} Button
    id: button
    device_class: window
    # when button is pressed, toggle the switch on/off
    on_press:
      then:
        - switch.toggle: relay

switch:          # the relay

  #-------------------------------------------------------------------------------
  # ESP32 Internal Switches
  #-------------------------------------------------------------------------------

  - platform: restart
    name: "Restart"
    id: device_restart
    disabled_by_default: True
    entity_category: diagnostic
  
  - platform: safe_mode
    name: Use Safe Mode
    id: device_safe_mode
    disabled_by_default: true       # Shows entity in HA, but disabled by default
    device_class: "switch"
    entity_category: "diagnostic"


  - platform: gpio
    pin: P8
    name: ${device_friendly_name}
    id: relay
    restore_mode: RESTORE_DEFAULT_ON   # 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: P26
#    restore_mode: always off   # default when power is turned on

#
# PC191HA sensors - power monitoring and wifi signal
#
sensor:
  #-------------------------------------------------------------------------------
  # ESP Internal Sensors
  #-------------------------------------------------------------------------------
  
  # WiFi
  - platform: wifi_signal
    name: "WiFi Signal dB"
    id: ${device_internal_name}_wifi_signal_db
    update_interval: ${device_sampling_time}
    entity_category: "diagnostic"
    device_class: "signal_strength"
    disabled_by_default: true       # Shows entity in HA, but disabled by default
  - platform: copy                  # Reports the WiFi signal strength %
    source_id: ${device_internal_name}_wifi_signal_db
    name: "WiFi Signal"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "%"
    entity_category: "diagnostic"
    disabled_by_default: true       # Shows entity in HA, but disabled by default

  # Uptime
  - platform: uptime
    name: "Uptime Sensor"
    id: ${device_internal_name}_uptime_sensor
    update_interval: ${device_sampling_time}
    disabled_by_default: true       # Shows entity in HA, but disabled by default
    entity_category: "diagnostic"
    on_raw_value:
      then:
        - text_sensor.template.publish:
            id: ${device_internal_name}_uptime_human
            state: !lambda |-
              int seconds = round(id(${device_internal_name}_uptime_sensor).raw_state);
              int days = seconds / (24 * 3600);
              seconds = seconds % (24 * 3600);
              int hours = seconds / 3600;
              seconds = seconds % 3600;
              int minutes = seconds /  60;
              seconds = seconds % 60;
              return (
                (days ? to_string(days) + "d " : "") +
                (hours ? to_string(hours) + "h " : "") +
                (minutes ? to_string(minutes) + "m " : "") +
                (to_string(seconds) + "s")
              ).c_str();


  #-------------------------------------------------------------------------------
  # ESP Internal Sensors
  #-------------------------------------------------------------------------------
    # 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: P24
    cf_pin:           # current or voltage (ele_pin: 7)
      inverted: true  # the logic of BL0937 is opposite from HLW8012
      number: P7
    cf1_pin:          #  Power (vi_pin: 8)
      inverted: true  # the logic of BL0937 is opposite from HLW8012
      number: P6

    update_interval: 15s      # 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:  775     # 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:   ${device_internal_name}_voltage
      unit_of_measurement: V
      accuracy_decimals: 1
      filters:
        - skip_initial: 2
    power:
      name: Power
      id:   ${device_internal_name}_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:   ${device_internal_name}_current
    unit_of_measurement: A
    accuracy_decimals: 2
    update_interval: "30s"
    lambda: |-
      return (id(${device_internal_name}_power).state / id(${device_internal_name}_voltage).state);
    filters:  
      - skip_initial: 2


################################################################################
# Interval
################################################################################
interval:


  #-------------------------------------------------------------------------------
  # ESP32 Internal Intervals
  #-------------------------------------------------------------------------------
  
  - interval: 10s
    then:
      - if:
          condition:
            wifi.connected:
          then:
            - lambda: |-
                id(wifi_connection) = true;
          else:
            - lambda: |-
                id(wifi_connection) = false;


text_sensor:
  - platform: debug
    device:
      name: Device Info
    reset_reason:
      name: Reset Reason

  - platform: template
    id: ${device_internal_name}_uptime_human
    icon: mdi:clock-start
    disabled_by_default: True
    entity_category: "diagnostic"