Open realrelook opened 2 weeks ago
Hello. It's a good idea, problem is I don't think the Shelly plug can handle any type of authentication but if it's a open source perhaps you can reach it.
Shelly doesn't support the full language in my experience and i don't have a compatible device right now to test it on but you can try this
let CONFIG = { priceFileUrl: "http://IP_ADDRESS/PATH/TO/FILE.txt", // URL to the text file with the price checkInterval: 60 * 10000 // Check every 600 seconds (10 minutes) };
function fetchEnergyPriceAndControlSwitch() { Shelly.call( "http.request", { url: CONFIG.priceFileUrl, method: "GET" }, function (response, error) { if (error) { print("Error fetching price file: ", error); return; } let maxPrice; try { maxPrice = parseFloat(response.body); } catch (e) { print("Error parsing price file: ", e); return; }
if (isNaN(maxPrice)) {
print("Invalid price in the file");
return;
}
// Control the switch based on the fetched price
if (maxPrice > 0) { // Additional logic can be added here if needed
activateSwitch(false);
} else {
activateSwitch(true);
}
print("Fetched energy price from file - ", maxPrice);
}
); }
function activateSwitch(activate) { Shelly.call( "Switch.Set", { id: 0, on: activate }, function (response, error_code, error_message) {} ); }
// Run the function at the specified interval Timer.set(CONFIG.checkInterval, true, function () { console.log("Checking energy price"); fetchEnergyPriceAndControlSwitch(); });
// Initial check at startup fetchEnergyPriceAndControlSwitch();
Den fre 5 juli 2024 09:27realrelook @.***> skrev:
Hello, now I have 2 Shelly Plus PlugS running with the script, now want to add a 3rd one. To change the switch price in every script is possible, but a little annoying. Is there a possibility to pull the EuroCt price from a file (text file) from a ‘home server’ e.g. the ‘NAS’ from FritzBox. This would make life a little easier. 😊 This is the line of code which I am talking about: maxEnergyPrice: 0.65, // Price above which the power should be turned off in local currency for your API (Sweden=SEK)
Something like 'get from 178.168.199.1/server/Tibbermax.txt' instead of the 0.65 Just an Idea. Thank you
— Reply to this email directly, view it on GitHub https://github.com/quadpunk/ShellyTibberScript/issues/6, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2S7ZULQ6BOMGDSQ3LTCTBDZKZDF5AVCNFSM6AAAAABKMVPTHCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGM4TEMBRGM4TKNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>
Sorry I also forgot to implement the compare to Tibber price function. Try this:
let TIBBER_CONFIG = { apiEndpoint: "https://api.tibber.com/v1-beta/gql", accessToken: "YOUR TIBBER API HERE", // Add your tibber API here priceFileUrl: "http://IP_ADDRESS/PATH/TO/FILE.txt", // URL to the text file with the price checkInterval: 60 * 10000 // Check every 600 seconds (10 minutes) };
function getEnergyPriceQuery() { return JSON.stringify({ query: "{\n viewer {\n homes {\n currentSubscription {\n priceInfo {\n current {\n total\n }\n }\n }\n }\n }\n}" }); }
function fetchMaxEnergyPrice(callback) { Shelly.call( "http.request", { url: TIBBER_CONFIG.priceFileUrl, method: "GET" }, function (response, error) { if (error) { print("Error fetching price file: ", error); return; } let maxPrice; try { maxPrice = parseFloat(response.body); } catch (e) { print("Error parsing price file: ", e); return; } callback(maxPrice); } ); }
function controlSwitchBasedOnPrice() { Shelly.call( "http.request", { url: TIBBER_CONFIG.apiEndpoint, method: "POST", headers: { "Authorization": "Bearer " + TIBBER_CONFIG.accessToken, "Content-Type": "application/json" }, body: getEnergyPriceQuery() }, function (response, error) { if (error) { print("Error: ", error); return; }
let tibberData;
try {
tibberData = JSON.parse(response.body);
} catch (e) {
print("Error parsing JSON: ", e);
return;
}
let currentPrice =
tibberData.data.viewer.homes[0].currentSubscription.priceInfo.current.total;
fetchMaxEnergyPrice(function (maxEnergyPrice) {
if (currentPrice > maxEnergyPrice) {
activateSwitch(false);
} else {
activateSwitch(true);
}
print("Current energy price - ", currentPrice);
print("Max energy price from file - ", maxEnergyPrice);
});
}
); }
function activateSwitch(activate) { Shelly.call( "Switch.Set", { id: 0, on: activate }, function (response, error_code, error_message) {} ); }
// Run the function at the specified interval Timer.set(TIBBER_CONFIG.checkInterval, true, function () { console.log("Checking energy price"); controlSwitchBasedOnPrice(); });
controlSwitchBasedOnPrice();
Den fre 5 juli 2024 09:27realrelook @.***> skrev:
Hello, now I have 2 Shelly Plus PlugS running with the script, now want to add a 3rd one. To change the switch price in every script is possible, but a little annoying. Is there a possibility to pull the EuroCt price from a file (text file) from a ‘home server’ e.g. the ‘NAS’ from FritzBox. This would make life a little easier. 😊 This is the line of code which I am talking about: maxEnergyPrice: 0.65, // Price above which the power should be turned off in local currency for your API (Sweden=SEK)
Something like 'get from 178.168.199.1/server/Tibbermax.txt' instead of the 0.65 Just an Idea. Thank you
— Reply to this email directly, view it on GitHub https://github.com/quadpunk/ShellyTibberScript/issues/6, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2S7ZULQ6BOMGDSQ3LTCTBDZKZDF5AVCNFSM6AAAAABKMVPTHCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGM4TEMBRGM4TKNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>
I have a compressed weekend, but I will do my best to check it out within the next 2 days. Thank you.
I did test it. The file on the FritzBox ist called Tibber.txt. I did enter there in line one only 0.65 and saved it. Error code I get from Shelly: Uncaught SyntaxError: Got ID:with expected ':' at file with the price ^ Any Idea.?
Additional info. With the script line priceFileUrl: "file://fritz.box/FRITZ.NAS/shelly/Tibber.txt", // URL to the text or better file://fritz.box/FRITZ.NAS/shelly/Tibber.txt I can see the Tibber.txt in the browser with the price info 0.65 From various browser w/o PW.
No sorry, no idea right now. It may be that shelly doesn't support the fetch function because it doesn't support the full library.
I'll see if I can get my hands on a compatible device so i can debug myself.
Den mån 8 juli 2024 23:06realrelook @.***> skrev:
Additional info. With the script line priceFileUrl: "file://fritz.box/FRITZ.NAS/shelly/Tibber.txt", // URL to the text or better file://fritz.box/FRITZ.NAS/shelly/Tibber.txt I can see the Tibber.txt in the browser with the price info 0.65 From various browser w/o PW.
— Reply to this email directly, view it on GitHub https://github.com/quadpunk/ShellyTibberScript/issues/6#issuecomment-2215310297, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2S7ZUKABFDSUX3NPJTQT63ZLL5OJAVCNFSM6AAAAABKMVPTHCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMJVGMYTAMRZG4 . You are receiving this because you commented.Message ID: @.***>
Hello, now I have 2 Shelly Plus PlugS running with the script, now want to add a 3rd one. To change the switch price in every script is possible, but a little annoying. Is there a possibility to pull the EuroCt price from a file (text file) from a ‘home server’ e.g. the ‘NAS’ from FritzBox. This would make life a little easier. 😊 This is the line of code which I am talking about:
maxEnergyPrice: 0.65, // Price above which the power should be turned off in local currency for your API (Sweden=SEK)
Something like 'get from 178.168.199.1/server/Tibbermax.txt' instead of the 0.65 Just an Idea. Thank you