Sign inSign up

stormotron/tuyahelper

By stormotron

•Updated 8 months ago

TuyaHelper is a OpenSource/Python tool to collect data from Tuya sensors via local access

Image
Internet of things
0

1.6K

stormotron/tuyahelper repository overview

⁠TuyaHelper version 0.0.2

Copyright (c) Netstorm, 2025

TuyaHelper is a OpenSource/Python tool to collect data from Tuya sensors via local access (LocalTuya). This tool was developed by me when I bought a Tuya power consumption sensor (electricity meter) and found that it may not work properly if WiFi is not working well. It has built-in electricity meter, which for some reason (firmware bugs?) can show very large changes when WiFi network breaks. The question was how to integrate data from the meter itself, i.e. Current_Power readings, because WiFi quality did not allow to do it correctly in this case. This tool was written for this purpose. Of course, it is not a panacea, because most of the data will most likely be lost in the WiFi channel, but it will allow you to get as close to the real values as possible.

Environment variables:

INTERVAL - sensor polling interval in seconds;
SENSOR(NUM) - description of each sensor. The following comma-separated parameters are allowed:

  • name=(sensor name)
  • ip=(IP of sensor)
  • device_id_base64=(Device ID of the sensor encoded by BASE64);
  • local_key_base64=(Local Key of the sensor, encoded by BASE64);
  • dp_id=(DP Sensor Id);
  • protocol=(3.1|3.2|3.3|3.4)
  • coefficient=(correction factor for readings)
  • powerkwh - flag that sets the data integration to KWh, implying that the meter through coefficient provides the current instantaneous power;
  • directkwh - indicates that the counter produces KWh by itself, this flag applies additional functionality of median filtering with sliding window to eliminate spikes;
  • wifipreheat=(ICMP_INTERVAL, default 0.02 - sending ICMP packets to Tuya device to keep WiFi warm)

Example template for Docker:

version: "3.9"

services:
  service:
    image: stormotron/tuyahelper:latest
    environment:
      - "INTERVAL=5"
      - "SENSOR1=name=obj1_energy,ip=192.168.62.44,device_id_base64=SWFtIG5vdCBEZXZpY2UgSUQgOik=,local_key_base64=SWFtIG5vdCBMb2NhbCBLZXkhIFdoeSByIHUgc3RhcmluZyBhdCBpdD8=,dp_id=103,protocol=3.4,coefficient=0.1,powerkwh,wifipreheat"
      - "SENSOR2=name=obj2_energy,ip=192.168.101.12,device_id_base64=SWFtIG5vdCBEZXZpY2UgSUQgOik=,local_key_base64=SWFtIG5vdCBMb2NhbCBLZXkhIFdoeSByIHUgc3RhcmluZyBhdCBpdD8=,dp_id=103,protocol=3.4,coefficient=0.1,powerkwh"
      - "SENSOR3=name=obj1_energy_direct,ip=192.168.62.44,device_id_base64=SWFtIG5vdCBEZXZpY2UgSUQgOik=,local_key_base64=SWFtIG5vdCBMb2NhbCBLZXkhIFdoeSByIHUgc3RhcmluZyBhdCBpdD8=,dp_id=1,protocol=3.4,coefficient=0.0001,directkwh"
      - "SENSOR4=name=obj2_energy_direct,ip=192.168.101.12,device_id_base64=SWFtIG5vdCBEZXZpY2UgSUQgOik=,local_key_base64=SWFtIG5vdCBMb2NhbCBLZXkhIFdoeSByIHUgc3RhcmluZyBhdCBpdD8=,dp_id=1,protocol=3.4,coefficient=0.0001,directkwh"
      - "SENSOR5=name=obj1_energy_direct_raw,ip=192.168.62.44,device_id_base64=SWFtIG5vdCBEZXZpY2UgSUQgOik=,local_key_base64=SWFtIG5vdCBMb2NhbCBLZXkhIFdoeSByIHUgc3RhcmluZyBhdCBpdD8=,dp_id=1,protocol=3.4,coefficient=0.0001"
      - "SENSOR6=name=obj2_energy_direct_raw,ip=192.168.101.12,device_id_base64=SWFtIG5vdCBEZXZpY2UgSUQgOik=,local_key_base64=SWFtIG5vdCBMb2NhbCBLZXkhIFdoeSByIHUgc3RhcmluZyBhdCBpdD8=,dp_id=1,protocol=3.4,coefficient=0.0001"
    ports:
      - "16001:80"
    volumes:
      - /mnt/your-local-storage/powerdb:/opt/powerdb
      - /mnt/your-local-storage/sensordb:/opt/sensordb

Example JSON output:

{
  "tuya_helper": {
    "obj1_energy": {
      "success_rate_5min": 0,
      "success_rate_overall": 62.3,
      "current": 322.1,
      "kwh": 60286.2281,
      "avg_5min": 393.923,
      "avg_30min": 392.536,
      "avg_1hour": 394.458
    },
    "obj2_energy": {
      "success_rate_5min": 11.1,
      "success_rate_overall": 20.95,
      "current": 145.4,
      "kwh": 10905.6606,
      "avg_5min": 102.405,
      "avg_30min": 80.317,
      "avg_1hour": 93.473
    },
    "obj1_energy_direct": {
      "success_rate_5min": 0,
      "success_rate_overall": 33.85,
      "current": 60284.3825,
      "spikes_detected": 481,
      "last_data_at": "01.05.2025 00:16:05"
    },
    "obj2_energy_direct": {
      "success_rate_5min": 10,
      "success_rate_overall": 14.75,
      "current": 10907.7088,
      "spikes_detected": 2,
      "last_data_at": "01.05.2025 03:30:30"
    },
    "obj1_energy_direct_raw": {
      "success_rate_5min": 0,
      "success_rate_overall": 33.91,
      "current": 2365.275,
      "avg_5min": 2362.477,
      "avg_30min": 2362.47,
      "avg_1hour": 2362.479
    },
    "obj2_energy_direct_raw": {
      "success_rate_5min": 11.1,
      "success_rate_overall": 15.04,
      "current": 605.432,
      "avg_5min": 605.38,
      "avg_30min": 605.359,
      "avg_1hour": 588.988
    }
  }
}

As you can see, the program uses averaging to get more plausible results for Current and, if necessary, Kwh.

How to encode your Device ID/Local Key to BASE64:

echo -n "YOUR DATA" | base64

Tag summary

Content type

Image

Digest

sha256:78568e139…

Size

49.1 MB

Last updated

8 months ago

docker pull stormotron/tuyahelper