Git Product home page Git Product logo

tuya-cloud-to-influxdb's Introduction

tuya-cloud-to-influxdb

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.

This is a work-in-progress, so expect breaking changes :).

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.

Available commands

Usage: cli [options] [command]

Options:
  -V, --version                        output the version number
  -v, --verbose                        Increase verbosity, may be appied multiple times
  -h, --help                           output usage information

Commands:
  auth <user> <pass>                   Authorize the Tuya client (this will save only the session ID in the config, no password will be saved)
  list                                 list all devices
  show [options] [devName]             show current values available on the devices (optionally filter by group/device name)
  switch [options] <on|off> [devName]  switch state of the device (optionally filter by group/device name)
  stats-monthly [options] [devName]    retrieve monthly cumulative stats [works for power monitoring]
  stats-daily [options] [devName]      retrieve daily cumulative stats [works for power monitoring]
  time                                 get unix time from the tuya servers (use it to test session validity)
  config-tuya [options]                manipulate Tuya API configuration
  config-influx [options]              manipulate InfluxDB connection configuration

Basic usage

  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 repo. The below example configures the keys being used in Tuya app
./cli.js config-tuya --api-key 3fjrekuxank9eaej3gcx
./cli.js config-tuya --api-secret aq7xvqcyqcnegvew793pqjmhv77rneqc
./cli.js config-tuya --api-secret2 vay9g59g9g99qf3rtqptmc3emhkanwkx
./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"
  1. After the configuration you need to open session (authorize) with Tuya cloud. Note: username and password will not be stored in the config file - only the resulting session ID
./cli.js auth username password
  1. Verify if everything works by getting time from the Tuya Cloud:
./cli.js time
✔ getting time
{ validTime: 1800, time: 1582799524 }
  1. Get device list from the cloud
./cli.js list
✔ getting device list
group: RR       device: Pralka (devId: 462683xxxxxxxxxxxxxx groupId: 2xxxxxx)
group: RR       device: Biurko (devId: 462683xxxxxxxxxxxxxx groupId: 2xxxxxx)
  1. Get device data from the cloud
./cli.js show
✔ getting device list
✔ getting device schemas
Device: Pralka
[rw] {1} switch_1:      true
[rw] {9} countdown_1:   0 s
[rw] {17} add_ele:      0.001
[ro] {18} cur_current:  0 mA
[ro] {19} cur_power:    0 W
[ro] {20} cur_voltage:  230.2 V
[ro] {21} test_bit:     1
[ro] {22} voltage_coe:  652
[ro] {23} electric_coe: 30909
[ro] {24} power_coe:    17875
[ro] {25} electricity_coe:      1165
[ro] {26} fault:        0
Device: Biurko
[rw] {1} switch_1:      true
[rw] {9} countdown_1:   0 s
[rw] {17} add_ele:      0.019
[ro] {18} cur_current:  304 mA
[ro] {19} cur_power:    44.7 W
[ro] {20} cur_voltage:  229.6 V
[ro] {21} test_bit:     1
[ro] {22} voltage_coe:  617
[ro] {23} electric_coe: 30029
[ro] {24} power_coe:    17084
[ro] {25} electricity_coe:      1220
[ro] {26} fault:        0

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.

Advanced usage

  1. Configure influxDB:
./cli.js config-influx --measurement tuya
./cli.js config-influx --uri http://example.com/database
  1. Upload the instantenous data at a given interval:
screen
while :; do date; ./cli.js show -u; sleep $((5*60)); done
  1. Setup cron to upload daily stats (for the previous day at 2:00 am every day)
0 2 * * * /path/to/tuya-cloud-to-influxdb/cli.js stats-daily --yesterday -u

Additionaly: push manually previus stats using cli.

  1. Create nice graphs in Grafana :) TODO: image/json?

tuya-cloud-to-influxdb's People

Contributors

dependabot[bot] avatar nalajcie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

kfigiela

tuya-cloud-to-influxdb's Issues

Invalid credentials

Hi,

I created an account in TuyaSmart app.

cli.js auth fails (error returned by Tuya cloud API: { code: 'TIME_VALIDATE_FAILED', message: 'time validate failed' }).

Are API key/secrets up to date? Is there another explanation for this issue?

TypeError: Cannot read properties of undefined (reading 'type')

/home/user/tuya-cloud-to-influxdb/lib/common.js:31
    if (def.property.type === 'value') {
                     ^

TypeError: Cannot read properties of undefined (reading 'type')
    at /home/user/tuya-cloud-to-influxdb/lib/common.js:31:22
    at Array.forEach (<anonymous>)
    at Object.getDeviceMeasurement (/home/user/tuya-cloud-to-influxdb/lib/common.js:26:24)
    at /home/user/tuya-cloud-to-influxdb/cli.js:75:34
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.12.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.