GET Tuya cloud (power day)

Hi,

Can someone help me.. I have node red integration with tuya cloud API and I need to check my smart plug consumption per day. I try to send command total power working well but if I send command power day the result sign invalid. I try send command power day from postman working.

the result working (total power):
image

problematic results (power day):
image

Below this my node red.

[
    {
        "id": "a6c422e0c1f1059f",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "30e07ae83d05587c",
        "type": "function",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "func": "var url = \"https://openapi.tuyaus.com/v1.0/token?grant_type=1\";\nvar t = msg.time;\nvar sign = msg.payload;\nvar client_id  = flow.get(\"tuya_client_id\");\nmsg.headers ={\n \"sign_method\": \"HMAC-SHA256\",\n \"client_id\" : client_id,\n \"t\": t.toString(),\n \"sign\": sign.toUpperCase(),\n },\n msg.payload = '';\n msg.url = url;\n msg.method = \"GET\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 560,
        "y": 100,
        "wires": [
            [
                "70c77b5c60b93b02"
            ]
        ]
    },
    {
        "id": "70c77b5c60b93b02",
        "type": "http request",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "x": 730,
        "y": 100,
        "wires": [
            [
                "1d39ccf2e051c5bd"
            ]
        ]
    },
    {
        "id": "486b1e5360769c46",
        "type": "inject",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "props": [
            {
                "p": "time",
                "v": "",
                "vt": "date"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 90,
        "y": 100,
        "wires": [
            [
                "86ca345a20fb2c40"
            ]
        ]
    },
    {
        "id": "86ca345a20fb2c40",
        "type": "function",
        "z": "a6c422e0c1f1059f",
        "name": "Create signStr",
        "func": "var client_id  = flow.get(\"tuya_client_id\");\nvar t = msg.time;\n\n\nvar method = \"GET\";\nvar sign_url = \"/v1.0/token?grant_type=1\";\n\n// Couldn't get nodered to process an empty string so this is a hash of an empty file\nvar content_hash = \"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\";\nvar string_to_sign = method+\"\\n\"+content_hash+\"\\n\"+\"\"+\"\\n\"+sign_url;\nvar signStr = client_id+t+string_to_sign;\n\nmsg.payload = signStr;\nmsg.time = msg.time;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "// Code added here will be run once\n// whenever the node is started.\nvar client_id  = \"gmkrt9qagn7f5puwmgws\";\nvar user_id = \"arcontrol.ytm3@gmail.com\";\nvar device_id = \"eb893b4451760756a9nepw\";\nflow.set(\"tuya_client_id\", client_id);\nflow.set(\"tuya_user_id\", user_id );\nflow.set(\"device_id\", device_id );",
        "finalize": "",
        "libs": [],
        "x": 260,
        "y": 100,
        "wires": [
            [
                "efeb97daa09e5d4c"
            ]
        ]
    },
    {
        "id": "efeb97daa09e5d4c",
        "type": "hmac",
        "z": "a6c422e0c1f1059f",
        "name": "Sign",
        "algorithm": "HmacSHA256",
        "key": "cffcc28cfaef4e88a415b5b2d3842ff5",
        "x": 410,
        "y": 100,
        "wires": [
            [
                "30e07ae83d05587c"
            ]
        ]
    },
    {
        "id": "edc2de9f19eaf09d",
        "type": "comment",
        "z": "a6c422e0c1f1059f",
        "name": "Get Access Token",
        "info": "",
        "x": 110,
        "y": 60,
        "wires": []
    },
    {
        "id": "117862ed28c55bd6",
        "type": "comment",
        "z": "a6c422e0c1f1059f",
        "name": "Get Total",
        "info": "",
        "x": 80,
        "y": 260,
        "wires": []
    },
    {
        "id": "1d39ccf2e051c5bd",
        "type": "function",
        "z": "a6c422e0c1f1059f",
        "name": "Extract Token from Response",
        "func": "var data = msg.payload;\nvar access = data.result.access_token;\nvar refresh = data.result.refresh_token;\n\nvar creds ={\n \"access_token\" : access,\n \"refresh_token\" : refresh\n}\nflow.set(\"tuya\", creds);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 940,
        "y": 100,
        "wires": [
            [
                "7c9e38a0ba3edfae"
            ]
        ]
    },
    {
        "id": "536be60460901270",
        "type": "inject",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "props": [
            {
                "p": "time",
                "v": "",
                "vt": "date"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 90,
        "y": 300,
        "wires": [
            [
                "7cca48367d988c0f"
            ]
        ]
    },
    {
        "id": "7cca48367d988c0f",
        "type": "function",
        "z": "a6c422e0c1f1059f",
        "name": "Create signStr",
        "func": "var creds = flow.get(\"tuya\");\nvar access_token = creds.access_token;\nvar deviceID = msg.deviceID;\nvar t = msg.time;\nvar client_id  = flow.get(\"tuya_client_id\");\nvar device_id = flow.get(\"device_id\");\nvar token = access_token;\nvar method = \"GET\";\nvar sign_url = \"/v1.0/devices/\"+device_id+\"/statistics/total?code=add_ele\";\nvar content_hash = \"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\";\nvar string_to_sign = method+\"\\n\"+content_hash+\"\\n\"+\"\"+\"\\n\"+sign_url;\nvar signStr = client_id+token+t+string_to_sign;\n\nmsg.payload = signStr;\nmsg.time = msg.time;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 240,
        "y": 300,
        "wires": [
            [
                "be44fa094e1b9cc7"
            ]
        ]
    },
    {
        "id": "be44fa094e1b9cc7",
        "type": "hmac",
        "z": "a6c422e0c1f1059f",
        "name": "Sign",
        "algorithm": "HmacSHA256",
        "key": "cffcc28cfaef4e88a415b5b2d3842ff5",
        "x": 390,
        "y": 300,
        "wires": [
            [
                "37de899f6fee6d54"
            ]
        ]
    },
    {
        "id": "37de899f6fee6d54",
        "type": "function",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "func": "var deviceID = msg.deviceID;\nvar url = \"https://openapi.tuyaus.com\";\nvar t = msg.time;\nvar method = \"GET\";\nvar client_id  = flow.get(\"tuya_client_id\");\nvar device_id = flow.get(\"device_id\");\nvar creds = flow.get(\"tuya\");\nvar token = creds.access_token;\nvar sign = msg.payload;\nmsg.headers ={\n \"sign_method\": \"HMAC-SHA256\",\n \"client_id\" : client_id,\n \"t\": t.toString(),\n \"mode\" : \"cors\",\n \"Content-Type\": \"application/json\",\n \"sign\": sign.toUpperCase(),\n \"access_token\" : token\n },\n msg.payload = '';\n msg.url = url+\"/v1.0/devices/\"+device_id+\"/statistics/total?code=add_ele\";\n msg.method = \"GET\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 520,
        "y": 300,
        "wires": [
            [
                "417fb6f2925212f9"
            ]
        ]
    },
    {
        "id": "417fb6f2925212f9",
        "type": "http request",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 670,
        "y": 300,
        "wires": [
            [
                "0ab9eb54836bb821"
            ]
        ]
    },
    {
        "id": "0ab9eb54836bb821",
        "type": "debug",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 850,
        "y": 300,
        "wires": []
    },
    {
        "id": "72cc7ea85efc352a",
        "type": "comment",
        "z": "a6c422e0c1f1059f",
        "name": "GetDay",
        "info": "",
        "x": 70,
        "y": 360,
        "wires": []
    },
    {
        "id": "2ca33bf60df7d7bd",
        "type": "comment",
        "z": "a6c422e0c1f1059f",
        "name": "GetStatus",
        "info": "",
        "x": 80,
        "y": 160,
        "wires": []
    },
    {
        "id": "7c9e38a0ba3edfae",
        "type": "debug",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1150,
        "y": 100,
        "wires": []
    },
    {
        "id": "87990bab98005173",
        "type": "inject",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "props": [
            {
                "p": "time",
                "v": "",
                "vt": "date"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 90,
        "y": 200,
        "wires": [
            [
                "2b37c286a860e920"
            ]
        ]
    },
    {
        "id": "2b37c286a860e920",
        "type": "function",
        "z": "a6c422e0c1f1059f",
        "name": "Create signStr",
        "func": "var creds = flow.get(\"tuya\");\nvar access_token = creds.access_token;\nvar deviceID = msg.deviceID;\nvar t = msg.time;\nvar client_id  = flow.get(\"tuya_client_id\");\nvar device_id = flow.get(\"device_id\");\nvar token = access_token;\nvar method = \"GET\";\nvar sign_url = \"/v1.0/devices/\"+device_id;\nvar content_hash = \"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\";\nvar string_to_sign = method+\"\\n\"+content_hash+\"\\n\"+\"\"+\"\\n\"+sign_url;\nvar signStr = client_id+token+t+string_to_sign;\n\nmsg.payload = signStr;\nmsg.time = msg.time;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 240,
        "y": 200,
        "wires": [
            [
                "6770b78a12f4a539"
            ]
        ]
    },
    {
        "id": "6770b78a12f4a539",
        "type": "hmac",
        "z": "a6c422e0c1f1059f",
        "name": "Sign",
        "algorithm": "HmacSHA256",
        "key": "cffcc28cfaef4e88a415b5b2d3842ff5",
        "x": 390,
        "y": 200,
        "wires": [
            [
                "2e64ee78353c5ddf"
            ]
        ]
    },
    {
        "id": "2e64ee78353c5ddf",
        "type": "function",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "func": "var deviceID = msg.deviceID;\nvar url = \"https://openapi.tuyaus.com\";\nvar t = msg.time;\nvar method = \"GET\";\nvar client_id  = flow.get(\"tuya_client_id\");\nvar device_id = flow.get(\"device_id\");\nvar creds = flow.get(\"tuya\");\nvar token = creds.access_token;\nvar sign = msg.payload;\nmsg.headers ={\n \"sign_method\": \"HMAC-SHA256\",\n \"client_id\" : client_id,\n \"t\": t.toString(),\n \"mode\" : \"cors\",\n \"Content-Type\": \"application/json\",\n \"sign\": sign.toUpperCase(),\n \"access_token\" : token\n },\n msg.payload = '';\n msg.url = url+\"/v1.0/devices/\"+device_id;\n msg.method = \"GET\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 520,
        "y": 200,
        "wires": [
            [
                "8c4ae4a5e2dc27e7"
            ]
        ]
    },
    {
        "id": "8c4ae4a5e2dc27e7",
        "type": "http request",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 670,
        "y": 200,
        "wires": [
            [
                "74ea0f207b97b951"
            ]
        ]
    },
    {
        "id": "74ea0f207b97b951",
        "type": "debug",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 850,
        "y": 200,
        "wires": []
    },
    {
        "id": "d41ce93a1cdae79a",
        "type": "inject",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "props": [
            {
                "p": "time",
                "v": "",
                "vt": "date"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 95,
        "y": 400,
        "wires": [
            [
                "662328987632219e"
            ]
        ]
    },
    {
        "id": "662328987632219e",
        "type": "function",
        "z": "a6c422e0c1f1059f",
        "name": "Create signStr",
        "func": "var creds = flow.get(\"tuya\");\nvar access_token = creds.access_token;\nvar deviceID = msg.deviceID;\nvar t = msg.time;\nvar client_id  = flow.get(\"tuya_client_id\");\nvar device_id = flow.get(\"device_id\");\nvar token = access_token;\nvar method = \"GET\";\nvar sign_url = \"/v1.0/devices/\"+device_id+\"/statistics/days?code=add_ele&start_day=20221011&end_day=20221011\";\nvar content_hash = \"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\";\nvar string_to_sign = method+\"\\n\"+content_hash+\"\\n\"+\"\"+\"\\n\"+sign_url;\nvar signStr = client_id+token+t+string_to_sign;\n\nmsg.payload = signStr;\nmsg.time = msg.time;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 245,
        "y": 400,
        "wires": [
            [
                "0623a62f6bfa6dab"
            ]
        ]
    },
    {
        "id": "0623a62f6bfa6dab",
        "type": "hmac",
        "z": "a6c422e0c1f1059f",
        "name": "Sign",
        "algorithm": "HmacSHA256",
        "key": "cffcc28cfaef4e88a415b5b2d3842ff5",
        "x": 395,
        "y": 400,
        "wires": [
            [
                "c4ea7a7e08833d5d"
            ]
        ]
    },
    {
        "id": "c4ea7a7e08833d5d",
        "type": "function",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "func": "var deviceID = msg.deviceID;\nvar url = \"https://openapi.tuyaus.com\";\nvar t = msg.time;\nvar method = \"GET\";\nvar client_id  = flow.get(\"tuya_client_id\");\nvar device_id = flow.get(\"device_id\");\nvar creds = flow.get(\"tuya\");\nvar token = creds.access_token;\nvar sign = msg.payload;\nmsg.headers ={\n \"sign_method\": \"HMAC-SHA256\",\n \"client_id\" : client_id,\n \"t\": t.toString(),\n \"mode\" : \"cors\",\n \"Content-Type\": \"application/json\",\n \"sign\": sign.toUpperCase(),\n \"access_token\" : token\n },\n msg.payload = '';\n msg.url = url+\"/v1.0/devices/\"+device_id+\"/statistics/days?code=add_ele&start_day=20221011&end_day=20221011\";\n msg.method = \"GET\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 525,
        "y": 400,
        "wires": [
            [
                "73d92ece690a2e23"
            ]
        ]
    },
    {
        "id": "73d92ece690a2e23",
        "type": "http request",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 675,
        "y": 400,
        "wires": [
            [
                "e684d0839b0833d3"
            ]
        ]
    },
    {
        "id": "e684d0839b0833d3",
        "type": "debug",
        "z": "a6c422e0c1f1059f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 855,
        "y": 400,
        "wires": []
    }
]

Thanks,

wiwan

additional information for "node-red-contrib-crypto-js-dynamic" from palette manager, to encrypt the data sent to Tuya Cloud and I also share the file collection&environment postman.
picture postman work to send power day.

image

postman_collection.json (43.0 KB)
postman_environment.json (581 Bytes)

I tried to find from the docs it seems the problem is in the "type", as below.

result of the total using string type

image

result of the day using map type
image

docs from tuya: Device Data Statistic-Cloud Services API Reference-Tuya Developer

is there a way like convert from string to map or something else.

thanks

Search the Web for tuya cloud api "sign invalid"

It suggests...

Please confirm whether the signature sign is correct. Refer to the document signature mechanism:
Sign Requests-Tuya IoT Development Platform-Tuya Developer

I don't think this is relevant because it is the return value. You're not even getting a return value. You're getting a signature error.