-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from Spot-hinta-fi/plan-ahead-vesivaraaja-yosi…
…irto Plan ahead vesivaraaja-yosiirto
- Loading branch information
Showing
7 changed files
with
63 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,68 @@ | ||
// Kiitos tuestasi: https://www.buymeacoffee.com/spothintafi | ||
// Tuetut Shelly ohjelmistot: 1.0.3 - 1.4.2. Skriptin versio: 2024-10-09 | ||
// Tuetut Shelly ohjelmistot: 1.0.3 - 1.4.4. Skriptin versio: 2024-11-02 | ||
|
||
// ASETUKSET | ||
let Rankit = [1, 2, 3]; // Listaa 'rankit' (eli tunnin järjestysnumero hinnan mukaan), jolloin rele kytketään | ||
let Rele = 0; // Ohjattavan releen numero | ||
let Rankit = [1, 2, 3]; // Listaa 'rankit' (eli tunnin järjestysnumero hinnan mukaan), jolloin releet kytketään | ||
let Releet = [0]; // Ohjattavien releiden numerot. Esimerkiksi [0,1,2] ohjataksesi kolmea relettä | ||
let Yotunnit = [22, 23, 0, 1, 2, 3, 4, 5, 6]; // Yösiirron tunnit. Näihin ei tarvitse normaalisti koskea (edes kellonsiirron aikaan). | ||
let Hintaero = -1.43; // Paljonko sähkön siirtohinta on halvempi yösiirron aikaan? | ||
let Varatunnit = [3, 4, 5]; // Tunnit jolloin rele kytketään, mikäli Internet yhteys ei toimi tai palvelu on alhaalla | ||
let SallittuHinta = 0; // Hinta kokonaisina sentteinä, jonka alapuolella lämmitys aina sallitaan | ||
let Varatunnit = [3, 4, 5]; // Tunnit jolloin rele kytketään, mikäli ohjaustietoja ei saada haettua. | ||
|
||
// KOODI | ||
let url = "https://api.spot-hinta.fi/JustNowRanksAndPrice?priorityHours=" + Yotunnit.join() + "&priceModifier=" + Hintaero + "&ranksAllowed=" + Rankit.join(); | ||
let hour = -1; let previousAction = ""; print("WaterBoiler: Ohjaus käynnistyy 30 sekunnissa."); | ||
Timer.set(30000, true, function () { | ||
if (hour == new Date().getHours()) { print("WaterBoiler: Odotetaan tunnin vaihtumista."); return; } | ||
let url = "https://api.spot-hinta.fi/PlanAhead?priorityHours=" + Yotunnit.join() + "&priceModifier=" + Hintaero + "&ranksAllowed=" + Rankit.join() + "&priceAlwaysAllowed=" + SallittuHinta; | ||
let hour = -1; let minute = new Date().getMinutes(); let previousAction = ""; print("WaterBoiler: Ohjaus käynnistyy 15 sekunnissa."); | ||
let instructions = null; let loadInstructions = true; let instructionsTimeOut = new Date(); let previousStatus = ""; | ||
|
||
Timer.set(15000, true, function () { | ||
if (loadInstructions == true || instructionsTimeOut < new Date()) { LoadInstructionsFromServer(); } | ||
else { ChangeRelayStatusIfNeeded(); } | ||
|
||
if (minute !== new Date().getMinutes()) { | ||
minute = new Date().getMinutes(); | ||
print("WaterBoiler: Ohjaus on toiminnassa. Releiden tila: " + previousStatus); | ||
} | ||
}); | ||
|
||
function ChangeRelayStatusIfNeeded() { | ||
let relayStatus = GetCurrentlyExpectedRelayStatus(); | ||
if (loadInstructions == true) { print("WaterBoiler: uudet ohjaustiedot täytyy ladata."); return; } | ||
if (previousStatus !== relayStatus.result) { SetRelayStatus(relayStatus); return; } | ||
} | ||
|
||
function SetRelayStatus(newStatus) { | ||
previousStatus = newStatus.result; | ||
for (let i = 0; i < Releet.length; i++) { Shelly.call("Switch.Set", "{ id:" + Releet[i] + ", on:" + newStatus.result + "}", null, null); } | ||
print("WaterBoiler: Releiden tila vaihdettiin. Uusi tila: " + newStatus.result); | ||
} | ||
|
||
function LoadInstructionsFromServer() { | ||
Shelly.call("HTTP.GET", { url: url, timeout: 15, ssl_ca: "*" }, function (res, err) { | ||
hour = (err != 0 || res == null || (res.code !== 200 && res.code !== 400)) ? -1 : new Date().getHours(); | ||
let on = false; | ||
if (hour === -1) { | ||
previousAction = ""; | ||
if (Varatunnit.indexOf(new Date().getHours()) > -1) { | ||
on = true; hour = new Date().getHours(); | ||
print("WaterBoiler: Virhetilanne. Kuluva tunti on varatunti: rele kytketään päälle tämän tunnin ajaksi."); | ||
} else { | ||
print("WaterBoiler: Virhetilanne. Kuluva tunti ei ole varatunti: relettä ei kytketä. Yhteyttä yritetään uudestaan."); | ||
} | ||
} else { if (res.code === 200) { on = true; } } | ||
|
||
if (previousAction !== on) { | ||
Shelly.call("Switch.Set", "{ id:" + Rele + ", on:" + on + "}", null, null); | ||
print("WaterBoiler: Kytketty " + (on ? "päälle" : "pois päältä")); | ||
previousAction = on; | ||
if (err != 0 || res == null || res.code !== 200 || res.body == null) { | ||
print("WaterBoiler: Virhe ohjaustietoja haettaessa. Yritetään uudelleen."); | ||
} else { | ||
print("WaterBoiler: Releen tilaa ei muutettu, koska se olisi sama kuin edellisellä tunnilla."); | ||
instructions = JSON.parse(res.body); loadInstructions = false; | ||
instructionsTimeOut = new Date(instructions[0].epochMs - 10800 * 1000); | ||
print("WaterBoiler: Ohjaustiedot haettiin onnistuneesti. Uudet ohjaustiedot haetaan viimeistään: " + instructionsTimeOut.toString()); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function GetCurrentlyExpectedRelayStatus() { | ||
if (instructions == null || instructions.length == 0) { ActivateBackupHours(); return; } | ||
const epochMs = Date.now(); if (instructions[0].epochMs < epochMs) { ActivateBackupHours(); return; } | ||
|
||
for (let i = 0; i < instructions.length; i++) { | ||
if (instructions.length > i && instructions[i + 1].epochMs > epochMs) { continue; } | ||
if (instructions.length > i && instructions[i + 1].epochMs <= epochMs) { return instructions[i + 1]; } | ||
if (instructions[i].epochMs <= epochMs) { return instructions[i]; } | ||
} | ||
|
||
print("WaterBoiler: Virhetilanne... Ei löytynyt sopivaa ohjaustietoa listalta."); ActivateBackupHours(); | ||
} | ||
|
||
function ActivateBackupHours() { | ||
loadInstructions = true; print("WaterBoiler: Siirrytään varatuntien käyttöön."); | ||
if (Varatunnit.indexOf(new Date().getHours()) > -1) { SetRelayStatus(true); return; } | ||
else { SetRelayStatus(false); return; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters