# GET Tuya cloud (power day)

**URL:** <https://discourse.nodered.org/t/get-tuya-cloud-power-day/68898>\
**Category:** General\
**Created:** [12 October 2022 06:07 UTC](https://discourse.nodered.org/t/get-tuya-cloud-power-day/68898 "2022-10-12T06:07:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wiwan-wijoyo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wiwan-wijoyo/32/48198_2.png) [@wiwan-wijoyo](https://discourse.nodered.org/u/wiwan-wijoyo)\
**Post date:** [12 October 2022 06:07 UTC](https://discourse.nodered.org/t/get-tuya-cloud-power-day/68898/1 "2022-10-12T06:07:39Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8b5ef860904222fb940833cce2f7558d30c4aed8.png)

problematic results (power day):  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f17d477b46bf178072db01f193ca3db9ae6283e0.png)

Below this my node red.

```auto
[
    {
        "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

---

<div class="post-metadata">

**Author:** ![wiwan-wijoyo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wiwan-wijoyo/32/48198_2.png) [@wiwan-wijoyo](https://discourse.nodered.org/u/wiwan-wijoyo)\
**Post date:** [14 October 2022 07:56 UTC](https://discourse.nodered.org/t/get-tuya-cloud-power-day/68898/2 "2022-10-14T07:56:20Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bb85b6bec6c0f1f4c949f2fccb25ddc046a9bcc4.png)

[postman\_collection.json](https://discourse.nodered.org/uploads/short-url/3UeAjFTZVoJ4XTwCSEr0R2IrPgx.json) (43.0 KB)  
[postman\_environment.json](https://discourse.nodered.org/uploads/short-url/1KECDwO4HP84RKJ3qr6jEKD41aP.json) (581 Bytes)

---

<div class="post-metadata">

**Author:** ![wiwan-wijoyo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wiwan-wijoyo/32/48198_2.png) [@wiwan-wijoyo](https://discourse.nodered.org/u/wiwan-wijoyo)\
**Post date:** [15 October 2022 00:40 UTC](https://discourse.nodered.org/t/get-tuya-cloud-power-day/68898/3 "2022-10-15T00:40:17Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a938032897a6ace163d41cf9cde568a002bdbdd7.png)

result of the day using **map** type  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9cde263e45af30a90226459580da5bb357996f90.png)

docs from tuya: [Device Data Statistic-Cloud Services API Reference-Tuya Developer](https://developer.tuya.com/en/docs/cloud/device-data-statistic?id=Ka7g7nvnad1rm)

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

thanks

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 October 2022 06:50 UTC](https://discourse.nodered.org/t/get-tuya-cloud-power-day/68898/4 "2022-10-15T06:50:55Z")

</div>

> [@wiwan-wijoyo](#):
>
> problematic results (power day):  
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f17d477b46bf178072db01f193ca3db9ae6283e0.png)

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](https://developer.tuya.com/en/docs/iot/open-api/api-reference/singnature?id=Ka43a5mtx1gsc)

> [@wiwan-wijoyo](#):
>
> is there a way like convert from string to map or something else

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 December 2022 06:51 UTC](https://discourse.nodered.org/t/get-tuya-cloud-power-day/68898/5 "2022-12-14T06:51:33Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
