-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tuya smart plug keeps reseting on multiple queries #30
Comments
this is the JSON response of the script: $ python3 plugjson.py 7033XXXXXXXXXXXXXXXX 10.0.XX.XXX { "datetime": "2023-07-27T11:52:55Z", "switch": "{'1': True, '2': True}", "power": "61.0", "current": "570.0", "voltage": "244.3", "error": "OK" } and for clearance, this is the script I was doing , nothing fancy, just an exporter : import json
import subprocess
from flask import Flask, Response
from prometheus_client import Gauge, generate_latest, REGISTRY
app = Flask(__name__)
# Create Prometheus metrics
power_gauge = Gauge('power', 'Current power consumption', ['type'])
current_gauge = Gauge('current', 'Current value', ['type'])
voltage_gauge = Gauge('voltage', 'Current voltage', ['type'])
# Function to parse the JSON response and extract relevant data
def parse_json_response(json_str):
parsed_response = json.loads(json_str)
power_value = float(parsed_response["power"])
current_value = float(parsed_response["current"])
voltage_value = float(parsed_response["voltage"])
return power_value, current_value, voltage_value
# Route to expose Prometheus metrics
@app.route('/metrics')
def metrics():
# Read the output of your Python script
try:
responsejson = subprocess.check_output(["python3", "plugjson.py", "7033XXXXXXXXXXXXXXXX", "10.0.XX.XXX"]).decode("utf-8")
except subprocess.CalledProcessError as e:
return f"Error executing script: {e}"
power, current, voltage = parse_json_response(responsejson)
# Set metrics values
power_gauge.labels(type="current_power").set(power)
current_gauge.labels(type="current_value").set(current)
voltage_gauge.labels(type="current_voltage").set(voltage)
return Response(generate_latest(REGISTRY), content_type='text/plain')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9442) |
I tested the plugjson.py script only and it seems that something is triggering the plug to reset. $ while true; do python3 plugjson.py; date; sleep 5; done
{ "datetime": "2023-07-27T13:14:28Z", "switch": "{'1': True, '2': True}", "power": "65.0", "current": "500.0", "voltage": "245.1", "error": "OK" }
Thu Jul 27 13:14:28 UTC 2023
...
...
...
{ "datetime": "2023-07-27T13:24:40Z", "switch": "{'1': True, '2': True}", "power": "57.7", "current": "429.0", "voltage": "246.3", "error": "OK" }
Thu Jul 27 13:24:40 UTC 2023 |
Hi @alexbutoi - what do you mean by "resetting"? Do you mean it seems to disappear? |
Hi Jason, thanks for your reply. I hear the relay inside the KSIX-BXWSP211 flipping from ON to OFF and back ON very fast. This model has 2 power outputs so this happens to both of the relay switches. |
Since I've moved the server out of the smartplug, I can continue to monitor. Can I activate some debug for a more verbose logs?
|
You can activate tinytuya debugging by adding this to the import tinytuya
tinytuya.set_debug(True) You could also create a tinytuya monitor loop to run to see what the swtich is emitting when this occurs: import tinytuya
d = tinytuya.OutletDevice('DEVICEID', 'DEVICEIP', 'DEVICEKEY')
d.set_version(3.3)
d.set_socketPersistent(True)
print(" > Send Request for Status < ")
payload = d.generate_payload(tinytuya.DP_QUERY)
d.send(payload)
print(" > Begin Monitor Loop <")
while(True):
# See if any data is available
data = d.receive()
print('Received Payload: %r' % data)
# Send keyalive heartbeat
print(" > Send Heartbeat Ping < ")
payload = d.generate_payload(tinytuya.HEART_BEAT)
d.send(payload) |
Hi,
I have created my own Prometheus exporter with the help of your plugjson.py ( big thanks by the way) and I pull metrics every 5 seconds from the plug .
.....
.....
responsejson = subprocess.check_output(["python3", "plugjson.py", "7033XXXXXXXXXXXXXXXX", "10.0.XX.XXX"]).decode("utf-8")
.....
.....
Everything works fine, Prometheus is reading the metrics but the plug keeps resetting.
The text was updated successfully, but these errors were encountered: