ERROR: Using raw pin numbers is ambiguous

Pretty new to ESPHome, setting my my first ESPHome configuration file, for a Arlec PC191HA power plug. It has a profile on Blakadder (Arlec Plug In Socket Power Monitoring Plug (PC191HA) Configuration for Tasmota) which includes a list of GPIO numbers, and the instruction to replace the WB2S module. Instead i have run cloudcutter for this device, and also used it to load an initial basic LibreTuya-esphome configuration. So far so good …

Now I have assembled a configuration from components on esphome.io website which I hope will duplicate the original functionality:

esphome:
  name: "pc191ha-109"

libretuya:
  board: wb2s
  framework:
    version: dev

# Enable logging
logger:

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

ota:
#  password: "eb9494d8b9ac189def0a76bc22bb638b"
  password: ""

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

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

captive_portal:
    
web_server:
  port: 80


# Example configuration entry
binary_sensor:
  - platform: gpio
    pin: 01
    name: "pc191ha-109_button"
    device_class: window

# Example configuration entry
light:
  - platform: status_led
    name: "Switch state"
    pin: 14

# Example configuration entry
switch:
  - platform: gpio
    pin: 13
    name: "pc191ha-109_switch"

# Example configuration entry for device BL0937 using inverted SEL pin functionality
sensor:
  - platform: hlw8012
    model: BL0937
    sel_pin:
      number: GPIO12
      inverted: true
    cf_pin: GPIO04
    cf1_pin: GPIO05
    current:
      name: "BL0937 Current"
    voltage:
      name: "BL0937 Voltage"
    power:
      name: "BL0937 Power"
    update_interval: 60s

Problem is that when I use the libreTuya-esphome (a fork of esphome) HA add-on to install, I get errors about the pin numbers. Not sure whether the issue comes from upstream (ESPHome) or libretuya-esphome - hence posting also to the larger community here.

With numeric pin numbers I get

INFO Reading configuration /config/libretuya-esphome/pc191ha-109.yaml... 
Failed config 

binary_sensor.gpio: [source /config/libretuya-esphome/pc191ha-109.yaml:38] 
	platform: gpio 
	
	Using raw pin numbers is ambiguous. Use either D1, A1 or P1 - all these are different pins! Refer to the docs for details. 
	pin: 1 
	name: pc191ha-109_button 
	device_class: window 
light.status_led: [source /config/libretuya-esphome/pc191ha-109.yaml:45] 
	platform: status_led 
	name: Switch state 

	Using raw pin numbers is ambiguous. Use either D14, A14 or P14 - all these are different pins! Refer to the docs for details. 
	pin: 14 switch.gpio: [source /config/libretuya-esphome/pc191ha-109.yaml:51] 
	platform: gpio 

Using raw pin numbers is ambiguous. Use either D13, A13 or P13 - all these are different pins! Refer to the docs for details. 
	pin: 13 
	name: pc191ha-109_switch 

however all the docs I have found so far seem to use GPIO pin numbers, most often without an D, A or P prefix !!! Which doc is this referring to ?

I’ve googled for “Using raw pin numbers is ambiguous”, but no results.

If I prefix them all with “GPIO” I get

Compiling /data/pc191ha-109/.pioenvs/pc191ha-109/src/main.cpp.o 
src/main.cpp: In function 'void setup()': 
src/main.cpp:214:45: error: 'PIN_FUNCTION_P01' was not declared in this scope 
	libretuya_arduinointernalgpiopin->set_pin(PIN_FUNCTION_P01); 
						     ^ 
src/main.cpp:237:47: error: 'PIN_FUNCTION_P14' was not declared in this scope 
	libretuya_arduinointernalgpiopin_2->set_pin(PIN_FUNCTION_P14); 
							^ 
src/main.cpp:276:47: error: 'PIN_FUNCTION_P13' was not declared in this scope
	libretuya_arduinointernalgpiopin_3->set_pin(PIN_FUNCTION_P13); 
							^ 
src/main.cpp:351:47: error: 'PIN_FUNCTION_P12' was not declared in this scope 	libretuya_arduinointernalgpiopin_4->set_pin(PIN_FUNCTION_P12); 
							^ 
src/main.cpp:356:47: error: 'PIN_FUNCTION_P04' was not declared in this scope 	libretuya_arduinointernalgpiopin_5->set_pin(PIN_FUNCTION_P04); 
							^ 
src/main.cpp:361:47: error: 'PIN_FUNCTION_P05' was not declared in this scope 	libretuya_arduinointernalgpiopin_6->set_pin(PIN_FUNCTION_P05); 
							^ 
*** [/data/pc191ha-109/.pioenvs/pc191ha-109/src/main.cpp.o] Error 1 
========================= [FAILED] Took 18.15 seconds ========================= 

One of the documentation pages (can’t remember which one) says that ESPHome has a table of GPIO to actual physical pin numbers - maybe this hasn’t been setup in libreTuya-esphome for the new modules ?

Any other suggestions please.

Read the function column of the pinout tables here:

Thanks Tom, I know you’re trying to be helpful …

I am no electronics engineer or unix GOD. Just trying to go from Blakadders table of GPIO01 = Button 1, GPIO13 = Relay 1, GPIO14 = Led 1 to an ESPHome yaml configuration.

