npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tuya-cloud-to-influxdb

v0.1.0

Published

Use Tuya cloud API to push data from devices to influxdb

Downloads

5

Readme

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]https://github.com/nalajcie/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-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?