Das sind super Nachrichten! 
Hier sind meine drei Shelly scripts. Das sind meine ersten Shelly-Skripte. Verbesserungen oder Anregungen sind jederzeit willkommen.
Ein dropdown gibt es als virtual component in Shelly nicht, aber das gibt es bestimmt in Home Assistant o. Ä.
Sie benutzen folgende virtual components:
Shelly script 1 - setInverterAndFloorHeatingSchedule:
let outdoor_temp_threshold = 18;
// Virtual component id ID
let at_home_boolean_component_id = 200;
let outdoor_temp_component_id = 200;
let device_count_component_id = 201;
let outdoor_temp_forecast_component_id = 202;
let indoor_temp_component_id = 203;
let floor_heating_devices = ["192.168.50.xxx", // heating_floor_office1a
"192.168.50.yyy", // heating_floor_bedroom2a
"192.168.50.zzz", // heating_floor_bedroom3
"192.168.50.aaa", // heating_floor_livingroom4
"192.168.50.bbb" // heating_floor_childroom5
];
let i = 0
// Main function
function setInverter() {
let d = Date();
let hour = d.getHours();
let heating_url = "";
if(getDeviceCount()>0){
print("Devices connected, so assuming that at home.");
setAtHomeBoolean(true);
}
else if (getDeviceCount()==0 & hour >= 10 & hour <= 21){
print("No devices connected and time between 10:00 and 22:00, so assuming that away.");
setAtHomeBoolean(false);
}
if(getAtHomeBoolean()==false){ // Away from home
// Switch off floor heating
// Iterate through floor heating devices
//for (let i = 0; i < floor_heating_devices.length; i ++) {
heating_url = "http://" + floor_heating_devices[i] + "/rpc/Switch.SetConfig?id=0&config={%22in_mode%22:%22detached%22,%22initial_state%22:%22restore_last%22}";
print("Switching off floor heating device "+i+": "+heating_url);
try{Shelly.call("HTTP.GET", { url: heating_url }, function (result, error_code, error_message) {})}catch(e) {};
if(i < floor_heating_devices.length-1){i=i+1;}else{i=0} // Only 5 rpc calls per script, so switching only one heating device per script iteration.
//}
if(getOutdoorTempForecast()>outdoor_temp_threshold){ // Hot day expected
// Set inverter
if(getOutdoorTemp()<getIndoorTemp()){ // Take cold air in
print("Ventilation level 4 (10%)");
try{Shelly.call("HTTP.GET", { url: "http://localhost/rpc/light.set?id=0&brightness=10" }, function (result, error_code, error_message) {})}catch(e) {};
}
else{ // Keep cold air inside
print("Heat recovery ventilation level 4 (80%)");
try{Shelly.call("HTTP.GET", { url: "http://localhost/rpc/light.set?id=0&brightness=80" }, function (result, error_code, error_message) {})}catch(e) {};
}
}
else{ // Cold day expected
// Set inverter
if(getOutdoorTemp()<getIndoorTemp()){ // Keep warmth inside
print("Heat recovery ventilation level 4 (80%)");
try{Shelly.call("HTTP.GET", { url: "http://localhost/rpc/light.set?id=0&brightness=80" }, function (result, error_code, error_message) {})}catch(e) {};
}
else{ // Take warmth inside
print("Ventilation level 4 (10%)");
try{Shelly.call("HTTP.GET", { url: "http://localhost/rpc/light.set?id=0&brightness=10" }, function (result, error_code, error_message) {})}catch(e) {};
}
}
}
else{ // At home
if(getOutdoorTempForecast()>outdoor_temp_threshold){ // Hot day expected
// Switch off floor heating
// Iterate through floor heating devices
//for (let i = 0; i < floor_heating_devices.length; i ++) {
heating_url = "http://" + floor_heating_devices[i] + "/rpc/Switch.SetConfig?id=0&config={%22in_mode%22:%22detached%22,%22initial_state%22:%22restore_last%22}";
print("Switching off floor heating device "+i+": "+heating_url);
try{Shelly.call("HTTP.GET", { url: heating_url }, function (result, error_code, error_message) {})}catch(e) {};
if(i < floor_heating_devices.length-1){i=i+1;}else{i=0} // Only 5 rpc calls per script, so switching only one heating device per script iteration.
//}
if(hour>=23 | hour < 8){
print("Ventilation OFF (40%) between 23:00 and 08:00 if at home");
try{Shelly.call("HTTP.GET", { url: "http://localhost/rpc/light.set?id=0&brightness=40" }, function (result, error_code, error_message) {})}catch(e) {};
}
else if(getOutdoorTemp()<getIndoorTemp()){ // Take warmth inside
print("Ventilation level 1 (30%)");
try{Shelly.call("HTTP.GET", { url: "http://localhost/rpc/light.set?id=0&brightness=30" }, function (result, error_code, error_message) {})}catch(e) {};
}
else{ // Keep cold air inside
print("Heat recovery ventilation level 1 (50%)");
try{Shelly.call("HTTP.GET", { url: "http://localhost/rpc/light.set?id=0&brightness=50" }, function (result, error_code, error_message) {})}catch(e) {};
}
}
else{ // Cold day expected
// Switch on floor heating
// Iterate through floor heating devices
//for (let i = 0; i < floor_heating_devices.length; i ++) {
heating_url = "http://" + floor_heating_devices[i] + "/rpc/Switch.SetConfig?id=0&config={%22in_mode%22:%22follow%22,%22initial_state%22:%22match_input%22}";
print("Switching on floor heating device "+i+": "+heating_url);
try{Shelly.call("HTTP.GET", { url: heating_url }, function (result, error_code, error_message) {})}catch(e) {};
if(i < floor_heating_devices.length-1){i=i+1;}else{i=0} // Only 5 rpc calls per script, so switching only one heating device per script iteration.
//}
if(hour>=23 | hour < 8){
print("Ventilation OFF (40%) between 23:00 and 08:00 if at home");
try{Shelly.call("HTTP.GET", { url: "http://localhost/rpc/light.set?id=0&brightness=40" }, function (result, error_code, error_message) {})}catch(e) {};
}
else if(getOutdoorTemp()<getIndoorTemp()){ // Keep warmth inside
print("Heat recovery ventilation level 1 (50%)");
try{Shelly.call("HTTP.GET", { url: "http://localhost/rpc/light.set?id=0&brightness=50" }, function (result, error_code, error_message) {})}catch(e) {};
}
else{ // Take warmth inside
print("Ventilation level 1 (30%)");
try{Shelly.call("HTTP.GET", { url: "http://localhost/rpc/light.set?id=0&brightness=30" }, function (result, error_code, error_message) {})}catch(e) {};
}
}
}
print(""+getOutdoorTemp());
print(""+getOutdoorTempForecast());
print(""+getAtHomeBoolean());
}
function getOutdoorTemp() {
let current_value = Virtual.getHandle("number:"+outdoor_temp_component_id);
return current_value.getValue();
}
function getOutdoorTempForecast() {
let current_value = Virtual.getHandle("number:"+outdoor_temp_forecast_component_id);
return current_value.getValue();
}
function getIndoorTemp() {
let current_value = Virtual.getHandle("number:"+indoor_temp_component_id);
return current_value.getValue();
}
function getDeviceCount() {
let current_value = Virtual.getHandle("number:"+device_count_component_id);
return current_value.getValue();
}
function getAtHomeBoolean() {
let current_value = Virtual.getHandle("boolean:"+at_home_boolean_component_id);
return current_value.getValue();
}
function setAtHomeBoolean(x) {
Shelly.call(
"Boolean.Set",
{
"id": at_home_boolean_component_id,
"value": x
});
print("At home set to: " + x);
}
// Check inverter settings every 1 min
Timer.set(60000, true, function () {
print("Timer triggered. Setting inverter.");
setInverter();
});
// Initial fetch to verify script is working
print("Initial fetch to verify setInverter script is working...");
setInverter();
Shelly script 2 - checkDevicesOnNetwork:
// List all devices that the script should check
let devices = [
"192.168.50.ggg", // Samsung
"192.168.50.hhh" // iPhone
];
// Virtual component id ID
let component_id = 201;
let connected_device_count = 0;
// Function to check devices on network
function checkDevicesOnNetwork() {
print("Starting checkDevicesOnNetwork");
let url = "";
connected_device_count = 0;
// Iterate through devices
for (let i = 0; i < devices.length; i ++) {
url = devices[i];
print("Device "+i+": "+url);
Shelly.call("HTTP.GET", { url: url }, processHttpResponse, url);
}
print("Current connected device count: "+getCounter());
}
function processHttpResponse(result, error_code, error_message, url) {
if (error_code == -114) {
print("Device "+url+" connected");
print("Error message: " + error_message);
print("Error code: " + error_code);
connected_device_count = connected_device_count + 1;
}
else if (error_code == -104) {
print("Device "+url+" disconnected");
print("Error message: " + error_message);
print("Error code: " + error_code);
connected_device_count = connected_device_count + 0;
}
else {
print("HTTP request successful: "+result+" - "+error_message+" - "+error_code);
}
setCounter(connected_device_count);
}
function setCounter(x) {
Shelly.call(
"Number.Set",
{
"id": component_id,
"value": x
});
print(x+ " devices connected");
}
function getCounter() {
let current_counter_value = Virtual.getHandle("number:"+component_id);
return current_counter_value.getValue();
}
// Check device connections each minute
Timer.set(60000, true, function () {
print("Timer triggered. Checking device connections.");
checkDevicesOnNetwork();
});
// Initial fetch to verify script is working
print("Initial check to verify script is working...");
setCounter(0);
Shelly script 3 - Openweather_script:
// API key and city settings
let api_key = "xxxxxx"; // OpenWeatherMap API key
let city = "Berlin,de"; // Change it to your city, country
// Virtual component id ID
let outdoor_temp_component_id = 200;
// Function to fetch weather data from OpenWeatherMap
function fetchWeatherData() {
print("Starting fetchWeatherData function");
// URL az OpenWeatherMap API kéréshez
let url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=" + api_key;
Shelly.call("HTTP.GET", { url: url }, function (result, error_code, error_message) {
if (error_code !== 0) {
print("Error fetching weather data: " + error_message);
print("Error code: " + error_code);
} else {
print("HTTP GET request successful. Parsing result...");
try {
let weatherData = JSON.parse(result.body);
let temperature = weatherData.main.temp;
print("External temperature in your city: " + temperature + "°C");
Shelly.call(
"Number.Set",
{
"id": outdoor_temp_component_id,
"value": temperature
},
function(result, error_code, error_message) {
if (error_code !== 0) {
print("An error occurred when updating the virtual component: " + error_message);
} else {
print("Virtual componentupdated. Value: " + temperature);
}
}
);
} catch (e) {
print("Error parsing weather data: " + e.message);
print("Received data: " + result.body); // Print the actual data received for debugging
}
}
});
}
// Fetch weather data every 15 min
Timer.set(900000, true, function () {
print("Timer triggered. Fetching weather data...");
fetchWeatherData();
});
// Initial fetch to verify script is working
print("Initial fetch to verify script is working...");
fetchWeatherData();
// API key and city settings
let api_key = "xxxxxx"; // OpenWeatherMap API key
let city = "Berlin,de"; // Change it to your city, country
let forecast_intervals = 8; // 16 would be nice, as it's two days, but more than a certain number leads to out of memory error
// Virtual component id ID
let outdoor_forecast_component_id = 202;
// Function to fetch weather data from OpenWeatherMap
function fetchWeatherForecast() {
print("Starting fetchWeatherForecastfunction");
// URL az OpenWeatherMap API kéréshez
let url = "http://api.openweathermap.org/data/2.5/forecast?q=" + city + "&units=metric&appid=" + api_key + "&cnt=" + forecast_intervals;
Shelly.call("HTTP.GET", { url: url }, function (result, error_code, error_message) {
if (error_code !== 0) {
print("Error fetching weather data: " + error_message);
print("Error code: " + error_code);
} else {
print("HTTP GET request successful. Parsing result...");
try {
let weatherData = JSON.parse(result.body);
let temperature = -30;
let i = 0;
while (i < forecast_intervals) {
if (weatherData.list[i].main.temp>temperature) {temperature=weatherData.list[i].main.temp}
i = i + 1;
}
// delete weatherData
print("Maximal forecasted temperature during current and next day in your city: " + temperature + "°C");
Shelly.call(
"Number.Set",
{
"id": outdoor_forecast_component_id,
"value": temperature
},
function(result, error_code, error_message) {
if (error_code !== 0) {
print("An error occurred when updating the virtual component: " + error_message);
} else {
print("Virtual component updated. Value: " + temperature);
}
}
);
} catch (e) {
print("Error parsing weather data: " + e.message);
print("Received data: " + result.body); // Print the actual data received for debugging
}
}
});
}
// Fetch weather data every 15 min
Timer.set(900000, true, function () {
print("Timer triggered. Fetching weather data...");
fetchWeatherForecast();
});
// Initial fetch to verify script is working
print("Initial fetch to verify script is working...");
fetchWeatherForecast();