logo elektroda
logo elektroda
X
logo elektroda

Exploring the Inner Workings of PJ-MGW1103 CT-Clamp Energy Meter: Tuya, BL0942, CB2S Components

TurkeyMan 19113 134
ADVERTISEMENT
  • #121 21116078
    erdeidominik1999
    Level 8  

    >>21115933Thank you it works now, the command:
    linkTuyaMCUOutputToChannel 106 1 10 0 10
    linkTuyaMCUOutputToChannel 108 1 11 0 10
  • ADVERTISEMENT
  • #122 21117274
    snkbrzmir4
    Level 4  
    Hello, following several tutorials here on the forum, get the Dpids of the PJ-1103A and map them, but there is a problem with the direction of the power, it is signaled in another Dpid and not as a negative power directly. So do I need to transform it into negative power based on the other Dpid?

    Example:
    Power(A)=dpid(101)
    Direction(A)=dpid(102).

    200W
    dpid(101)=200
    dpid(102)=0

    -200W
    dpid(101)=200
    dpid(102)=1

    I need to map it to 200W and -200W instead of 2 separate pieces of information.

    Thank you in advance for the help.

    
    startDriver TuyaMCU
    tuyaMcu_setBaudRate 9600
    tuyaMcu_defWiFiState 4
    
    //Total energy forward
    setChannelType 1 EnergyTotal_kWh_div1000
    SetChannelLabel 1 "Total Forward Energy"
    linkTuyaMCUOutputToChannel  1 1 1
    
    //Total energy reverse
    setChannelType 2 EnergyTotal_kWh_div1000
    SetChannelLabel 2 "Total Reverse Energy"
    linkTuyaMCUOutputToChannel  2 1 2
    
    //power A
    setChannelType 3 Power_div10
    SetChannelLabel 3 "Power(A)"
    linkTuyaMCUOutputToChannel  101 1 3
    
    //Direction A
    setChannelType 4 Raw
    SetChannelLabel 4 "Direction(A)"
    linkTuyaMCUOutputToChannel  102 1 4
    
    //Direction B
    setChannelType 5 Raw
    SetChannelLabel 5 "Direction(B)"
    linkTuyaMCUOutputToChannel  104 1 5
    
    //power B
    setChannelType 6 Power_div10
    SetChannelLabel 6 "Power(B)"
    linkTuyaMCUOutputToChannel  105 1 6
    
    //forward energy A
    setChannelType 7 EnergyTotal_kWh_div1000
    SetChannelLabel 7 "Forward energy(A)"
    linkTuyaMCUOutputToChannel  106 1 7
    
    //reverse energy A
    setChannelType 8 EnergyTotal_kWh_div1000
    SetChannelLabel 8 "Reverse energy(A)"
    linkTuyaMCUOutputToChannel  107 1 8
    
    //forward energy B
    setChannelType 9 EnergyTotal_kWh_div1000
    SetChannelLabel 9 "Forward energy(B)"
    linkTuyaMCUOutputToChannel  108 1 9 
    
    //reverse energy B
    setChannelType 10 EnergyTotal_kWh_div1000
    SetChannelLabel 10 "Reverse energy(B)"
    linkTuyaMCUOutputToChannel  109 1 10
    
    //power factor A
    setChannelType 11 PowerFactor_div100
    SetChannelLabel 11 "Power factor(A)"
    linkTuyaMCUOutputToChannel  110 1 11
    
    //frequency
    setChannelType 12 Frequency_div100
    SetChannelLabel 12 "Frequency"
    linkTuyaMCUOutputToChannel  111 1 12
    
    //voltage
    setChannelType 13 Voltage_div10
    SetChannelLabel 13 "Voltage"
    linkTuyaMCUOutputToChannel  112 1 13
    
    //current A
    setChannelType 14 Current_div1000
    SetChannelLabel 14 "Current(A)"
    linkTuyaMCUOutputToChannel 113 1 14
    
    //current B
    setChannelType 15 Current_div1000
    SetChannelLabel 15 "Current(B)"
    linkTuyaMCUOutputToChannel 114 1 15
    
    //total power
    setChannelType 16 Power_div10
    SetChannelLabel 16 "Total Power"
    linkTuyaMCUOutputToChannel  115 1 16
    
    //power factor B
    setChannelType 17 PowerFactor_div100
    SetChannelLabel 17 "Power factor(B)"
    linkTuyaMCUOutputToChannel  121 1 17
    
    //reporting rate
    setChannelType 18 TextField
    SetChannelPrivate 18 true
    SetChannelLabel 18 "Reporting rate"
    linkTuyaMCUOutputToChannel  129 val 18
    
    addRepeatingEvent 10 1 SetChannel 18 3
  • ADVERTISEMENT
  • #123 21124317
    snkbrzmir4
    Level 4  
    >>21117274

    Can anyone help me with this? I looked for examples and read the documentation but I can't see a way to do it. Or at least someone give me an idea, otherwise I'll look for another way to use this adapter.
  • #124 21124319
    p.kaczmarek2
    Moderator Smart Home
    Hello, sorry, I didn't get notification before.

    So, the solution is simple:
    - you have a sign channel and a value channel
    - create a result channel
    - then create a command that sets result channel to sign * value
    - then add it as a change handler to both sign and value channels

    Added after 3 [minutes]:



    something like (sorry I am not able to check syntax at the moment):
    
    // warning! pseudocode! take care!
    // I assume that channel 12 is result
    // channel 10 is sign
    // channel 11 is unsigned val
    alias positive setChannel 12 $CH11
    alias negative setChannel 12 $CH11*-1
    alias myset if $CH10==0 then positive else negative
    // WARNING 2 - PSEUDOCODE, NOT TESTED, may have syntax errors
    // now trigger it on every change
    addEventHandler OnChannelChange 10 myset 
    addEventHandler OnChannelChange 11 myset 
    
    


    Added after 2 [minutes]:

    Let me know if it works for you @snkbrzmir4 , tomorrow I will check this in simulator
    Helpful post? Buy me a coffee.
  • #125 21125206
    snkbrzmir4
    Level 4  
    >>21124319

    When it is positive it is outputting correctly, when it turns negative (channel 4 output=1), channel 19 shows the power as -0.10W
    Below are the relevant bits, I reviewed it and I think I understood it as explained.
    Again, thanks for your time help-me

    
    //power A
    setChannelType 3 Power_div10
    SetChannelLabel 3 "Power(A)"
    linkTuyaMCUOutputToChannel  101 1 3
    
    //Direction A
    setChannelType 4 Raw
    SetChannelLabel 4 "Direction(A)"
    linkTuyaMCUOutputToChannel  102 1 4
    
    //Corrected Power
    setChannelType 19 Raw
    SetChannelLabel 19 "Corrected Power(A)"
    
    // warning! pseudocode! take care!
    // I assume that channel 12 is result
    // channel 10 is sign 4
    // channel 11 is unsigned val 3
    alias positive setChannel 19 $CH3
    alias negative setChannel 19 $CH3*-1
    alias myset if $CH4==0 then positive else negative
    // WARNING 2 - PSEUDOCODE, NOT TESTED, may have syntax errors
    // now trigger it on every change
    addEventHandler OnChannelChange 3 myset 
    addEventHandler OnChannelChange 4 myset 
    
    
  • ADVERTISEMENT
  • #126 21125287
    p.kaczmarek2
    Moderator Smart Home
    I see, I've found one little issue.

    Please update OBK to 1.17.619 and retry. It should work better then.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #127 21125327
    snkbrzmir4
    Level 4  
    >>21125287

    Thank you very much, working 100% now
  • #128 21125353
    p.kaczmarek2
    Moderator Smart Home
    That's good to hear. Now, it seems you have PJ-1103A working, right? Can you share a complete config and some photos?
    Helpful post? Buy me a coffee.
  • #129 21126158
    snkbrzmir4
    Level 4  
    >>21125353>>21125353>>21125353

    I sent the last config above, I only changed the last one you asked for. But I'll put it in the 2 phases and pass, I only added it to one to validate.
  • #130 21140392
    p.kaczmarek2
    Moderator Smart Home
    Ok, in a moment I will submit your config to our autoexec.bat examples, here:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    So other users can learn how to combine two dpIDs into one value (sign and unsigned number).
    Helpful post? Buy me a coffee.
  • #131 21156132
    rohanb
    Level 2  
    >>21042266 Is there a way with this 2 clamps device to calculate and save the forward and reverse energy as I cannot seem to make it work. Thanks
  • #132 21429711
    randomalias324
    Level 8  
    Have been having a lot of success with the 2 channel versions of these, and thought I'd post my configuration.
    Model: PJ-1103A
    Range: 0.2-80A
    Manufacturer: Dongguan Pinjia Technology CO.,LTD.
    Label on the box of PJ-1103A energy monitor by Dongguan Pinjia Technology CO, LTD.

    1st point is, I had some difficulty resetting the WiFi password once WiFi had been configured to a previous SSID. When booted into safe mode, to get Hopspot / wireless access point mode active, the MCU was resetting the device. As the TuyaMCU driver was not loaded during safe mode, the best way I found to stop this behaviour was to de-solder the reset / CEN pin, which is quite easy on these units. After this modification, I have removed tuyaMcu_defWiFiState 4 from my autoexec because the MCU also drives an LED that flashes based on state, which is handy feedback.
    PJ-1103A circuit board with marked CEN pin.

    I have been using one of these modules for about 12 months without the need to know the current direction. I may need that information soon, so I have updated my autoexec.bat. I have implemented current and power channels that go negative when the current direction is reversed. Note the polarity of the clamp wires if you want the current direction to make sense with respect to the arrow on the clamp. The intended polarity is not marked on the device or in the instruction manual.
    I didn't know you could set the update rate! Thats there too.
    startDriver TuyaMCU
    
    //Total energy forward
    setChannelType 1 EnergyTotal_kWh_div1000
    SetChannelLabel 1 "Total Forward Energy"
    linkTuyaMCUOutputToChannel  1 1 1
    
    //Total energy reverse
    setChannelType 2 EnergyTotal_kWh_div1000
    SetChannelLabel 2 "Total Reverse Energy"
    linkTuyaMCUOutputToChannel  2 1 2
    
    //forward energy A
    setChannelType 3 EnergyTotal_kWh_div1000
    SetChannelLabel 3 "Forward energy(A)"
    linkTuyaMCUOutputToChannel  106 1 3
    
    //reverse energy A
    setChannelType 4 EnergyTotal_kWh_div1000
    SetChannelLabel 4 "Reverse energy(A)"
    linkTuyaMCUOutputToChannel  107 1 4
    
    //forward energy B
    setChannelType 5 EnergyTotal_kWh_div1000
    SetChannelLabel 5 "Forward energy(B)"
    linkTuyaMCUOutputToChannel  108 1 5 
    
    //reverse energy B
    setChannelType 6 EnergyTotal_kWh_div1000
    SetChannelLabel 6 "Reverse energy(B)"
    linkTuyaMCUOutputToChannel  109 1 6
    
    //voltage
    setChannelType 7 Voltage_div10
    SetChannelLabel 7 "Voltage"
    linkTuyaMCUOutputToChannel  112 1 7
    
    //frequency
    setChannelType 8 Frequency_div100
    SetChannelLabel 8 "Frequency"
    linkTuyaMCUOutputToChannel  111 1 8
    
    //total power
    setChannelType 9 Power_div10
    SetChannelLabel 9 "Total Power"
    linkTuyaMCUOutputToChannel  115 1 9
    
    //current A
    setChannelType 16 Current_div1000
    SetChannelLabel 16 "Current Raw(A)"
    linkTuyaMCUOutputToChannel 113 1 16
    setChannelType 10 Current_div1000
    SetChannelLabel 10 "Current(A)"
    addEventHandler OnChannelChange 16 SetChannel 10 $CH16-(2*$CH14*$CH16)
    
    //current B
    setChannelType 17 Current_div1000
    SetChannelLabel 17 "Current Raw(B)"
    linkTuyaMCUOutputToChannel 114 1 17
    setChannelType 11 Current_div1000
    SetChannelLabel 11 "Current(B)"
    addEventHandler OnChannelChange 17 SetChannel 11 $CH17-(2*$CH15*$CH17)
    
    //power A
    setChannelType 18 Power_div10
    SetChannelLabel 18 "Power Raw(A)"
    linkTuyaMCUOutputToChannel  101 1 18
    setChannelType 12 Power_div10
    SetChannelLabel 12 "Power(A)"
    addEventHandler OnChannelChange 18 SetChannel 12 $CH18-(2*$CH14*$CH18)
    
    //power B
    setChannelType 19 Power_div10
    SetChannelLabel 19 "Power Raw(B)"
    linkTuyaMCUOutputToChannel  105 1 19
    setChannelType 13 Power_div10
    SetChannelLabel 13 "Power(B)"
    addEventHandler OnChannelChange 19 SetChannel 13 $CH19-(2*$CH15*$CH19)
    
    //Direction A
    setChannelType 14 ReadOnly
    SetChannelLabel 14 "Direction(A)"
    linkTuyaMCUOutputToChannel  102 1 14
    
    //Direction B
    setChannelType 15 ReadOnly
    SetChannelLabel 15 "Direction(B)"
    linkTuyaMCUOutputToChannel  104 1 15
    
    //power factor A
    setChannelType 20 PowerFactor_div100
    SetChannelLabel 20 "Power factor(A)"
    linkTuyaMCUOutputToChannel  110 1 20
    
    //power factor B
    setChannelType 21 PowerFactor_div100
    SetChannelLabel 21 "Power factor(B)"
    linkTuyaMCUOutputToChannel  121 1 21
    
    //reporting rate
    setChannelType 30 TextField
    SetChannelPrivate 30 true
    SetChannelLabel 30 "Reporting rate"
    linkTuyaMCUOutputToChannel  129 val 30
    
    addRepeatingEvent 10 1 SetChannel 30 1


    The resulting UI page
    Screenshot of OpenBK7231N_2DEE1C07 user interface with energy data.

    It does not seem possible to calibrate these units, at least in the usual way. Scaling of the result could probably handled in a script, but the factory calibration does seem reasonably accurate. Tested with a kettle and verified with multimeters
    PJ-1103A measuring device with connected power wires.

    Added after 17 [minutes]:

    >>21140392
    snkbrzmir4 wrote:
    alias positive setChannel 19 $CH3
    alias negative setChannel 19 $CH3*-1
    alias myset if $CH4==0 then positive else negative
    // WARNING 2 - PSEUDOCODE, NOT TESTED, may have syntax errors
    // now trigger it on every change
    addEventHandler OnChannelChange 3 myset 
    addEventHandler OnChannelChange 4 myset


    It is quite easy and clear doing it branchless in one line, which might be a better addition to the example file
    randomalias324 wrote:
    addEventHandler OnChannelChange 16 SetChannel 10 $CH16-(2*$CH14*$CH16)

    Where $CH16 is the unsigned power figure, $CH14 is the boolean direction flag and 10 is the channel being set with a signed power figure

    Added after 10 [minutes]:

    >>21156132
    Not sure if there are script commands that can save persistent values in the Beken flash, but the total energy figures being used and displayed here are simply reading the total energy figures sent from the separate MCU. The separate MCU doesn't seem to capable of or programmed to save the figures in it's flash over a power cycle.
  • #133 21443304
    dragonmen
    Level 3  
    In case someone use esphome here's relevant part:
    
    uart:
      id: uart_bus
    # pin 7	TX1	I/O	UART_TX1, which is used for transmitting user data and corresponds to P11 of the IC. Do not pull it up. By default, the MCU serial port should be in low-level or high-impedance state.
      tx_pin: GPIO11
    # pin 5	RX1	I/O	UART_RX1, which is used for receiving user data and corresponds to P10 of the IC. Do not pull it up. By default, the MCU serial port should be in low-level or high-impedance state.
      rx_pin: GPIO10
      baud_rate: 9600
    
    tuya:
    
    sensor:
    # --- Total Energy Forward ---
      - platform: tuya
        name: "Total Forward Energy"
        id: energy_total_fwd
        sensor_datapoint: 1
        unit_of_measurement: "kWh"
        accuracy_decimals: 3
        filters:
          - multiply: 0.001
        state_class: "total_increasing"  # Marks it as a total increasing sensor for Home Assistant
        device_class: "energy"           # Marks it as an energy sensor for Home Assistant
    
    # --- Total Reverse Energy ---
      - platform: tuya
        name: "Total Reverse Energy"
        id: energy_total_rev
        sensor_datapoint: 2
        unit_of_measurement: "kWh"
        accuracy_decimals: 3
        filters:
          - multiply: 0.001
        state_class: "total_increasing"  # Marks it as a total increasing sensor for Home Assistant
        device_class: "energy"           # Marks it as an energy sensor for Home Assistant
    
      # --- Voltage ---
      - platform: tuya
        name: "Voltage"
        id: voltage
        sensor_datapoint: 112
        unit_of_measurement: "V"
        accuracy_decimals: 1
        filters:
          - multiply: 0.1
    
      # --- Frequency ---
      - platform: tuya
        name: "Frequency"
        id: frequency
        sensor_datapoint: 111
        unit_of_measurement: "Hz"
        accuracy_decimals: 2
        filters:
          - multiply: 0.01
    
      # --- Total Power ---
      - platform: tuya
        name: "Total Power"
        id: power_total
        sensor_datapoint: 115
        unit_of_measurement: "W"
        accuracy_decimals: 1
        filters:
          - multiply: 0.1
    
      # --- Current (Phase A) ---
      - platform: tuya
        name: "Current(A)"
        id: current_a
        sensor_datapoint: 113
        unit_of_measurement: "A"
        accuracy_decimals: 3
        filters:
          - multiply: 0.001
    
      # --- Current (Phase B) ---
      - platform: tuya
        name: "Current(B)"
        id: current_b
        sensor_datapoint: 114
        unit_of_measurement: "A"
        accuracy_decimals: 3
        filters:
          - multiply: 0.001
    
      # --- Power (Phase A) ---
      - platform: tuya
        name: "Power(A)"
        id: power_a
        sensor_datapoint: 101
        unit_of_measurement: "W"
        accuracy_decimals: 1
        filters:
          - multiply: 0.1
    
      # --- Power (Phase B) ---
      - platform: tuya
        name: "Power(B)"
        id: power_b
        sensor_datapoint: 105
        unit_of_measurement: "W"
        accuracy_decimals: 1
        filters:
          - multiply: 0.1
    
      # --- Power Factor (Phase A) ---
      - platform: tuya
        name: "Power Factor(A)"
        id: power_factor_a
        sensor_datapoint: 110
        accuracy_decimals: 2
        filters:
          - multiply: 0.01
    
      # --- Power Factor (Phase B) ---
      - platform: tuya
        name: "Power Factor(B)"
        id: power_factor_b
        sensor_datapoint: 121
        accuracy_decimals: 2
        filters:
          - multiply: 0.01
    
      # --- WiFi Signal Strength ---
      - platform: wifi_signal
        name: "WiFi Signal"
        update_interval: 60s
        id: wifi_strength
        unit_of_measurement: "dBm"
    
      # --- Uptime ---
      - platform: uptime
        name: "Uptime"
        id: uptime_sensor
        update_interval: 60s
    
    text_sensor:
      - platform: tuya
        name: "Reporting Rate"
        id: reporting_rate
        sensor_datapoint: 129
    
      # --- WiFi IP Address ---
      - platform: wifi_info
        ip_address:
          name: "Device IP"
    
    
  • #134 21489831
    jokapu
    Level 1  
    Hey, just wondering after reading through all info about these meters. If you finally have working measurement, how are the metered values stored/processed? All the scripts I saw just map the tuya dp to channels. Do we use the bl0942 driver in obk to get total/daily consumption as data objects?
  • #135 21501510
    dragonmen
    Level 3  
    >>21489831
    For me, I managed to make it work correctly.
    I needed to upgrade esphome to 2025.3.1 because the device got stuck with rx/tx for some odd reason (new tuya protocol incompatibility?).
    The device is added to Homeassistant and processed there.

Topic summary

The discussion revolves around the PJ-MGW1103 CT-Clamp Energy Meter, focusing on its teardown, functionality, and integration with Tuya and BL0942 components. Users share insights on the device's internal workings, including the communication protocols and data points (dpIDs) for voltage, current, and power measurements. Various configurations for the autoexec.bat file are provided to optimize the device's performance, including baud rate settings and channel mappings. Users also address issues related to data accuracy, calibration, and the need for firmware updates. The conversation highlights the importance of understanding dpIDs for effective monitoring and control of energy consumption.
Summary generated by the language model.
ADVERTISEMENT