Hallo zusammen,
ich habe mir mit Hilfe von CHATGPT ein kleines Skript zusammengebastelt, welche eine Art Kellerlüft mittels Shelly steuern soll. Problem ist hierbei, dass Shelly das Skript ohne Fehlermeldung nach Zeile 46
var weatherLocation = JSON.parse(response.body);
abbricht.
Die JSON Daten sind fehlerfrei.
Code
let CONFIG = {
accuWeatherAPIKEY: YOURAPIKEY,
weatherCurrentEndpoint: "http://dataservice.accuweather.com/currentconditions/v1/",
locations: {
YOURLOCATION: YOURLOCATION_KEY,
},
windThreshold: 20,
internalHumidityThreshold: 50,
relativeActivationWaterVaporPressure: 0.1,
};
let location = "YOURLOCATION";
let locationKey = CONFIG.locations[location];
function getWeatherURLForLocation(locationKey) {
return (
CONFIG.weatherCurrentEndpoint +
locationKey +
"?apikey=" +
CONFIG.accuWeatherAPIKEY +
"&details=true"
);
}
function httpGet(url, callback) {
Shelly.call("http.get", { url: url }, function (response, error_code, error_message) {
if (error_code !== 0) {
console.error('HTTP request error:', error_message);
callback(error_message, null);
} else if (typeof response === 'object' && response.hasOwnProperty('body')) {
callback(null, response);
} else {
console.error('Unexpected response:', response);
callback('Unexpected response', null);
}
});
}
function getWeatherData(callback) {
httpGet(getWeatherURLForLocation(locationKey), function (error, response) {
if (error) {
console.error("HTTP call to weather service failed:", error);
callback(null);
} else {
console.log("API Request: ", response.body);
var weatherLocation = JSON.parse(response.body);
let temperatureCelsius = weatherLocation[0].Temperature.Metric.Value;
let humidity = weatherLocation[0].RelativeHumidity;
let windSpeed = weatherLocation[0].Wind.Speed.Metric.Value;
let perception = weatherLocation[0].HasPrecipitation;
console.log("Weather Data available");
console.log("Temperature", location, "=", temperatureCelsius, "°C");
console.log("Humidity", location, "=", humidity, "%");
console.log("Wind Speed", location, "=", windSpeed);
console.log("Perception", location, "=", perception);
callback(temperatureCelsius, humidity, windSpeed, perception);
}
});
}
function statusWindowCover(callback) {
httpGet("http://127.0.0.1/rpc/Cover.GetStatus?id=0", function (error, result) {
if (error) {
console.error('Error:', error);
callback(error, null);
} else {
let jsonData = JSON.parse(result.body);
let state = jsonData.state;
console.log("Cover Status: ", state);
callback(null, state);
}
});
}
function closeWindowCover(state, callback) {
if (state !== "closed" && state !== "closing") {
httpGet("http://127.0.0.1/rpc/Cover.Close?id=0", function (error, result) {
if (error) {
console.error('Error:', error);
} else {
console.log("Closing Cover ...");
}
callback(error);
});
} else {
console.log("Cover already closed.");
callback(null);
}
}
function openWindowCover(state, callback) {
if (state !== "open" && state !== "opening") {
httpGet("http://127.0.0.1/rpc/Cover.Open?id=0", function (error, result) {
if (error) {
console.error('Error:', error);
} else {
console.log("Opening Cover ...");
}
callback(error);
});
} else {
console.log("Cover already opened.");
callback(null);
}
}
function getInternalHumidity(callback) {
httpGet("http://127.0.0.1/rpc/Humidity.GetStatus?id=100", function (error, result) {
if (error) {
console.error('Error:', error);
callback(error, null);
} else {
let jsonData = JSON.parse(result.body);
let internalHumidity = jsonData.rh;
console.log("Internal Humidity =", internalHumidity, "%");
callback(null, internalHumidity);
}
});
}
function getInternalTemperature(callback) {
httpGet("http://127.0.0.1/rpc/Temperature.GetStatus?id=100", function (error, result) {
if (error) {
console.error('Error:', error);
callback(error, null);
} else {
let jsonData = JSON.parse(result.body);
let internalTemperature = jsonData.tC;
console.log("Internal Temperature =", internalTemperature, "°C");
callback(null, internalTemperature);
}
});
}
function calculateWaterVaporPressure(humidity, temperatureCelsius) {
let aWater = 17.27;
let bWater = 237.7;
let aIce = 21.8746;
let bIce = 265.5;
let vaporPressure;
if (temperatureCelsius < 0) {
let exponentIce = aIce * temperatureCelsius / (bIce + temperatureCelsius);
vaporPressure = (humidity / 100) * 6.112 * Math.exp(exponentIce);
} else {
let exponentWater = aWater * temperatureCelsius / (bWater + temperatureCelsius);
vaporPressure = (humidity / 100) * 6.112 * Math.exp(exponentWater);
}
return vaporPressure;
}
function runScriptPeriodically() {
console.log("Executing periodic script");
statusWindowCover(function (error, state) {
if (error) {
// Handle the error
} else {
getInternalHumidity(function (error, internalHumidity) {
if (error) {
// Handle the error
} else {
getInternalTemperature(function (error, internalTemperature) {
if (error) {
// Handle the error
} else {
getWeatherData(function (temperatureCelsius, humidity, windSpeed, perception) {
let internalVaporPressure = calculateWaterVaporPressure(internalHumidity, internalTemperature);
let externalVaporPressure = calculateWaterVaporPressure(humidity, temperatureCelsius);
console.log("Internal water vapor pressure =", internalVaporPressure, "hPa");
console.log("External water vapor pressure =", externalVaporPressure, "hPa");
if (windSpeed < CONFIG.windThreshold && perception === null) {
console.log("Wind speed is below the threshold and no precipitation is present. Checking opening conditions...");
if (internalHumidity > CONFIG.internalHumidityThreshold && internalVaporPressure > (1 + CONFIG.relativeActivationWaterVaporPressure) * externalVaporPressure) {
console.log("Internal water vapor pressure is above the threshold and sufficiently larger than the external water vapor pressure.");
openWindowCover(state, function (error) {
if (error) {
// Handle the error
}
});
} else {
console.log("Internal water vapor pressure is fine relative to outside.");
closeWindowCover(state, function (error) {
if (error) {
// Handle the error
}
});
}
} else {
console.log("Closing Window due to strong winds or precipitation.");
closeWindowCover(state, function (error) {
if (error) {
// Handle the error
}
});
}
});
}
});
}
});
}
});
}
// Start the script and schedule it to run periodically
runScriptPeriodically();
Alles anzeigen
Ausgabe sieht wie folgt aus:
Code
Executing periodic script
Cover Status: closed
Internal Humidity = 74.3 %
Internal Temperature = 14.3 °C
API Request: [{"LocalObservationDateTime":"2023-10-24T18:13:00+02:00","EpochTime":1698163980,"WeatherText":"Sunny","WeatherIcon":1,"HasPrecipitation":false,"PrecipitationType":null,"IsDayTime":true,"Temperature":{"Metric":{"Value":13.7,"Unit":"C","UnitTy ... und so weiter
danach stoppt das Skript.
Die Diagonstics gibt einen "OUT OF MEMORY" Fehler aus. Wie ist damit umzugehen?