-
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 #64 from Spot-hinta-fi/multi-relay-quick-code
New version of "Shelly-pikakoodi.js" script. It now supports multiple relays with different quick codes. Also updated comment about supported Shelly firmwares. Added 1.2.0.
- Loading branch information
Showing
7 changed files
with
28 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,26 @@ | ||
// Kiitos tuestasi: https://www.buymeacoffee.com/spothintafi | ||
// Tuetut Shelly ohjelmistot: 1.0.3 - 1.1.0. Skriptin versio: 2024-01-21 | ||
// Tuetut Shelly ohjelmistot: 1.0.3 - 1.2.0. Skriptin versio: 2024-02-17 | ||
|
||
// Asetukset | ||
let Pikakoodi = 103; // Hae sopiva pikakoodi: https://spot-hinta.fi/pikakoodit | ||
let Releet = [0]; // Releen tai releiden numero. Arvoa ei tarvitse vaihtaa, jos Shelly on yksireleinen. Useampi rele määritellään pilkulla eroteltuna. Esim. [0,1,2] | ||
// OHJE: Anna "pikakoodi" kullekin ohjattavalle releelle. Koodilla 999 voit ohittaa releen. | ||
// Esimerkki 1: Ohjaa releitä 2 ja 3. Aseta pikakoodit näin: [999, 103, 125] | ||
// Esimerkki 2: Ohjaa vain yhtä relettä. Aseta pikakoodi näin: [103] | ||
// Hae sopivat pikakoodit täältä: https://spot-hinta.fi/pikakoodit | ||
|
||
// Alla olevaa koodia ei tarvitse muokata. Se suorittaa kyselyn rajapintaan ja ohjaa releiden toimintaa. | ||
// Ohjaus tapahtuu tunnoin vaihduttua noin puolen minuutin aikana. Virheen tapahtuessa rele suljetaan, eli ohjaus sallitaan. | ||
let cHour = -1; let Executed = false; let pAction = ""; let url = "https://api.spot-hinta.fi/QuickCode/" + Pikakoodi; | ||
print("Pikakoodi: Skripti suoritetaan..."); | ||
Timer.set(30000, true, function () { | ||
let hour = new Date().getHours(); | ||
if (cHour !== hour) { cHour = hour; Executed = false; print("Pikakoodi: Ensiohjaus tai tunti vaihtui. Suoritetaan ohjaus.") } | ||
if (cHour == hour && Executed == true) { print("Pikakoodi: Kuluva tunti on jo suoritettu onnistuneesti."); return; } | ||
Shelly.call("HTTP.GET", { url: url, timeout: 15, ssl_ca: "*" }, function (result, error_code) { | ||
if (error_code === 0 && result !== null) { | ||
if ((result.code === 400 || result.code === 200) && pAction === result.code) { print("Releiden tila pysyy samana kuin aiemmalla tunnilla."); Executed = true; return; } | ||
if (result.code === 400 || result.code === 200) { | ||
print("Pikakoodi: Onnistunut vastaus palvelimelta: " + result.body); | ||
for (let i = 0; i < Releet.length; i++) { | ||
if (result.code === 400) { Shelly.call("Switch.Set", "{ id:" + Releet[i] + ", on:false}", null, null); pAction = result.code; print("Pikakoodi: Tunti on liian kallis. Rele " + Releet[i] + " avataan."); Executed = true; } | ||
if (result.code === 200) { Shelly.call("Switch.Set", "{ id:" + Releet[i] + ", on:true}", null, null); pAction = result.code; print("Pikakoodi: Tunti on tarpeeksi halpa. Rele " + Releet[i] + " suljetaan."); Executed = true; } | ||
} | ||
return; | ||
} | ||
} | ||
print("Pikakoodi: Virheellinen vastaus palvelimelta. Koodi: " + result.code + " - Vastaus: " + result.body); | ||
for (let i = 0; i < Releet.length; i++) { | ||
Shelly.call("Switch.Set", "{ id:" + Releet[i] + ", on:true}", null, null); print("Pikakoodi: Virhe ohjauksessa, rele " + Releet[i] + " suljetaan."); pAction = ""; Executed = false; | ||
} | ||
}) | ||
// PIKAKOODIT: Aseta tähän parametriin haluamasi pikakoodit | ||
let Pikakoodit = [103]; | ||
|
||
// SKRIPTI. Älä muokkaa alla olevaa koodia | ||
let rr = -1; let tunti = -1; let vtila = false; print("Pikakoodi: Skripti käynnistyy."); | ||
Timer.set(15000, true, function () { | ||
if (tunti == new Date().getHours()) { return; } else { | ||
rr--; rr = (rr < 0) ? Pikakoodit.length - 1 : rr; if (Pikakoodit[rr] == 999) { return; } | ||
Shelly.call("HTTP.GET", { url: "https://api.spot-hinta.fi/QuickCode/" + Pikakoodit[rr], timeout: 5, ssl_ca: "*" }, function (res, err) { | ||
let tila = (err == 0 && res != null && res.code == 400) ? false : true; | ||
let virhe = (err == 0 && res != null && res.code == 200 || res.code == 400) ? false : true; if (virhe) { vtila = true; } | ||
Shelly.call("Switch.Set", "{ id:" + rr + ", on:" + tila + "}", null, null); | ||
print("Pikakoodi: Rele " + rr + " on kytketty " + (tila ? "päälle" : "pois päältä") + (!virhe ? "." : " (virhetilanne).")); | ||
if (virhe && res != null && res.body != null) { print("Pikakoodi: Rele " + rr + " virheviesti: " + res.body) } | ||
if (rr == 0) { tunti = (vtila) ? -1 : new Date().getHours(); print("Pikakoodi: Tunti suoritettu? " + !vtila); vtila = (tunti > -1) ? false : true; } | ||
}); | ||
} | ||
}); |
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