-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermostat.be
149 lines (121 loc) · 4.62 KB
/
thermostat.be
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import string
import persist
import webserver
import json
var export = module('thermostat')
def load_file(fn)
var obj, f
f = open(tasmota.wd .. fn, 'r')
obj = f.read()
f.close()
return obj
end
class Thermostat : Driver
static var _heater = 0 # tasmota's relay number to which heater is connected
static var _msg_no_temperature = "temperature source is lost"
var _current_temp, _target_temp, _hysteresis # in celsius
var _control_period # in minutes
var _running
def init()
self._current_temp = 9000.0
self._target_temp = number(persist.member('target_temp'))
if !self._target_temp
self._target_temp = 22.5
end
self._hysteresis = number(persist.member('hysteresis'))
if !self._hysteresis
self._hysteresis = 0.5
end
self._control_period = number(persist.member('control_period'))
if !self._control_period
self._control_period = 5 # 5 min
end
self._running = false
tasmota.add_rule("event#knxrx_val1", /value -> self.current_temperature_knx_handler(value))
tasmota.add_rule("rules#timer=1", /-> self.rule_timer1_timeout_handler())
#tasmota.add_rule("power1#state", /value -> tasmota.cmd(string.format("Power2 %s", str(value))))
end
def start()
tasmota.cmd('RuleTimer1 60', true)
tasmota.set_timer(0, /-> self.control())
end
def current_temperature_knx_handler(value)
self._running = true
self._current_temp = number(value)
tasmota.cmd('RuleTimer1 60', true)
end
def rule_timer1_timeout_handler()
self._running = false
tasmota.set_power(self._heater, true)
tasmota.log(self._msg_no_temperature)
end
def control()
# bang-bang
tasmota.set_timer(self._control_period * 60 * 1000, /-> self.control())
if self._running
if self._current_temp > self._target_temp + 0.5 * self._hysteresis
tasmota.set_power(self._heater, false)
elif self._current_temp < self._target_temp - 0.5 * self._hysteresis
tasmota.set_power(self._heater, true)
end
end
end
def json_append()
tasmota.response_append(
string.format(
",\"current_temperature\": %.1f"..
",\"target_temperature\": %.1f"..
",\"hysteresis\": %.1f"..
",\"control_period\": %d",
self._current_temp, self._target_temp, self._hysteresis, self._control_period))
end
def web_add_main_button()
webserver.content_send("<form method=\"get\" action=\"cts\"><button>Configure Thermostat</button></form>")
end
def web_add_handler()
webserver.on('/cts', /-> self.configure_thermostat_http_get(), webserver.HTTP_GET)
webserver.on('/cts', /-> self.configure_thermostat_http_post(), webserver.HTTP_POST)
end
def web_sensor()
if !self._running
tasmota.web_send("Thermostat is not running")
return
end
tasmota.web_send_decimal(
string.format(
"{s}Current temperature: {m}%.1f °C{e}"..
"{s}Target temperature: {m}%.1f °C{e}"..
"{s}Hysteresis: {m}%.1f °C{e}"..
"{s}Control period: {m}%d min{e}",
self._current_temp, self._target_temp, self._hysteresis, self._control_period))
end
def configure_thermostat_http_get()
webserver.content_start('Configure Thermostat')
webserver.content_send_style()
var content = load_file('configure_template.html')
webserver.content_send(string.format(content, self._target_temp, self._hysteresis, self._control_period))
webserver.content_button(webserver.BUTTON_MAIN)
webserver.content_stop()
end
def configure_thermostat_http_post()
if webserver.has_arg('target_temp')
var value = webserver.arg('target_temp')
persist.setmember('target_temp', value)
self._target_temp = number(value)
end
if webserver.has_arg('hysteresis')
var value = webserver.arg('hysteresis')
persist.setmember('hysteresis', value)
self._hysteresis = number(value)
end
if webserver.has_arg('control_period')
var value = webserver.arg('control_period')
persist.setmember('control_period', value)
self._control_period = number(value)
end
persist.save()
webserver.redirect("/")
end
end
export.Thermostat = Thermostat
return export