Energy consumption and Arlec Grid Connect (Tuya) smart plug

I got another 4 of these flashed without much drama. Much easier and faster after you’ve done your first.

There’s an opportunity to use these things as ble scanners (or bluetooth proxies?).

But I know those things are memory intensive, so maybe not a good idea.

I had a go at using a common config using include.

In the devices yaml (arlec-smartplug-3.yaml)

<<: !include .arlec-smartplug-common.yaml
  
esphome:
  name: arlec-smartplug-3
  friendly_name: Arlec Smartplug 3
  comment: "Arlec White Grid Connect Smart Plug in Socket with Energy Meter"  
 
wifi:
  ap:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.1.131
    gateway: 192.168.1.1
    subnet: 255.255.255.0

in .arlec-smartplug-common.yaml

# Config for Arlec White Grid Connect Smart Plug in Socket with Energy Meter 
# Product Code: PC191HA (White) & PC191BKHA (Black). “Series 2” (CB2S module, BK7231N chip)
# https://www.bunnings.com.au/arlec-white-grid-connect-smart-plug-in-socket-with-energy-meter_p0273367
# https://upk.libretiny.eu/?profile=arlec-pc191ha-smart-plug-bk7231n-v1.1.8
# Copied some config from Athom: https://github.com/athom-tech/athom-configs/blob/e9d1218fe0d8b4631cda211d204dd3ff5ac698be/athom-smart-plug-v2.yaml

substitutions:
  relay_restore_mode: RESTORE_DEFAULT_OFF         #Adjust as required.
  hlw8012_update_interval: 30s                    #Not sure how this affects lifetime and maybe overheating? 
  

bk72xx:
  board: generic-bk7231n-qfn32-tuya #https://docs.libretiny.eu/boards/cb2s/  

logger:
mdns:
api:
  password: ""
ota:
  password: ""

globals:
  - id: total_energy
    type: float
    restore_value: yes
    initial_value: '0.0'
  
sensor:
  - platform: uptime
    name: Uptime
  - platform: wifi_signal
    name: "WiFi Signal"
    update_interval: 60s
    
  - platform: hlw8012
    model: BL0937
    update_interval: ${hlw8012_update_interval}
    change_mode_every: 3
    voltage_divider: 770 #Required changing
    current_resistor: 0.001 ohm
    cf_pin:
      number: P8
      inverted: true
    cf1_pin:
      number: P7
      inverted: true
    sel_pin:
      number: P6
      inverted: true
    current:
      name: Current
      filters:
        - multiply: 0.4250 #https://community.home-assistant.io/t/esphome-install-does-not-update-firmware/544026/10?u=mahko_mahko
        - lambda: if (x < 0.01) {return 0;} else {return x;}
    voltage:
      name: Voltage
    power:
      name: Power
      id: power_sensor
      filters:
        - multiply: 0.97
        - lambda: if (x < 0.01) {return 0;} else {return x;}
    energy:
      name: Energy
      id: energy
      unit_of_measurement: kWh
      filters:
        - multiply: 0.001  # Multiplication factor from W to kW is 0.001
      on_value:
        then:
          - lambda: |-
              static float previous_energy_value = 0.0;
              float current_energy_value = id(energy).state;
              id(total_energy) += current_energy_value - previous_energy_value;
              previous_energy_value = current_energy_value;
              
  - platform: template
    name: "Total Energy"
    unit_of_measurement: kWh
    device_class: "energy"
    state_class: "total_increasing"
    icon: "mdi:lightning-bolt"
    accuracy_decimals: 3
    lambda: |-
      return id(total_energy);
    update_interval: ${hlw8012_update_interval}
    
  - platform: total_daily_energy
    name: "Total Daily Energy"
    restore: true
    power_id: power_sensor
    unit_of_measurement: kWh
    accuracy_decimals: 3
    filters:
      - multiply: 0.001
    
output:
  - platform: libretiny_pwm
    id: output_led_1
    pin:
      number: P23
      inverted: true

light:
  - platform: monochromatic
    id: light_switch_1
    output: output_led_1

binary_sensor:
  - platform: gpio
    id: binary_switch_1
    pin:
      number: P10
      inverted: true
      mode: INPUT_PULLUP
    on_multi_click:
      - timing:
          - ON for at most 1s
          - OFF for at least 0.2s
        then:
          - switch.toggle: switch_1
      - timing:
          - ON for at least 4s
        then:
          - button.press: Reset

switch:
  - platform: gpio
    id: switch_1
    name: Relay 1
    pin: P24
    on_turn_on:
      - light.turn_on: light_switch_1
    on_turn_off:
      - light.turn_off: light_switch_1
    restore_mode: ${relay_restore_mode}

status_led:
  pin:
    number: P26
    inverted: true
    
text_sensor:
  - platform: libretiny
    version:
      name: LibreTiny Version
  - platform: wifi_info
    ip_address:
      name: "IP Address"
    ssid:
      name: "Connected SSID"
    mac_address:
      name: "Mac Address"
      
button:
  - platform: factory_reset
    name: "Restart with Factory Default Settings"
    id: Reset

  - platform: safe_mode
    name: "Safe Mode"
    internal: false
    
time:
  - platform: sntp
    id: sntp_time