Telling me that PWM2 (whatever that is) in Pin 2 is connected to P8 pin is actually more confusing.

Interface pin definition

Pin No.  Symbol  I/O type  Function
1         VBAT   P         Power supply pin (3.3 V), which is connected to the VBAT pin on the internal IC
2         PWM2   I/O       Common GPIO, which is connected to the P8 pin on the internal IC

Is there any tutorial showing how to connect up these dots ?

There is your answer.

Did you get anywhere with this @donburch888 ?
Flashed mine using tuya cloud cutter and epshome bin file. Got the button, relay and led running but get no reading from the power monitoring chip

Yes, I got a couple of PC191HA’s working under libretuya-esphome in HA and node-red. Button, relay, and LED were pretty straight-forward. Trying things copied from other configurations I get values for power consumption and even Wifi signal strength - but the power consumption values are NOWHERE NEAR accurate.

I do want a meaningful amperage figure returned so I can detect when the washing machine has finished, or my phone has finished charging … so I tried adjusting the voltage_divider and current_resistor values to get them into a reasonable-looking range … but found that the OTA firmware update (from the HA libretuya-esphome/dashboard) said the firmware was uploaded and was OK - but the changes didn’t take. I even trimmed out all the energy reporting (back to bare minimum), compiled and uploaded firmware … and still got all the energy readings. Help on discord totally put me off the whole project :frowning:

A few weeks later I tried plugging in my device again, and strangely after being powered off so long, the last firmware update seemed to have taken. I have successfully done a firmware update on both my test units, but am hesitant to fiddle more if it takes so long for the devices to reset and take the firmware changes. My other 3 units are still using localTuya, though they stopped reporting values for power consumption some time back.

FWIW, this is my configuration. Note that I have 5 of these devices, with the last octet of IP address at the end of device name, so the substitutions allow me to copy the configuration for multiple units and make only one change at the top.

substitutions:
  deviceIP: "109"
  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: !secret esphome_api_encryption

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 configuration 
    # 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 
    # so no point exposing these to HA - use internal: true option

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

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

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

# Example configuration entry for device BL0937 using inverted SEL pin functionality
sensor:
  - platform: wifi_signal
    name: $devicename WiFi Signal
    id:   ${deviceID}_wifi_signal
    update_interval: 50s

   # PC191HA includes a BL0937 chip for measuring power consumption
   #          and BL0937 is a variation of hlw8012
  - 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: 10s
    change_mode_every: 4
    voltage_divider: 720  # adjust it according to the actual resistor values on board
                              # LOWER VALUE GIVES LOWER VOLTAGE
    current_resistor: 0.107   # HIGHER VALUE GIVES LOWER WATTAGE
    #current_resistor: 0.11    # HIGHER VALUE GIVES LOWER WATTAGE

    current:
      name: $devicename Current
      id:   ${deviceID}_current
      unit_of_measurement: A
      accuracy_decimals: 5
    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: 5

Thank you!
Glad that it worked for you in the end
Unfortunately my plug did not like these pin values and was not reporting voltage or current
I also worked out how to make the firmware to “update” - after OTA unplug it, hold the button for few seconds (probably drains the caps) and while holding plug it back in.

In the end I flashed OpenBk on it - Tasmota like firmware that sends via MQTT. If you want to try it, you will need to convert from bin/rbl to uf2 for OTA update

Are you using the Arlec PC191HA power plug with Energy Monitoring ? I started down this track after seeing the teardown https://www.elektroda.com/rtvforum/topic3944452.html by jessemclachlan.

Different models - or a newer version with the same model number - might use a different module with different pin numbers … but I guess that would give other issues.

I suggest you compare our configurations closely. There were several tips I came across in various places (such as inverting the BL0937 pins) which I incorporated.

It’s the same model with monitoring. I tried various pins and energy monitoring just did not work.
Changed firmware to Openbk and it just worked with the same pins that would not work with esphome.
Anyway, I find it nicer to have via mqtt in case I need to change things etc

I am glad that OpenBK is working for you - great that there are alternatives !

I am however still puzzled as to why the same configuration didn’t work on the same hardware for you. I wonder if it could be libreTuya and libretuya-esphome ? I found it rather confusing that they look identical, and so I was careful to try to completely remove ESPHome before installing libretuya-esphome.

Personally I am hopeful that ESPHome project will accept the newer Tuya ESP-based modules, but so far they seem very reluctant. I know they’re “by engineers for engineers” - but it would be nice for the rest of us to be able to use off-the-shelf components rather than having to custom build devices.

I did it on a completely different computer using the latest docker image and all. Anyway, flashed the rest of the devices just as easily.
Had an issue with Grid 4ch powerboard. Tried everything apart from soldering. But Cloud Cutters guys mentioned FAQ and one of the steps was to check its MAC. I must have an older version of the board as it turned put to be an ESP chip, so was able to flash it with Tasmota via OTA

Thank you for telling us about the cf and cf1 pins being inverted, been pulling my hair about it!
fyi the plugs i used are the BSD-33 with BK7231T bl0937, for doing the cloudcutting i used version 1.3.5 in cloudcutter, as there’s no profile for it and selecting tuya generic nas-wr07w as the device