You can also try this script:
It’s important to know where you save your script. If you save your script in the 1st slot, then your URL will be:
http://<IP of your Shelly>/script/1/myTrigger?SogliaTemp=60.5
Code
let SogliaTemp = 58.5; // Default value
function myCallback(optionalArg)
{
// Check if optionalArg and its property 'query' exist
if (optionalArg && optionalArg.query)
{
// Store the value of optionalArg.query in the variable optArg
let optArg = optionalArg.query;
// Call the function getQueryParams with optArg as an argument and store the result in ArgArray
let ArgArray = getQueryParams(optArg);
// Check if the parameter "SogliaTemp" exists in ArgArray
if (ArgArray.SogliaTemp)
{
// Update the value of SogliaTemp with the passed value
SogliaTemp = parseFloat(ArgArray.SogliaTemp);
}
}
}
// Registration of the endpoint for HTTP requests
HTTPServer.registerEndpoint(
"myTrigger",
myCallback,
"callback_arg"
);
// Function for parsing query parameters from a URL
// Example: From "http://localhost/script/1/myTrigger?param1=value1¶m2=value2¶m3=value3"
// an object is created: {"param1": "value1", "param2": "value2", "param3": "value3"}
function getQueryParams(str)
{
let result = {}; // An empty object is created to store the parameters and their values
let params = str.split('&'); // The input string is split at each '&' to create an array of parameters
// Example: From "param1=value1¶m2=value2¶m3=value3" becomes
// ["param1=value1", "param2=value2", "param3=value3"]
// In the following loop, parameters with names and their values are added to the object
params.forEach(function(param)
{
let paramParts = param.split('='); // The parameter is split at '=' to extract the name and value of the parameter
result[paramParts[0]] = paramParts[1]; // The parameter name is added as the key, and the parameter value as the value to the result object
});
return result; // The result object containing the parameters and their values is returned
}
function Loop(){
try{
var TempH2O = Shelly.getComponentStatus('Temperature', 100).tC; //Lettura temperatura da addon
if (TempH2O < SogliaTemp) {
Shelly.call("Switch.Set", {"id": 0, "on": true});
print("Output: ON");
} else {
Shelly.call("Switch.Set", {"id": 0, "on": false});
print("Output: OFF");
}
print("")
}catch(e){print(e);}
}
Timer.set(1000,true,Loop);
Alles anzeigen