Advertisement
ApiSpotHintaFi

Malliparametrit

Jan 22nd, 2024 (edited)
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Thank you for your support: https://www.buymeacoffee.com/spothintafi
  2. // Supported Shelly firmwares: 1.0.3 - 1.1.0. Script version: 2024-01-21
  3.  
  4. // Region to use
  5. let Region = "FI"; // Supported regions: DK1, DK2, EE, FI, LT, LV, NO1, NO2, NO3, NO4, NO5, SE1, SE2, SE3, SE4
  6.  
  7. // First settings to control relay
  8. let SETTINGS_1 =
  9. {
  10.     // Relay settings
  11.     RelayIsInUse: true, // To activate this rule, change this to: true
  12.     Relays: [0], // Tarkista että releen numero on oikein!!!
  13.     RelayName: "Varaaja",  // Name for this relay/rule
  14.     Inverted: false, // If "true", relay logic is inverted
  15.  
  16.     // Conditions when heating should happen
  17.     RanksAllowed: "1,2,3,4,5,6,7,8", // List allowed 'ranks' in this rule.
  18.     PriceAlwaysAllowed: "-999", // Allowed price (in euro cents, without decimals). Use "-999" if not wanted.
  19.     MaxPrice: "20", // Maximum allowed price in euro cents.
  20.     BackupHours: [2, 3, 4, 5, 18, 19, 20, 21], // Backup hours (0...23)
  21.     BoosterHours: "99", // Comma separated list of booster hours (0...23). Relay is always ON during booster hours
  22.  
  23.     // When is this rule active?
  24.     AllowedDays: "1,2,3,4,5,6,7", // 1 = Monday and 7 = Sunday. Modify only if you don't want everyday execution.
  25.     AllowedMonths: "1,2,3,4,5,6,7,8,9,10,11,12", // Execution months: 1=January to 12=December, separated with a comma.
  26.  
  27.     // PriorityHours have two alternative use cases:
  28.     // A) Number of cheapest PriorityHours ("PriorityHoursRank") are given the lowest rank
  29.     // B) Priorityhours price is modified before Rank calculation (this does not guarantee lowest ranks)
  30.     PriorityHours: "99", // Comma separated list of hours (0...23) you want to prioritize. Use "99" to disable this.
  31.     PriorityHoursRank: "3", // How many PriorityHours are given the lowest rank?
  32.     PriceModifier: "0", // If PriorityHours have a lower price, you can compensate that with this parameter. F.ex. "-1.27" (euro cents)
  33.  
  34.     // Script technical fields. Do not edit!
  35.     SettingsNumber: 1, RelayStatus: true, InvertedOn: true, InvertedOff: false, RelayExecuted: false, Url: "", IsFirstRound: true
  36. };
  37.  
  38. // Second settings to control relay
  39. let SETTINGS_2 =
  40. {
  41.     // Relay settings
  42.     RelayIsInUse: true,
  43.     Relays: [1], // Tarkista että releen numero on oikein!!!
  44.     RelayName: "Lattialämmitys",
  45.     Inverted: false,
  46.  
  47.     // Conditions when heating should happen
  48.     RanksAllowed: "1,2,3,4,5,6,7,8",
  49.     PriceAlwaysAllowed: "-999",
  50.     MaxPrice: "20",
  51.     BackupHours: [2, 3, 4, 5, 18, 19, 20, 21],
  52.     BoosterHours: "99",
  53.  
  54.     // When is this rule active?
  55.     AllowedDays: "1,2,3,4,5,6,7",
  56.     AllowedMonths: "1,2,3,4,5,6,7,8,9,10,11,12",
  57.  
  58.     // PriorityHours have two alternative use cases:
  59.     // A) Number of cheapest PriorityHours ("PriorityHoursRank") are given the lowest rank
  60.     // B) Priorityhours price is modified before Rank calculation (this does not guarantee lowest ranks)
  61.     PriorityHours: "99",
  62.     PriorityHoursRank: "3",
  63.     PriceModifier: "0",
  64.  
  65.     // Script technical fields. Do not edit!
  66.     SettingsNumber: 2, RelayStatus: true, InvertedOn: true, InvertedOff: false, RelayExecuted: false, Url: "", IsFirstRound: true
  67. };
  68.  
  69. // Script starts here - Do not edit anything below
  70. print("Rank-and-Price: Script is starting...");
  71. if (SETTINGS_1.RelayIsInUse === false && SETTINGS_2.RelayIsInUse === false) { print("Rank-and-Price: Both rules are disabled, script does nothing!"); }
  72. let currentHour = -1; let roundRobin = 0;
  73.  
  74. Timer.set(30000, true, function () {
  75.     if (currentHour !== new Date().getHours()) {
  76.         currentHour = new Date().getHours();
  77.         if (SETTINGS_1.RelayIsInUse === true) { SETTINGS_1.RelayExecuted = false } else { SETTINGS_1.RelayExecuted = true; };
  78.         if (SETTINGS_2.RelayIsInUse === true) { SETTINGS_2.RelayExecuted = false } else { SETTINGS_2.RelayExecuted = true; };
  79.     }
  80.     if (SETTINGS_1.RelayExecuted === true && SETTINGS_2.RelayExecuted === true) { print("Rank-and-Price: Current hour is already done."); return; }
  81.     if (roundRobin === 0) { ExecuteRelayRule(SETTINGS_1); roundRobin = 1; return; }
  82.     if (roundRobin === 1) { ExecuteRelayRule(SETTINGS_2); roundRobin = 0; return; }
  83. });
  84.  
  85. function ExecuteRelayRule(Settings) {
  86.     if (Settings.RelayIsInUse === false || Settings.RelayExecuted === true) { return; }
  87.     print("Rank-and-Price: running rule for a relay: " + Settings.RelayName);
  88.     Shelly.call("HTTP.Request", { method: "GET", url: Settings.Url, timeout: 10, ssl_ca: "*" }, ProcessHttpRequestResponse, Settings);
  89. }
  90.  
  91. function ProcessHttpRequestResponse(response, error_code, error_msg, Settings) {
  92.     let relayExecuted = SetRelayStatusInShellyBasedOnHttpStatus(response, error_code, error_msg, Settings);
  93.     if (Settings.SettingsNumber === 1) { SETTINGS_1.RelayExecuted = relayExecuted; }
  94.     if (Settings.SettingsNumber === 2) { SETTINGS_2.RelayExecuted = relayExecuted; }
  95. }
  96.  
  97. function SetRelayStatusInShellyBasedOnHttpStatus(response, error_code, error_msg, Settings) {
  98.     if (error_code === 0 && response !== null) {
  99.         if (response.code === 200) { SetRelayStatusInShelly(Settings, Settings.InvertedOn); return true; }
  100.         if (response.code === 400) { SetRelayStatusInShelly(Settings, Settings.InvertedOff); return true; }
  101.     }
  102.     if (Settings.BackupHours.indexOf(cHour) > -1) { SetRelayStatusInShelly(Settings, Settings.InvertedOn); return false; }
  103.     else { SetRelayStatusInShelly(Settings, Settings.InvertedOff); return false; }
  104. }
  105.  
  106. function SetRelayStatusInShelly(Settings, newStatus) {
  107.     if (Settings.RelayStatus === newStatus && Settings.IsFirstRound === false) { print("Rank-and-Price: No action is done. The relay status remains the same as during previous execution."); return; }
  108.  
  109.     for (let i = 0; i < Settings.Relays.length; i++) {
  110.         print("Rank-and-Price: Changing relay status. Id: " + Settings.Relays[i] + " - New relay status: " + newStatus);
  111.         Shelly.call("Switch.Set", "{ id:" + Settings.Relays[i] + ", on:" + newStatus + "}", null, null);
  112.     }
  113.  
  114.     if (Settings.SettingsNumber === 1) { SETTINGS_1.RelayStatus = newStatus; SETTINGS_1.IsFirstRound = false; }
  115.     if (Settings.SettingsNumber === 2) { SETTINGS_2.RelayStatus = newStatus; SETTINGS_2.IsFirstRound = false; }
  116. }
  117.  
  118. function BuildUrl(Settings) {
  119.     let url = "https://api.spot-hinta.fi/JustNowRanksAndPrice";
  120.     url += "?ranksAllowed=" + Settings.RanksAllowed;
  121.     url += "&priceAlwaysAllowed=" + Settings.PriceAlwaysAllowed;
  122.     url += "&maxPrice=" + Settings.MaxPrice;
  123.     url += "&allowedDays=" + Settings.AllowedDays;
  124.     url += "&allowedMonths=" + Settings.AllowedMonths;
  125.     url += "&boosterHours=" + Settings.BoosterHours;
  126.     url += "&priorityHours=" + Settings.PriorityHours;
  127.     url += "&priorityHoursRank=" + Settings.PriorityHoursRank;
  128.     url += "&priceModifier=" + Settings.PriceModifier;
  129.     url += "&region=" + Region;
  130.     return url;
  131. }
  132. SETTINGS_1.Url = BuildUrl(SETTINGS_1);
  133. SETTINGS_2.Url = BuildUrl(SETTINGS_2);
  134. if (SETTINGS_1.Inverted === true) { SETTINGS_1.InvertedOn = false; SETTINGS_1.InvertedOff = true; }
  135. if (SETTINGS_2.Inverted === true) { SETTINGS_2.InvertedOn = false; SETTINGS_2.InvertedOff = true; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement