Advertisement
ApiSpotHintaFi

PriceLimit

Jan 17th, 2024 (edited)
1,257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.16 KB | Source Code | 0 0
  1. // You can support spot-hinta.fi service here: https://www.buymeacoffee.com/spothintafi
  2. // Supported Shelly firmwares: 1.0.3 - 1.1.0. Script version: 2024-01-18
  3.  
  4. // Change these settings as you like
  5. let Region = "FI"; // Supported regions: DK1, DK2, EE, FI, LT, LV, NO1, NO2, NO3, NO4, NO5, SE1, SE2, SE3, SE4
  6. let Relays = [0]; // Relays to control with this script. List relays as comma separated. For example: [0,1,2]
  7. let PriceLimit = 10;  // Set the limit (euro cents) when relay is turned on. For example to stop heating by turning the relay on.
  8. let PriceLimitHours = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]; // When price limit is checked?
  9.  
  10. // Don't touch below!
  11. print("PriceLimit: Script has started succesfully. The first relay action happens in 30 seconds.");
  12. let cHour = -1; let Executed = false; let urlToCall = "https://api.spot-hinta.fi/JustNowRank/0/" + PriceLimit + "?region=" + Region;
  13.  
  14. Timer.set(30000, true, function () {
  15.     let hour = new Date().getHours();
  16.     if (cHour !== hour) { cHour = hour; Executed = false; print("PriceLimit: The hour has now changed and a new relay action is going to be performed.") }
  17.     if (cHour == hour && Executed == true) { print("PriceLimit: This hour has already been executed. Waiting for an hour change."); return; }
  18.     if (PriceLimitHours.indexOf(cHour) > -1) { Shelly.call("HTTP.GET", { url: urlToCall, timeout: 15, ssl_ca: "*" }, RunResponse); }
  19.     else {
  20.       print("PriceLimit: This hour has not been listed in PriceLimitHours. Turning relay(s) off."); Executed = true;      
  21.       for (let i = 0; i < Relays.length; i++) {
  22.           Shelly.call("Switch.Set", "{ id:" + Relays[i] + ", on:false}", null, null);
  23.       }
  24.     }
  25. });
  26.  
  27. function RunResponse(result, error_code) {
  28.     let relayStatus = false; Executed = true;
  29.     if (error_code === 0 && result !== null && result.code === 400) { relayStatus = true; print("PriceLimit: This hour is expensive, turning relay on.");
  30.     } else { print("PriceLimit: This hour is cheap, turning relay off"); }
  31.     for (let i = 0; i < Relays.length; i++) { Shelly.call("Switch.Set", "{ id:" + Relays[i] + ", on:" + relayStatus + "}", null, null); }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement