Energy consumption and Arlec Grid Connect (Tuya) smart plug

Hi. Literally it is the message above. Basically you need to setup the Tuya integration (as described here: Tuya - Home Assistant), then when you add devices in via the Tuya Smart Life app, those devices that are supported will simply appear in home assistant enabling you to turn them on/off etc. The power sensors may not appear, in which case you need to enable them (as mentioned above - it’s just done via the home assistant web interface). If you want to monitor power usage over time via the ‘Energy’ tab in home assistant you can create a sensor that tracks the instant W readings to kW over time via “platform: integration” as shown above - that’s the only yaml editing you need to do, and that is in your sensor section in configuration.yaml or - if you pulled them all into a separate sensor.yaml file - in sensor.yaml.

1 Like

FYI, I bought a 4-pack of the Arlec Smart plug-in socket with energy meter (model PC191HA) from Bunnings for Aus$65.oo.
I followed instructions in the video at Get your Tuya Device Local Key - YouTube to get the Local key and Device specification attributes:

Having installed in the Tuya phone app, I used my router to associate the device MAC addresses to specific IP addresses (which happen to be blocked by the firewall).

I have installed each using the TuyaLoacal integration:

  1. Select the new device from Device ID or IP Address, with Platform of “switch” then as shown:

    plus dp_id 20 for Voltage.
  2. UN-tick the “Do not add any more entities” box, select “number”, Submit, then select dp_id of 9, name “countdown” and range 0 to 86400 (from memory, number of seconds in 1 day).
  3. Same for dp_id 38, relay_status with enumerated values “off”, “on”, and “last”.
  4. no more entities to add, so leave the “Do not add any more entities” box ticked.

The switch works, as does the countdown timer. The relay_status does not - but I can live without it for now.

I see current, current_consumption and voltage values are attributes of the “switch” entities - however they aren’t automagically made available to report on.

I had to add the YAML code from the “Energy monitoring values” section of LocalTuya documentation to my HA configuration.yaml, and they are now reporting correctly (using the custom:multiple-entity-row frontend available in HACS)
Screenshot from 2022-05-25 19-20-44

5 Likes

I also recently bought a bunnings 4-pack of these AC plugs.

Thanks to your help I’ve got it to the point where it can be controlled on/off, and report Current / Power / Voltage correctly.

I’d love to be able to accurately measure the electricity usage over time, which the Tuya app can do, and I noticed one of the device attributes is “add_ele”

Which monitored over time, does change value:

0

It says that it is measured in kW.h, with a scale of “3” and a step of “100”
…whatever that means

Maybe someone out there with better understanding, and some good math’s can turn this value into a energy consumption meter? :wink:

I think You need to convert to kWh then run a utility meter for the daily sum. This will then usable in the energy dashboard.

#Convert W on the GPO's to kWh
  - platform: integration
    source: sensor.po_master_bed_power
    name: po_master_bed_power_k
    unit_prefix: k
    round: 2
    method: left  

utility_meter:
  daily_energy_po_master_bed:
    source: sensor.po_master_bed_power_k
    name: PO Master BED kWh Daily
    cycle: daily
1 Like

Great info.

I also have a number of these devices integrated into Home Assistant using Local Tuya.

Yesterday I plugged another into a pool pump to measure its usage.
All working well but noticed something very odd regarding the Current details provided.

The current output is incorrect, this isn’t just isolated to Home Assistant but also to the Smart Life App/Tuya.

Comparing your figures above also show the incorrect value for charger device.
Your dinning_table values seem to be correct.

ie: 240volt using 5.7W should be around 0.023amps not 0.07a

Any idea what firmware your devices are on?

Thanks

Sorry, but I don’t feel able to answer your queries :frowning:

In the image I posted above, “charger” and “dining_table” were using TP-Link Kasa plugs (which report Amps rather than milliAmps). Since then I have rearranged where each plug is used, and separated fridge/freezers and washer/drier onto separate lovelace cards to make the comparisons more consistent.

I have also found that my wi-fi AP only allows 15 devices connected, so some have been dropping off from time to time.

Moreover I found that my PC191HA switches are always showing Voltage, but only sometimes show non-zero values for Current and Amps; as described in this post.

I am currently waiting for a drayTek AP802 (32 wifi connections per band); and intend to do more thorough testing once all has settled down. Renaming devices has of course also played havoc with HA history, and I’m wondering if it’s a good time to start from scratch again on a new SD card.

I have two of these (2x different models), unfortunately they arent very sensitive at low power consumption (IE, 5w lights).
Recommend if you do use them, target your larger applicances (fridge, washing machine, etc).

1 Like

Thanks for that bit of code, was wondering how to do that.

It seems the plug logs it’s usage every 30mins, but Home Assistant isn’t getting these numbers as reliably through local tuya.

You’re method would work, if maybe every 30 mins a script can request their “add_elec” value directly from the AC plug, but I don’t know if that’s possible :slight_smile:

It took me some time to figure out how to get the current, power, voltage setup as sensors rather than the default attributes with Tuya Loca with the Arlec Grid connect plug-in sockets. A step by step guide;

Setup Tuya local - refer details elsewhere.

  1. from the integration select configure

  2. add new device
    image

  3. Select a discovered device
    image

  4. Configure Tuya device

  5. Select entity type switch
    image

  6. configure entity - ID 1, give it a name. leave the current, consumption and voltage fields blank
    image

  7. next entity type selection. Select sensor, ensure ‘do not add any more entities’ is not checked.
    image

  8. setup Power - ensure scaling factor is correct.
    image

  9. repeat step 7.

  10. setup Voltage. Ensure scaling factor is correct.
    image

  11. repeat step 7.

  12. Setup current
    image

  13. finish adding entities by ticking ‘do not add any more entities’
    image

  14. End result should be a switch control entity and three sensor entities.
    image

11 Likes

Interesting. I also have defined entities for the voltage, current and power when adding the Arlec plug-in socket to see what differences between using entities or attributes.

I do notice a few differences with your screen shots though. In your steps 8, 10 and 12 your images show a “Scaling factor” field which I don’t have. In your step 14 you show a “Sensors” category, where all my entities are listed under the “Controls”.

Both approaches seem to record history, and are easy to work with. After problems with HA getting flooded by messages I have reduced the number of devices connected, removed some of the lovelace cards that may have been taking more CPU time, and gone back to adding sensor templates in configuration.yaml (as in the “Energy monitoring values” section of LocalTuya documentation ) to reduce the amount of network messages needing to be processed.

Ahh, right. I was defining the other entities as number and select types.

Current challenge is accumulating daily total for kWh.

1 Like

that … and that I have noticed that my 3 Arlec smart plugs regularly disconnect and immediately reconnect to my shiny new ASUS RT-AX55 wifi AP - despite being located 2m from the AP with line of sight; and other devices with worse rssi remaining online >200 hours.

But, the weird thing is that it happens regularly at 25 past the hour, most hours, and only affects the Grid connect devices (with up-to-date Tuya firmware).

I’m wondering if this arlec can be connected to Tuya?

Hello,

I have a few Gridconnect Smart Plugs (Energy Monitoring) that appear in the Tuya intergration but not the Local Tuya integration. In the Local Tuya instance - no devices are appearing.

API server region = eu
Client ID = Project client id
Secret = Project secret
User ID = Linked device uid

Do I need to manually add the devices? Or is something not working?

If anyone is interested, I have managed to convert an Arlec PC191HA unit to ESPHome - OTA !.

This was prompted by the teardown https://www.elektroda.com/rtvforum/topic3944452.html by jessemclachlan, which put me onto CloudCutter. CloudCutter not only works with the Tuya WB2S module (and several of the other newer chips Tuya have swapped to) but does it OTA, so no having to pry the units open and apply a soldering iron !

I used CloudCutter to cut it from Tuya cloud leaving the Tuya firmware; and then later used CloudCutter, LibreTuya and the libretuya-esphome fork of ESPHome to install a basic ESPHome firmware.

Still more to be done to give the same results as the Tuya firmware (especially calibrating the voltage reading :wink:) but hopefully ESPHome will be more reliable than LocalTuya was for me.

esphome:
  name: pc191ha-108

libretuya:
  board: wb2s
  framework:
    version: dev

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret esphome_api_encryption

ota:
  password: !secret esphome_ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  use_address: 192.168.1.108

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "pc191ha-108 Fallback Hotspot"
    password: "v8ag08b0GTNj"

captive_portal:
    
web_server:
  port: 80

#
# PC191HA configuration 

binary_sensor:        # button   (bt1_pin: 11)
  - platform: gpio
    pin: P11
    name: "pc191ha-108_button"
    id:   pc191ha_108_button
    device_class: window
    on_press:
      then:
        - switch.toggle: pc191ha_108_relay  # toggle the relay

light:                # LED in the button  (led1_pin: 26)
  - platform: status_led
    name: "pc191ha-108 Switch state"
    id:   pc191ha_108_led
    pin: P26

switch:              # relay    (rl1_pin: 6)
  - platform: gpio
    pin: P6
    name: "pc191ha-108_relay"
    id:   pc191ha_108_relay
    on_turn_on:
      then:
        - light.turn_on: pc191ha_108_led
    on_turn_off:
      then:
        - light.turn_off: pc191ha_108_led

# Example configuration entry for device BL0937 using inverted SEL pin functionality
sensor:
  - platform: hlw8012
    model: BL0937
    sel_pin:
      inverted: true
      number: P24
    cf_pin: 
      inverted: true  # the logic of BL0937 is opposite from HLW8012
      number: P7
    cf1_pin: 
      inverted: true  # the logic of BL0937 is opposite from HLW8012
      number: P8
    current:
      name: "pc191ha-108 Current"
    voltage:
      name: "pc191ha-108 Voltage"
    power:
      name: "pc191ha-108 Power"
    update_interval: 10s
    change_mode_every: 3
6 Likes

Update: I converted two units of the same device, and have them running.

On one unit I added reporting the wi-fi signal strength (I have previously had devices drop off my WLAN), added restoring the previous state when powered on; and was refining the calibration of the Voltage and Amperage readings … when the I noticed that the firmware configuration wasn’t changing !
Firmware still appeared to compile and load OTA. But even when I trimmed it down to wifi only, it still reported the power readings and operated the switch !!!

I tried asking for help on discord, but that wasn’t helpful. I don’t want to lock up my other devices, if that is what has happened to the one which now wont update firmware.

So I have put this whole issue aside for a while. Maybe Libretuya-esphome will be updated soon, and maybe I’ll try from scratch again after a fresh install of Ubuntu 23.04.

1 Like

Hi Don, I’ve managed to flash one of mine with a basic yaml, I was about to try and re flash with your yaml. Any progress on the more advanced yaml and overcoming the re-flashing bug?

Edit: I used what ever version of code for cloud cutter, ESPHome / libretuya was available yesterday. I can confirm although the OTA esp home update appears successful, on reboot the old firmware is still in place.

I now have a basic firmware (with no useful config like buttons and switches) on the device. Basically useless :slight_smile:

1 Like

My frustration with this issue and the response I received on discord resulted in me deciding to put the whole thing aside for a while.

After a week of being powered off; I powered them on again, and the new setting took ! I guess that I was being impatient and not giving enough time for the units to properly reset after flashing.
The result is that I have 2 units running ESPHome and returning values - but the values are not in the correct range, and so I have not modified my automations or flashed my other 3 arlec power plugs.

I guess its time I got back and experimented with the values for voltage_divider and current_resistor. This time giving each unit a full power cycle after flashing. I saw that someone suggested a method for draining the capacitor more quickly

I was planning to wait till I got reasonable values for voltage_divider and current_resistor (accepting that each unit would need individual calibration to be properly accurate anyway) before posting my final code, but I may as well post my current firmware yaml now. Hopefully my comments and researching the options I found and incorporated will be useful to others. Note that my device names include the last number of IP address (eg pc191ha-108) and so I have used yaml substitutions, so that I only need to change the value of deviceIP once at the top when I copy the yaml code to a different unit.

#
# Arlec PC191HA - power plug with energy monitoring
#       Uses a Tuya WB2S module with BL0937 for measuring power consumption
#

