From 555dc048467b6f99274bd274ea998529fd1dcfa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= Date: Sun, 15 Dec 2024 21:00:34 +0200 Subject: [PATCH] CheapPower module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fetches electricity prices and chooses the cheapest future time slot. Currently, the only data source is the Nordpool prices for Finland, as provided by ENTSO-E and https://sahkotin.fi. To use: * copy cheap_power.tapp to the file system * Invoke the Tasmota command CheapPower1, CheapPower2, … to * download prices for the next 24 to 48 hours * automatically choose the cheapest future time slot * to schedule Power1 ON, Power2 ON, … at the chosen slot * to install a Web UI in the main menu * For a full installation, you will want something like the following: ``` Backlog0 Timezone 99; TimeStd 0,0,10,1,4,120; TimeDst 0,0,3,1,3,180 Backlog0 SwitchMode1 15; SwitchTopic1 0 Backlog0 WebButton1 boiler; WebButton2 heat PulseTime1 3700 Rule1 ON Clock#Timer DO CheapPower1 ENDON Timer {"Enable":1,"Mode":0,"Time":"18:00","Window":0,"Days":"1111111","Repeat":1,"Output":1,"Action":3} Rule1 1 Timers 1 ``` The download schedule can be adjusted in the timer configuration menu. The prices for the next day will typically be updated in the afternoon or evening of the previous day. In case the prices cannot be downloaded, the download will be retried in 1, 2, 4, 8, 16, 32, 64, 64, 64, … minutes until it succeeds. The user interface in the main menu consists of 4 buttons: ⏮ moves to the previous time slot (or wraps from the first to the last) ⏯ pauses (switches off) or chooses the optimal slot 🔄 requests the prices to be downloaded and the optimal slot to be chosen ⏭ moves to the next time slot (or wraps from the last to the first) The status output above the buttons may also indicate that the output is paused until further command or price update: ⭘ It may also indicate the start time and the price of the slot: ⭙ 2024-11-22 21:00 12.8 ¢ I am using this for controlling a 3×2kW warm water boiler. For my usage, 1 hour every 24 or 48 hours is sufficient. --- tasmota/berry/modules/cheap_power.tapp | Bin 0 -> 5103 bytes tasmota/berry/modules/cheap_power/autoexec.be | 11 ++ .../berry/modules/cheap_power/cheap_power.be | 154 ++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 tasmota/berry/modules/cheap_power.tapp create mode 100644 tasmota/berry/modules/cheap_power/autoexec.be create mode 100644 tasmota/berry/modules/cheap_power/cheap_power.be diff --git a/tasmota/berry/modules/cheap_power.tapp b/tasmota/berry/modules/cheap_power.tapp new file mode 100644 index 0000000000000000000000000000000000000000..4235d50eb5d49289d7bef1683271b549228acc7a GIT binary patch literal 5103 zcmb_g-EJGl6?T)h7c9^M?R{}9gWjb>t*AfOP(=xbYcz&!L6D`SsUi@o-66Tva+lbh zHEk&Z8ld+9+FrC*1&SbdY0+2cO;Pj-dg~|1BlJ6GW|m7T0gNI{WbV$)`9J47XL$JZ z-S4(;@#pf3&p!J7#UI}M3(x%b1N?=R&ebaw1?TEzSkNq{KIyQW<~j}a7<{o5@S|@I@?$Wq z%CSF-7f2(PF_^LtVPOvDQ%l#_4oV#s+PAV*7PmUB!`j~e!rp)X<3B%o|Hrp(@!t<& zZVZsCxPQs6@|*QC_|TdrYIi{OQmU8WO8`)_yaKMb?NZo$&1H$T<_A!;V&K@^YccR4j%vNxKEplR3}fcxw5On^AwKK zuT$0cW=X8aJL|n(+uPgK#oh`?h!4e!CpNbEzy^mKq zKlk?DeD=59HFIchcTLCtFS~We?(g3UyA8wctH1p5ci#%XO{d3fLh(X(|vdKWm+_m)C=^z8V_a}KuG z{%Ey5S#8J1?e}+jJ^UZ^dc9wJEf8=~bA4MNDpfiRY~}Xp^h`o6i>6%d8pDz$7F`@% z0V=^RentI0WvJ0lK9RRwT6d3oH58B1{5(v9SiNN27)%R=a^=G0j|1s6ANj)uLSKRT zH6*Ri=nA1X31)|h(c15-dpJQM-Yx!I$~Z*}QGm)`vL zi!R;Y;^CvxstiOqr1&~af?{EJmo^`4)%zAl*`FpJ3uo`qh6r$$ z)R}|oVG@I?fu-)!+UlOrJIrD~FfG3Bpl4}Sz!+smI};Q)*D=&BMN33;9k>8u`hjs} za%rK)WQcJ#%sdbRs*mHWg8_zFl-ha_9IUz?Aq6Sw8a}8n+BLqi3r_A?lVu_nnQV4` zao;A3>eF-U**Y$-0gsylAfs=x)S{%Nt9@Udn(Z^RG=}Bchd`{+NST%)0iX~w?R-+^ zNgYyI+;TlM1fnUW_M+n=TBu3>QrVz-(%lAmW)oSk2;MX-+?23CuOr$q&);mKga$E+ z>2{x`TR-#7%8zNU!Rqc^9NI@)(e+Xmz$bsK!;ve;z7d`#qQnJ!^# zL&F=qa0P9$@^6dR4?q%)JN1ygOvV?}CK-qpat9-wKog2*LWZhHjSmq?yddEFQMad*gePf(!o$MXk z9v;=d-2fRH>(Zv|uT+Omzw^#d-~Hxa+3>@Yd_Mn_zZ$=}H3`4&=PE;X>(=L=euc+| XNQ~x(Prvtl-hT)GRrrp7!f*crRN(7| literal 0 HcmV?d00001 diff --git a/tasmota/berry/modules/cheap_power/autoexec.be b/tasmota/berry/modules/cheap_power/autoexec.be new file mode 100644 index 000000000..231c7fd4a --- /dev/null +++ b/tasmota/berry/modules/cheap_power/autoexec.be @@ -0,0 +1,11 @@ +var wd = tasmota.wd +tasmota.add_cmd("CheapPower", + def (cmd, idx) + import sys + var path = sys.path() + path.push(wd) + import cheap_power + path.pop() + cheap_power.start(idx) + end +) diff --git a/tasmota/berry/modules/cheap_power/cheap_power.be b/tasmota/berry/modules/cheap_power/cheap_power.be new file mode 100644 index 000000000..bf45f8bf0 --- /dev/null +++ b/tasmota/berry/modules/cheap_power/cheap_power.be @@ -0,0 +1,154 @@ +import webserver +import json + +var cheap_power = module("cheap_power") + +cheap_power.init = def (m) +class CheapPower + var prices # future prices for up to 48 hours + var times # start times of the prices + var timeout# timeout until retrying to update prices + var chosen # the chosen time slot + var channel# the channel to control + var tz # the current time zone offset from UTC + static var PAST = -3600 # minimum timer start age + static var MULT = .1255 # conversion to ¢/kWh including 25.5% VAT + static var PREV = 0, PAUSE = 1, UPDATE = 2, NEXT= 3 + static var UI = "" + "" + "" + "" + "" + "
" + static var URL0 = 'http://sahkotin.fi/prices?start=', URL1 = '&end=' + static var URLTIME = '%Y-%m-%dT%H:00:00.000Z' + + def init() + self.prices = [] + self.times = [] + end + + def start(idx) + if idx == nil || idx < 1 || idx > tasmota.global.devices_present + tasmota.log(f"CheapPower{idx} is not a valid Power output") + tasmota.resp_cmnd_failed() + else + self.channel = idx - 1 + tasmota.add_driver(self) + self.update() + tasmota.resp_cmnd_done() + end + end + + def power(on) tasmota.set_power(self.channel, on) end + + # fetch the prices for the next 24 to 48 hours + def update() + var wc = webclient() + var rtc = tasmota.rtc() + self.tz = rtc['timezone'] * 60 + var now = rtc['utc'] + var url = self.URL0 + + tasmota.strftime(self.URLTIME, now) + self.URL1 + + tasmota.strftime(self.URLTIME, now + 172800) + wc.begin(url) + var rc = wc.GET() + if rc == 200 + var data = json.load(wc.get_string()) + wc.close() + if data data = data.find('prices') end + var prices = [], times = [] + if data + for i: data.keys() + var datum = data[i] + prices.push(self.MULT * datum['value']) + times.push(tasmota.strptime(datum['date'], + '%Y-%m-%dT%H:%M:%S.000Z')['epoch']) + end + self.timeout = nil + self.prices = prices + self.times = times + self.schedule_chosen(self.find_cheapest(), now, self.PAST) + return + end + else + wc.close() + print(f'error {rc} for {url}') + end + # We failed to update the prices. Retry in 1, 2, 4, 8, …, 64 minutes. + if !self.timeout + self.timeout = 60000 + elif self.timeout < 3840000 + self.timeout = self.timeout * 2 + end + tasmota.set_timer(self.timeout, /->self.update()) + end + + # determine the cheapest slot + def find_cheapest() + var cheapest, N = size(self.prices) + if N + cheapest = 0 + for i: 1..N-1 + if self.prices[i] < self.prices[cheapest] cheapest = i end + end + end + return cheapest + end + + def date_from_now(chosen, now) return self.times[chosen] - now end + + # trigger the timer at the chosen hour + def schedule_chosen(chosen, now, old) + tasmota.remove_timer('power_on') + var d = chosen == nil ? self.PAST : self.date_from_now(chosen, now) + if d != old self.power(d > self.PAST && d <= 0) end + if d > 0 + tasmota.set_timer(d * 1000, def() self.power(true) end, 'power_on') + elif d <= self.PAST + chosen = nil + end + self.chosen = chosen + end + + def web_add_main_button() webserver.content_send(self.UI) end + + def web_sensor() + var ch, old = self.PAST, now = tasmota.rtc()['utc'] + var N = size(self.prices) + if N + ch = self.chosen + if ch != nil && ch < N old = self.date_from_now(ch, now) end + while N + if self.date_from_now(0, now) > self.PAST break end + ch = ch ? ch - 1 : nil + self.prices.pop(0) + self.times.pop(0) + N -= 1 + end + end + var op = webserver.has_arg('op') ? int(webserver.arg('op')) : nil + if op == self.UPDATE + self.update() + ch = self.chosen + end + if !N + elif op == self.PAUSE + ch = ch == nil ? self.find_cheapest() : nil + elif op == self.PREV + ch = (!ch ? N : ch) - 1 + elif op == self.NEXT + ch = ch != nil && ch + 1 < N ? ch + 1 : 0 + end + self.schedule_chosen(ch, now, old) + var status = ch == nil + ? '{s}⭘{m}{e}' + : format('{s}⭙ %s{m}%.3g ¢{e}', + tasmota.strftime('%Y-%m-%d %H:%M', self.tz + self.times[ch]), + self.prices[ch]) + tasmota.web_send_decimal(status) + end +end +return CheapPower() +end +return cheap_power