Advertisement
seston

Nordpooli boiler

Jul 9th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. // https://elspotcontrol.netlify.app/
  2. // This version: 18.1.2023
  3.  
  4.  
  5. // Find the time from time period starting here and lasting this many hours
  6. let period_start = 23;
  7. let period_length = 22;
  8.  
  9. // If period_start refers to this hour today, this should be 0. If the it is tomorrow, it must be 1.
  10. let period_day = 0;
  11.  
  12. // Request cheapest hours for this length. Set this to typical length needed for f. ex. water heater.
  13. let needed_length = 3;
  14.  
  15. // Turn off after this many hours. May be good to keep this longer than needed_length, as
  16. // f. ex. heating water may sometimes take longer
  17. let turn_off_hours = 5;
  18.  
  19. // If fetching prices fails, use these schedules. Crontab format
  20. let defaultstart = "0 1 2 * * SUN,MON,TUE,WED,THU,FRI,SAT";
  21. let defaultend = "0 1 7 * * SUN,MON,TUE,WED,THU,FRI,SAT";
  22.  
  23. // Maximum average hourly price. If hourly price is higher than this, do not turn switch on!
  24. // This setting still sets the schedule, but sets switch off both on start and stop.
  25. // If you want, you can manually then turn the scheduled time on.
  26. // By default, and if you do not want to uset this, set this very high.
  27. // The price is EUR per megawatthour, not including taxes! So eg. for a price of 50c/kWH, use 500
  28. let max_avg_price = 999999;
  29.  
  30. // Crontab for running this script. Good to keep this way
  31. // This means that this script is run at random moment during the first 15 minutes after 18.00
  32. // Random timing is used so that all clients wouldn't be polling the server exactly at same time
  33. let minrand = JSON.stringify(Math.floor(Math.random() * 15));
  34. let secrand = JSON.stringify(Math.floor(Math.random() * 60));
  35. let script_schedule = secrand+" "+minrand+" "+"18 * * SUN,MON,TUE,WED,THU,FRI,SAT";
  36. print(script_schedule);
  37.  
  38.  
  39. // Number for this script. If this doesn't work (as in earlier versions), get it from this url (use your own ip) http://192.168.68.128/rpc/Script.List
  40. let script_number = Shelly.getCurrentScriptId();
  41.  
  42.  
  43.  
  44. // You can check the schedules here (use your own ip) http://192.168.68.128/rpc/Schedule.List
  45.  
  46. function maybe_over_midnight(hour) {
  47. if (hour>24) {
  48. return hour-24;
  49. }
  50. else {
  51. return hour;
  52. }
  53. }
  54.  
  55.  
  56. function find_cheapest(result) {
  57. print("HTTP response is", result);
  58. if (result === null) {
  59. updateSchedules(defaultstart, defaultend, true);
  60. }
  61. else {
  62. print("Finding cheapest hours");
  63.  
  64. let prices = JSON.parse(result.body);
  65.  
  66. let hourly_prices = prices["hourly_prices"];
  67. let num = 0;
  68.  
  69. let cheapest_period = {};
  70. cheapest_period["period_price"] = 999999999999;
  71.  
  72. for (let i = period_start; i < period_start + period_length; i++) {
  73.  
  74. let hour = i;
  75.  
  76. if (i >= 24) {
  77. hour = hour - 24;
  78. period_day = 1;
  79. }
  80.  
  81. let hr = JSON.stringify(period_day)+"."+JSON.stringify(hour);
  82.  
  83. hourly_prices[hr]["period_price"] = 0;
  84.  
  85. for (let a = 0; a < needed_length; a++) {
  86. let ah = hour + a;
  87. let ahday=period_day;
  88. if (ah >= 24) { ah = ah - 24; ahday=1; }
  89. let ahh = JSON.stringify(ahday)+"."+JSON.stringify(ah);
  90. hourly_prices[hr]["period_price"] = hourly_prices[hr]["period_price"] + hourly_prices[ahh]["price"];
  91. }
  92.  
  93. if (hourly_prices[hr]["period_price"] < cheapest_period["period_price"]) {
  94. cheapest_period = hourly_prices[hr];
  95. cheapest_period["hour"] = hour;
  96. print("cheapest");
  97. }
  98.  
  99. print(hour, hourly_prices[hr]["time"], hourly_prices[hr]["price"], hourly_prices[hr]["period_price"]);
  100.  
  101.  
  102.  
  103.  
  104. }
  105. print("Cheapest hour", cheapest_period["time"], cheapest_period["period_price"], "average",
  106. cheapest_period["period_price"] / needed_length)
  107.  
  108. // Set the timers
  109. let timespec = "0 0 " + JSON.stringify(cheapest_period["hour"]) + " * * SUN,MON,TUE,WED,THU,FRI,SAT";
  110. let offspec = "0 0 " + JSON.stringify(maybe_over_midnight(cheapest_period["hour"] + turn_off_hours)) + " * * SUN,MON,TUE,WED,THU,FRI,SAT";
  111.  
  112. // Check the avg price if the switch should or should not be turned on.
  113. let turn_on=true;
  114.  
  115. if (cheapest_period["period_price"] / needed_length > max_avg_price) {
  116. print("Turning off!");
  117. turn_on=false;
  118. }
  119.  
  120. updateSchedules(timespec, offspec, turn_on);
  121. }
  122. }
  123.  
  124.  
  125. function updateSchedules(timespec, offspec, turn_on) {
  126.  
  127. print(Shelly.call("Schedule.DeleteAll"));
  128.  
  129.  
  130.  
  131.  
  132. print(Shelly.call("Schedule.Create", {
  133. "id": 0, "enable": true, "timespec": timespec,
  134. "calls": [{
  135. "method": "Switch.Set",
  136. "params": {
  137. id: 0,
  138. "on": turn_on
  139. }
  140. }]
  141. }
  142. ));
  143.  
  144.  
  145. print(Shelly.call("Schedule.Create", {
  146. "id": 0, "enable": true, "timespec": offspec,
  147. "calls": [{
  148. "method": "Switch.Set",
  149. "params": {
  150. id: 0,
  151. "on": false
  152. }
  153. }]
  154. }
  155. ));
  156.  
  157. // Schedule for the script itself
  158. print(Shelly.call("Schedule.create", {
  159. "id": 3, "enable": true, "timespec": script_schedule,
  160. "calls": [{
  161. "method": "Script.start",
  162. "params": {
  163. "id": script_number
  164. }
  165. }]
  166. }));
  167.  
  168. //Stop this script in one minute from now
  169. Timer.set(60 * 1000, false, function () {
  170. print("Stopping the script");
  171. Shelly.call("Script.stop", { "id": script_number })
  172. });
  173.  
  174. }
  175.  
  176.  
  177. function updateTimer() {
  178. print("Starting, fetching hourly prices");
  179. Shelly.call("HTTP.GET", { url: "https://elspotcontrol.netlify.app/spotprices-v01-EE.json", timeout:60, ssl_ca:"*"}, find_cheapest);
  180. }
  181.  
  182.  
  183. updateTimer();
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement