|
1 | 1 | # tuya-cloud-to-influxdb |
2 | | -Using Tuya cloud API to push data from device to influxdb |
| 2 | +Using Tuya cloud API to push data from device to influxdb. As a side effect it's possible to get various device information from the tuya cloud using CLI. |
| 3 | + |
| 4 | +This is a work-in-progress, so expect breaking changes :). |
| 5 | + |
| 6 | +Note: this was only tested with branded sockets with power metering. The sole purpose was to get the metering data and push it to influxdb and visualise using grafana. |
| 7 | + |
| 8 | +## Available commands |
| 9 | +``` |
| 10 | +Usage: cli [options] [command] |
| 11 | +
|
| 12 | +Options: |
| 13 | + -V, --version output the version number |
| 14 | + -v, --verbose Increase verbosity, may be appied multiple times |
| 15 | + -h, --help output usage information |
| 16 | +
|
| 17 | +Commands: |
| 18 | + auth <user> <pass> Authorize the Tuya client (this will save only the session ID in the config, no password will be saved) |
| 19 | + list list all devices |
| 20 | + show [options] [devName] show current values available on the devices (optionally filter by group/device name) |
| 21 | + switch [options] <on|off> [devName] switch state of the device (optionally filter by group/device name) |
| 22 | + stats-monthly [options] [devName] retrieve monthly cumulative stats [works for power monitoring] |
| 23 | + stats-daily [options] [devName] retrieve daily cumulative stats [works for power monitoring] |
| 24 | + time get unix time from the tuya servers (use it to test session validity) |
| 25 | + config-tuya [options] manipulate Tuya API configuration |
| 26 | + config-influx [options] manipulate InfluxDB connection configuration |
| 27 | +``` |
| 28 | + |
| 29 | +## Basic usage |
| 30 | +1. First You need to set-up the authentication keys to be used with the CLI. The keys are per-application (eg. Tuya, SmartLife or other branded ones). On details regarding how to get them - check [tuya-sign-hacking]https://github.com/nalajcie/tuya-sign-hacking repo. The below example configures the keys being used in **Tuya** app |
| 31 | +``` |
| 32 | +./cli.js config-tuya --api-key 3fjrekuxank9eaej3gcx |
| 33 | +./cli.js config-tuya --api-secret2 vay9g59g9g99qf3rtqptmc3emhkanwkx |
| 34 | +./cli.js config-tuya --api-cert-sign "93:21:9F:C2:73:E2:20:0F:4A:DE:E5:F7:19:1D:C6:56:BA:2A:2D:7B:2F:F5:D2:4C:D5:5C:4B:61:55:00:1E:40" |
| 35 | +``` |
| 36 | +2. After the configuration you need to open session (authorize) with Tuya cloud. |
| 37 | +**Note**: username and password will not be stored in the config file - only the resulting session ID |
| 38 | + |
| 39 | +``` |
| 40 | +./cli.js auth username password |
| 41 | +``` |
| 42 | +3. Verify if everything works by getting time from the Tuya Cloud: |
| 43 | +``` |
| 44 | +./cli.js time |
| 45 | +✔ getting time |
| 46 | +{ validTime: 1800, time: 1582799524 } |
| 47 | +``` |
| 48 | +4. Get device list from the cloud |
| 49 | +``` |
| 50 | +./cli.js list |
| 51 | +✔ getting device list |
| 52 | +group: RR device: Pralka (devId: 462683xxxxxxxxxxxxxx groupId: 2xxxxxx) |
| 53 | +group: RR device: Biurko (devId: 462683xxxxxxxxxxxxxx groupId: 2xxxxxx) |
| 54 | +``` |
| 55 | +5. Get device data from the cloud |
| 56 | +``` |
| 57 | +./cli.js show |
| 58 | +✔ getting device list |
| 59 | +✔ getting device schemas |
| 60 | +Device: Pralka |
| 61 | +[rw] {1} switch_1: true |
| 62 | +[rw] {9} countdown_1: 0 s |
| 63 | +[rw] {17} add_ele: 0.001 |
| 64 | +[ro] {18} cur_current: 0 mA |
| 65 | +[ro] {19} cur_power: 0 W |
| 66 | +[ro] {20} cur_voltage: 230.2 V |
| 67 | +[ro] {21} test_bit: 1 |
| 68 | +[ro] {22} voltage_coe: 652 |
| 69 | +[ro] {23} electric_coe: 30909 |
| 70 | +[ro] {24} power_coe: 17875 |
| 71 | +[ro] {25} electricity_coe: 1165 |
| 72 | +[ro] {26} fault: 0 |
| 73 | +Device: Biurko |
| 74 | +[rw] {1} switch_1: true |
| 75 | +[rw] {9} countdown_1: 0 s |
| 76 | +[rw] {17} add_ele: 0.019 |
| 77 | +[ro] {18} cur_current: 304 mA |
| 78 | +[ro] {19} cur_power: 44.7 W |
| 79 | +[ro] {20} cur_voltage: 229.6 V |
| 80 | +[ro] {21} test_bit: 1 |
| 81 | +[ro] {22} voltage_coe: 617 |
| 82 | +[ro] {23} electric_coe: 30029 |
| 83 | +[ro] {24} power_coe: 17084 |
| 84 | +[ro] {25} electricity_coe: 1220 |
| 85 | +[ro] {26} fault: 0 |
| 86 | +``` |
| 87 | + |
| 88 | +You can use `-v` option to list all retrived values (eg. `localKey` if You want to control the devices locally). `-vv` will give You even more verbose output. |
| 89 | + |
| 90 | +### Advanced usage |
| 91 | +1. Configure influxDB: |
| 92 | +```bash |
| 93 | +./cli.js config-influx --measurement tuya |
| 94 | +./cli.js config-influx --uri http://example.com/database |
| 95 | +``` |
| 96 | +2. Upload the instantenous data at a given interval: |
| 97 | +```bash |
| 98 | +screen |
| 99 | +while :; do date; ./cli.js show -u; sleep $((5*60)); done |
| 100 | +``` |
| 101 | +3. Setup cron to upload daily stats (for the previous day at 2:00 am every day) |
| 102 | +``` |
| 103 | +0 2 * * * /path/to/tuya-cloud-to-influxdb/cli.js stats-daily --yesterday -u |
| 104 | +``` |
| 105 | +Additionaly: push manually previus stats using cli. |
| 106 | + |
| 107 | +4. Create nice graphs in Grafana :) |
| 108 | +TODO: image/json? |
0 commit comments