# Note: My devices are named including the last part of their IP address (eg pc191ha-108), 
#       and so I have used sustitutions so that I can easily copy this yaml 
#       and need only change the deviceIP.  You do not need to do this. 
substitutions:
  deviceIP: "108"
  devicename: pc191ha-$deviceIP
  deviceID: pc191ha_$deviceIP

esphome:
  name: $devicename

libretuya:
  board: wb2s
  framework:
    version: dev

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "HgoRlyg+sp+PXuiZ0h8fcSCxq+HxVp9hvmCAPwzMnW8="

ota:
  password: !secret esphome_ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  use_address: 192.168.1.$deviceIP
  fast_connect: True

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "pc191ha Fallback Hotspot"
    password: "v8ag08b0GTNj"

captive_portal:

web_server:
  port: 80


#
# PC191HA basic switch operation - button, relay and LED
#
    # Pressing the button activates the switch, and the switch controls the light.
    #
    # Notes:
    #   - 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 
    #   - Switch and relay are different names for the same thing
    #   - Home Assistant uses the status of the switch (also called relay), and so 
    #       no point also exposing button and LED to HA - use internal: true option
    #   - There is also a wifi_LED, but it is not seen from outside the case

binary_sensor:        # button   (bt1_pin: 11)
  - platform: gpio
    pin: P11
    name: $devicename button
    id:   ${deviceID}_button
    device_class: window
    # when button is pressed, toggle the switch on/off
    on_press:
      then:
        - switch.toggle: ${deviceID}_relay  # toggle the relay / switch
    internal: True

switch:              # relay    (rl1_pin: 6)
  - platform: gpio
    pin: P6
    name: $devicename relay
    id:   ${deviceID}_relay
    restore_mode: RESTORE_DEFAULT_OFF
    # synchronise the LED with the relay
    on_turn_on:
      then:
        - light.turn_on: ${deviceID}_led
    on_turn_off:
      then:
        - light.turn_off: ${deviceID}_led
  - platform: restart
    name: $devicename Restart

light:                # LED in the button  (led1_pin: 26)
  - platform: status_led
    name: $devicename Switch state
    id:   ${deviceID}_led
    pin: P26
    restore_mode: RESTORE_DEFAULT_OFF
    internal: True


#
# PC191HA sensors - power monitoring
#
sensor:
  # I found i can add reporting the wi-fi signal strength received by the device, 
  #     which may be different from the wi-fi signal received by the Access Point. 
  # May help me in debugging wi-fi issues. Not necessary for operation of pc191ha.
  - platform: wifi_signal
    name: $devicename WiFi Signal
    id:   ${deviceID}_wifi_signal
    update_interval: 50s    # 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
      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: P8

    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 
    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 possibly inaccurate

    # Adjust according to the actual resistor values on board to calibrate the specific unit
    voltage_divider: 740      # LOWER VALUE GIVES LOWER VOLTAGE
    current_resistor: 1.00    # HIGHER VALUE GIVES LOWER WATTAGE
      # power is simply current x voltage, so needs no separate calibration

    # how the power monitoring values are returned to ESPHome
    current:
      name: $devicename Current
      id:   ${deviceID}_current
      unit_of_measurement: A
      accuracy_decimals: 4
    voltage:
      name: $devicename Voltage
      id:   ${deviceID}_voltage
      unit_of_measurement: V
      accuracy_decimals: 1
    power:
      name: $devicename Power
      id:   ${deviceID}_power
      unit_of_measurement: W
      accuracy_decimals: 4
1 Like

Hi Don,

I have been pulling my hair out two, just read your post and shorted the main input pins and put a load on it to discharge the cap and now ota update works.

with the voltage is this not giving peak to peak instead off rms?

*** Edit ***
This discharge don’t work, it’s is random on times. But the new firmware from Libretiny now does OTA.

Just flashed and refreshed 10 devices.

Also if a device is bricked from this don’t do a manual flash institute, remove the wb2s. (RIP buspriate)

I have also put esphome on a ws3b double gang light switch from Deta.

I found it’s best using the libretiny esphome flash to do this.