logo elektroda
logo elektroda
X
logo elektroda

Extracting Historical Data from TuyaMCU CT-Clamp Power Meters: PJ2101A Setup Guide

meszarosgabor 1107 3
ADVERTISEMENT
  • #1 20992694
    meszarosgabor
    Level 2  
    Hi,

    I'm working on a PJ2101A bi-directional power meter that has TuyaMCU (similar to how TAC2121C works).
    From the IoT Cloud Tuya webpage I collected the dpId mappings and created this autoexec.bat:
    
    // start driver
    startDriver NTP
    startDriver TuyaMCU
    tuyaMcu_setBaudRate 9600
    
    // Clear all previous definitions
    clearIO
    
    tuyaMcu_defWiFiState 4
    
    PowerSave 1
    
    // Used Tuya debug portal to get mappings
    // https://www.elektroda.com/rtvforum/topic4021129.html
    // Extracting DpIDs for TUYA MCU devices 
    // https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/channelTypes.md
    
    // Total energy - Dpid 1 "forward_energy_total" -> channel 1 type=value unit=kWh RW
    linkTuyaMCUOutputToChannel 1 val 1
    setChannelType 1 EnergyTotal_kWh_div100
    setChannelLabel 1 "Forward Energy Total"
    
    // Total energy - Dpid 2 "reverse_energy_total" -> channel 2 type=value unit=kWh RW
    linkTuyaMCUOutputToChannel 2 val 2 
    setChannelType 2 EnergyTotal_kWh_div100
    setChannelLabel 2 "Reverse Energy Total"
    
    // Total energy - Dpid 3 "month_energy" -> channel 3 type=raw RW
    linkTuyaMCUOutputToChannel 3 val 3
    setChannelType 3 EnergyTotal_kWh_div100
    setChannelLabel 3 "Monthly Electricity Consumption"
    
    // Total energy - Dpid 4 "daily_energy" -> channel 4 type=raw RW
    linkTuyaMCUOutputToChannel 4 val 4
    setChannelType 4 EnergyTotal_kWh_div100
    setChannelLabel 4 "Daily power consumption"
    
    // Measurements - Dpid 6 "phase_a" - channels RAW_TAC2121C_VCP -> 5,6,7 RW
    // TAC2121C VoltageCurrentPower Packet
    // TODO: check order
    // This will automatically set voltage, power and current
    linkTuyaMCUOutputToChannel 6 RAW_TAC2121C_VCP
    setChannelType 5 Voltage_div10
    setChannelLabel 5 "Voltage"
    setChannelType 6 Power
    setChannelLabel 6 "Power"
    setChannelType 7 Current_div1000
    setChannelLabel 7 "Current"
    
    // Total energy - Dpid 101 "power" -> channel 101 type=value unit="" RO
    linkTuyaMCUOutputToChannel 101 val 8
    setChannelType 8 Power
    setChannelLabel 8 "Power8"
    
    // Total energy - Dpid 102 "current_direction" -> channel 102 type=enum RO
    linkTuyaMCUOutputToChannel 102 bool 9
    setChannelType 9 Default
    setChannelLabel 9 "Current Direction"
    


    It works mostly, though I'm struggling to query the historical data - monthly and daily electricity consumption (dpId 3 and 4).

    From the Tuya IoT device debugging details I collected the following descriptions:
    
            {
              "abilityId": 3,
              "accessMode": "rw",
              "code": "month_energy",
              "description": "1. Monthly electricity consumption data
    2. Big endian mode, HEX format
    3. Unit accuracy 0.01 KWH
    4. Message format (data field content)
    1) Issue query: starting year (1 byte) + starting month (1 byte) + ending year (1 byte) + ending month (1 byte)
    For example: 14 01 14 02 means that the panel queries the electricity consumption for a total of two months from January 2020 to February 2020.
    2) Report reply: Starting year (1) + Starting month (1) + Ending year (1) + Ending month (1) + Electricity (4 bytes in total, 6 integers + 2 decimals)
    For example: 14 01 14 02 00 00 4E 2F, which means that from January 2020 to February 2020, a total of 200.15kWh was used.
    5. Communication logic
        Monthly freezing processing logic: The user enters the start and end dates on the panel, and then the panel issues a query. The MCU responds after receiving it. For specific messages, see the format above.
        The prerequisite for this function is that the MCU has RTC or timing synchronization function with the module, and can save at least 12 months of bill data.",
              "extensions": {
                "attribute": "1152"
              },
              "name": "月用电量数据",
              "typeSpec": {
                "maxlen": 128,
                "type": "raw"
              }
            },
            {
              "abilityId": 4,
              "accessMode": "rw",
              "code": "daily_energy",
              "description": "1. Daily power consumption data\n2, big-endian mode, HEX format\n3, unit precision 0.01 KWH\n4, message format\n1) Issue query: starting month (1 byte) + starting day (1 Byte) + end month (1 byte) + end day (1 byte)\nExample: 08 0A 08 0F means that the panel queries the electricity consumption for a total of 6 days from August 10 to August 15. \n2) Report reply: from
    Starting month (1 byte) + starting day (1 byte) + ending month (1 byte) + ending day (1 byte) + battery level (4 bytes in total, 6 digits integer + 2 decimal digits)\n For example: 08 0A 08 0F 14 44, which means that from August 10th to August 15th when the panel was queried, the MCU responded that a total of 51.88kWh was used. \n5. Communication logic\n Day freeze processing logic: The user inputs the start and end dates on the panel, and then the panel issues a query, and the MCU replies after receiving it. \n The prerequisite for this function is that the MCU has RTC or timing synchronization function with the module, and can save at least the last 30 days of daily bill data.",
              "extensions": {
                "attribute": "1152"
              },
              "name": "日用电量数据",
              "typeSpec": {
                "maxlen": 128,
                "type": "raw"
              }
            },
    


    My question: how can I send query to the TuyaMCU, let's say to gather yesterday's data?
    Something like
    tuyaMcu_sendState 4 0 03050305
    could work, but instead it sets the current daily consumption to EnergyTotal 30103.070kWh (Daily power consumption)

    Is there a way to send queryState instead of sendState?

    Thanks
  • ADVERTISEMENT
  • #2 21005251
    p.kaczmarek2
    Moderator Smart Home
    Of course you can send query state with OBK, have you checked our docs?
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md
    The command:
    
    tuyaMcu_sendQueryState
    

    will send a query for all dpIDs and futher behaviour is dependent on the MCU.

    You can't request for a specific dpID, you can just request a full update. This is determined by TuyaMCU protocol.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #3 21007453
    meszarosgabor
    Level 2  

    In the tuya UI I could query for specific time periods - ie. Jan1 to Jan31. That is sent over to the MCU, as it stores the last 30 days daily and 12 month monthly data, see the extracted json description from tuya iot site (description translated).
    This would need to send the queryState with parameters.

    Quote:

    {
    "abilityId": 3,
    "accessMode": "rw",
    "code": "month_energy",
    "description": "1. Monthly electricity consumption data
    2. Big endian mode, HEX format
    3. Unit accuracy 0.01 KWH
    4. Message format (data field content)
    1) Issue query: starting year (1 byte) + starting month (1 byte) + ending year (1 byte) + ending month (1 byte)
    For example: 14 01 14 02 means that the panel queries the electricity consumption for a total of two months from January 2020 to February 2020.
    2) Report reply: Starting year (1) + Starting month (1) + Ending year (1) + Ending month (1) + Electricity (4 bytes in total, 6 integers + 2 decimals)
    For example: 14 01 14 02 00 00 4E 2F, which means that from January 2020 to February 2020, a total of 200.15kWh was used.
    5. Communication logic
    Monthly freezing processing logic: The user enters the start and end dates on the panel, and then the panel issues a query. The MCU responds after receiving it. For specific messages, see the format above.
    The prerequisite for this function is that the MCU has RTC or timing synchronization function with the module, and can save at least 12 months of bill data.",
    "extensions": {
    "attribute": "1152"
    },
    "name": "月用电量数据",
    "typeSpec": {
    "maxlen": 128,
    "type": "raw"
    }
    },
    {
    "abilityId": 4,
    "accessMode": "rw",
    "code": "daily_energy",
    "description": "1. Daily power consumption datan2, big-endian mode, HEX formatn3, unit precision 0.01 KWHn4, message formatn1) Issue query: starting month (1 byte) + starting day (1 Byte) + end month (1 byte) + end day (1 byte)nExample: 08 0A 08 0F means that the panel queries the electricity consumption for a total of 6 days from August 10 to August 15. n2) Report reply: from
    Starting month (1 byte) + starting day (1 byte) + ending month (1 byte) + ending day (1 byte) + battery level (4 bytes in total, 6 digits integer + 2 decimal digits)n For example: 08 0A 08 0F 14 44, which means that from August 10th to August 15th when the panel was queried, the MCU responded that a total of 51.88kWh was used. n5. Communication logicn Day freeze processing logic: The user inputs the start and end dates on the panel, and then the panel issues a query, and the MCU replies after receiving it. n The prerequisite for this function is that the MCU has RTC or timing synchronization function with the module, and can save at least the last 30 days of daily bill data.",
    "extensions": {
    "attribute": "1152"
    },
    "name": "日用电量数据",
    "typeSpec": {
    "maxlen": 128,
    "type": "raw"
    }
    },

  • #4 21007468
    p.kaczmarek2
    Moderator Smart Home
    This looks very interesting, but are we talking about the same query state packet?

    Do you know which Tuya message type is used for the mechanism you've described above?

    Are you able to provide full details of this protocol feature or maybe an UART capture from unflashed device?

    Maybe you can use tuyaMcu_sendCmd command:
    https://github.com/search?q=repo%3Aopenshwpro...2FOpenBK7231T_App%20tuyaMcu_sendCmd&type=code
    It is a new command that is not yet fully documented, but it basically takes two arguments:
    
    tuyaMcu_sendCmd 0x30 000000
    

    First is message type and second is payload. The length is calculated, same goes for checksum. This allows you to send any TuyaMCU message easily without having to calculate the checksum yourself.
    Helpful post? Buy me a coffee.
ADVERTISEMENT