-
Notifications
You must be signed in to change notification settings - Fork 0
/
energy_saver.py
46 lines (40 loc) · 1.33 KB
/
energy_saver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python3
###############################################
# Copyright by Road IT (www.roadit.de) 2019 #
# Author: Dennis Eisold #
# Created: 11.02.2019 #
###############################################
import os, json, time, threading
import paho.mqtt.client as mqtt
mqtt_server = '10.10.31.3'
mqtt_port = 1883
mqtt_topic = 'N/9884e3983b8a/sonoff/Esszimmer/TV/'
last_power = 1000
last_state = 1
timestamp = int(time.time())
off_power = 20
off_time = 60
def on_message(client, userdata, msg):
global last_power, last_state, timestamp
if msg.topic == mqtt_topic+"SENSOR":
json_data = json.loads(msg.payload)
if json_data['ENERGY']['Power'] < off_power:
if last_power < off_power and last_state == 1 and (int(time.time()) - timestamp) > off_time:
client.publish(mqtt_topic+'cmnd/Power1', 'off')
last_power = 1000
last_state = 0
else:
last_power = json_data['ENERGY']['Power']
elif msg.topic == mqtt_topic+"POWER":
if msg.payload == 'on':
timestamp = int(time.time())
last_state = 1
def on_subscribe(client, userdata, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
while True:
client = mqtt.Client()
client.on_subscribe = on_subscribe
client.on_message = on_message
client.connect(mqtt_server, mqtt_port, 5)
client.subscribe(mqtt_topic+'#', qos=0)
client.loop_forever()