@haus-automatisierung
const suncalc = require('suncalc');
const config = getObject('system.config');
const lat = config.common.latitude;
const long = config.common.longitude;
const weatherHorizon = 3;
var ostSchrittId = 1;
var suedSchrittId = 1;
var westSchrittId = 1;
let schedules = {};
schedule('0 2 * * *', async function () {
const list = getSchedules(false);
telegramMike("Suncalc_v2 Timer aktiv: " + list.length)
checkSuncalc();
const list1 = getSchedules(false);
telegramMike("Suncalc_v2 Timer aktiv: " + list1.length)
});
on({id: '0_userdata.0.script.Trigger.Suncalc', change: "ne", val: true}, function (obj) {
setState(obj.id, false);
log("Manuell getriggert");
checkSuncalc()
const list = getSchedules(false);
list.forEach(schedule => log(JSON.stringify(schedule)));
telegramMike("Suncalc_v2 Timer aktiv: " + list.length)
});
function checkSuncalc() {
var times = suncalc.getTimes(new Date(), lat, long);
var startTime = new Date(times.sunrise.getFullYear(), times.sunrise.getMonth(), times.sunrise.getDate(), times.sunrise.getHours(), 0);
var endTime = new Date(times.sunset.getFullYear(), times.sunset.getMonth(), times.sunset.getDate(), times.sunset.getHours()+4, 0);
log(startTime + " bis " + endTime)
var checkTime = startTime.getTime();
var test = 0
ostSchrittId = 1;
suedSchrittId = 1;
westSchrittId = 1;
while(checkTime < endTime.getTime()) {
// Suncalc
checkTime = checkTime+2*60*1000;
var dateTime = new Date(checkTime);
var sunpos = suncalc.getPosition(checkTime, lat, long);
var h = sunpos.altitude * 180 / Math.PI; //elevation
var a = sunpos.azimuth * 180 / Math.PI + 180; //azimut
var azimut = Math.round(10 * a) / 10
var elevation = Math.round(10 * h) / 10
// Twilight
var twi = Math.round(((h-weatherHorizon+12)/18*1000)/10);
if (twi > 100) twi = 100;
if (twi < 0) twi = 0;
// Output
//log(twi + " - " + azimut + " - " + elevation)
// Check Automatik Sonnenschutz
ablaufOst(dateTime, azimut, elevation);
ablaufSued(dateTime, azimut, elevation);
ablaufWest(dateTime, azimut, elevation);
if (suedSchrittId == 5 && westSchrittId == 5 && ostSchrittId == 5) break;
test++;
};
log (test + " Durchläufe")
//https://forum.iobroker.net/topic/24683/variable-in-scheduler-nutzen/2
};
/**
* Ablauf Ostseite
* @param {number} azimut
* @param {number} elevation
* @param {Date} date
*/
function ablaufOst(date, azimut, elevation) {
switch(ostSchrittId) {
case 1: // Osten OG beschatten
if (azimut < 130 && elevation > 5) { // MUSS NOCH ANGEPASST WERDEN
ostSchrittId = 2;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatikost').val) {
log(date + " Osten OG Sonnenschutz Aktivieren", "info");
const name = "ostOgZu";
if (schedules[name]) clearSchedule(schedules[name]);
if (new Date() < date) {
schedules[name] = schedule(date.getMinutes() + " " + date.getHours() + " * * *",function() {
telegramMike(name);
clearSchedule(schedules[name]);
});
const nameExt = "ostOgZuExt";
const dateTimeExt = new Date(date.getTime()-60*60*1000);
log(dateTimeExt + " Osten OG Sonnenschutz Erweitert Aktivieren ", "info");
if (schedules[nameExt]) clearSchedule(schedules[nameExt]);
if (new Date() < dateTimeExt) {
schedules[name] = schedule(dateTimeExt.getMinutes() + " " + dateTimeExt.getHours() + " * * *", function() {
telegramMike(nameExt);
clearSchedule(schedules[name]);
});
}; // Eventuell dann Ext Aktivieren?
};
};
};
break;
case 2: // Osten OG öffnen
if (azimut > 130 || elevation > 60) { // MUSS NOCH ANGEPASST WERDEN
ostSchrittId = 5;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatikost').val) {
log(date + " Osten OG Sonnenschutz Deaktivieren", "info");
const name = "ostOgAuf";
if (schedules[name]) {
log(name + " gelöscht")
clearSchedule(schedules[name]);
}
if (new Date() < date) {
schedules[name] = schedule(date.getMinutes() + " " + date.getHours() + " * * *",function() {
telegramMike(name);
clearSchedule(schedules[name]);
});
};
};
};
break;
case 5: // Schon fertig!
break;
default:
//log("Sonnenautomatik SchrittId West unbekannt!!", "error");
break;
};
}
/**
* Ablauf Südseite
* @param {number} azimut
* @param {number} elevation
* @param {Date} date
*/
function ablaufSued(date, azimut, elevation) {
switch(suedSchrittId) {
case 1: // Süden EG beschatten
if (azimut > 92) {
suedSchrittId = 2;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatiksued').val) {
log(date + " Süden EG Sonnenschutz Aktivieren", "info");
const name = "suedEgZu";
if (schedules[name]) clearSchedule(schedules[name]);
if (new Date() < date) {
schedules[name] = schedule(date.getMinutes() + " " + date.getHours() + " * * *",function() {
telegramMike(name);
clearSchedule(schedules[name]);
});
const nameExt = "suedEgZuExt";
const dateTimeExt = new Date(date.getTime()-60*60*1000);
log(dateTimeExt + " Süden EG Sonnenschutz Erweitert Aktivieren ", "info");
if (new Date() < dateTimeExt) {
schedules[nameExt] = schedule(dateTimeExt.getMinutes() + " " + dateTimeExt.getHours() + " * * *", function() {
telegramMike(nameExt);
clearSchedule(schedules[nameExt]);
});
}; // Eventuell dann Ext Aktivieren?
};
};
};
break;
case 2: // Süden OG beschatten
if (azimut > 110 && elevation < 50) {
suedSchrittId = 3;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatiksued').val) {
log(date + " Süden OG Sonnenschutz Aktivieren", "info");
const name = "suedOgZu";
if (schedules[name]) clearSchedule(schedules[name]);
if (new Date() < date) {
schedules[name] = schedule(date.getMinutes() + " " + date.getHours() + " * * *",function() {
telegramMike(name);
clearSchedule(schedules[name]);
});
const nameExt = "suedOgZuExt";
const dateTimeExt = new Date(date.getTime()-60*60*1000);
log(dateTimeExt + " Süden OG Sonnenschutz Erweitert Aktivieren ", "info");
if (new Date() < dateTimeExt) {
schedules[nameExt] = schedule(dateTimeExt.getMinutes() + " " + dateTimeExt.getHours() + " * * *", function() {
telegramMike(nameExt);
clearSchedule(schedules[nameExt]);
});
}; // Eventuell dann Ext Aktivieren?
};
};
} else if (azimut > 180) {
suedSchrittId = 4;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatiksued').val) {
log(date + " Süden OG Sonnenschutz Aktivieren übersprungen", "info");
};
};
break;
case 3: // Süden OG öffnen
if ((azimut > 190 && elevation > 55) || azimut > 220) {
suedSchrittId = 4;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatiksued').val) {
log(date + " Süden OG Sonnenschutz Deaktivieren", "info");
const name = "suedOgAuf";
if (schedules[name]) clearSchedule(schedules[name]);
if (new Date() < date) {
schedules[name] = schedule(date.getMinutes() + " " + date.getHours() + " * * *",function() {
telegramMike(name);
clearSchedule(schedules[name]);
});
};
};
};
break;
case 4: // Süden EG öffnen
if (azimut > 250) {
suedSchrittId = 5;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatiksued').val) {
log(date + " Süden EG Sonnenschutz Deaktivieren", "info");
const name = "suedEgAuf";
if (schedules[name]) clearSchedule(schedules[name]);
if (new Date() < date) {
schedules[name] = schedule(date.getMinutes() + " " + date.getHours() + " * * *",function() {
telegramMike(name);
clearSchedule(schedules[name]);
});
};
};
};
break;
case 5: // Schon fertig!
break;
default:
log("Sonnenautomatik SchrittId Süd unbekannt!!", "error");
break;
};
}
/**
* Ablauf Westseite
* @param {number} azimut
* @param {number} elevation
* @param {Date} date
*/
function ablaufWest(date, azimut, elevation) {
switch(westSchrittId) {
case 1: // Westen EG beschatten
if (azimut > 202) {
westSchrittId = 2;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatikwest').val) {
log(date + " Westen EG Sonnenschutz Aktivieren", "info");
const name = "westEgZu";
if (schedules[name]) clearSchedule(schedules[name]);
if (new Date() < date) {
schedules[name] = schedule(date.getMinutes() + " " + date.getHours() + " * * *",function() {
telegramMike(name);
setState('0_userdata.0.eg.ku.fensterWest.Steuern.Sonnenschutz.Aktivieren'/*Aktivieren*/, true);
setState('s7.0.DBs.DB211.cmnd/eg/wb/rollladenwest_down'/*Cmnd/eg/wb/rollladenwest Down*/, true);
setState('s7.0.DBs.DB211.cmnd/eg/eb/rollladen_down', true);
clearSchedule(schedules[name]);
});
};
const nameExt = "westEgZuExt";
if (schedules[nameExt]) clearSchedule(schedules[nameExt]);
const dateTimeExt = new Date(date.getTime()-60*60*1000);
log(dateTimeExt + " Westen EG Sonnenschutz Erweitert Aktivieren ", "info");
if (new Date() < dateTimeExt) {
schedules[nameExt] = schedule(dateTimeExt.getMinutes() + " " + dateTimeExt.getHours() + " * * *",function() {
telegramMike(nameExt);
setState('0_userdata.0.eg.ku.fensterWest.Steuern.SonnenschutzExt.Aktivieren'/*Aktivieren*/, true);
clearSchedule(schedules[nameExt]);
});
};
};
};
break;
case 2: // Westen OG beschatten
if (azimut > 202 && elevation < 60) {
westSchrittId = 3;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatikwest').val) {
log(date + " Westen OG Sonnenschutz Aktivieren", "info");
const name = "westOgZu";
if (schedules[name]) clearSchedule(schedules[name]);
if (new Date() > date) log ("Schon Vorbei!")
schedules[name] = schedule(date.getMinutes() + " " + date.getHours() + " * * *",function() {
telegramMike(name)
clearSchedule(schedules[name]);
});
const nameExt = "westOgZuExt";
if (schedules[nameExt]) clearSchedule(schedules[nameExt]);
const dateTimeExt = new Date(date.getTime()-60*60*1000);
log(dateTimeExt + " Westen OG Sonnenschutz Erweitert Aktivieren ", "info");
if (new Date() > dateTimeExt) log ("Schon Vorbei!")
schedules[nameExt] = schedule(dateTimeExt.getMinutes() + " " + dateTimeExt.getHours() + " * * *", function() {
telegramMike(nameExt);
clearSchedule(schedules[nameExt]);
//setState('0_userdata.0.eg.ku.fensterWest.Steuern.SonnenschutzExt.Aktivieren'/*Aktivieren*/, true);
});
};
} else if (azimut > 275) {
westSchrittId = 3;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatikwest').val) {
log(date + " Westen OG Sonnenschutz übersprungen", "info");
};
};
break;
case 3: // Westen öffnen
if (elevation < 13) {
westSchrittId = 5;
if (getState('0_userdata.0.global.settings.rollladen.sonnenautomatikwest').val) {
log(date + " Westen Sonnenschutz Deaktivieren", "info");
const name = "westAuf";
if (schedules[name]) clearSchedule(schedules[name]);
if (new Date() > date) log ("Schon Vorbei!")
schedules[name] = schedule(date.getMinutes() + " " + date.getHours() + " * * *",function() {
telegramMike(name)
setState('0_userdata.0.eg.ku.fensterWest.Steuern.Sonnenschutz.Deaktivieren', true);
clearSchedule(schedules[name]);
});
const nameExt = "westRst";
log("23:55 Westen Sonnenschutz Erweitert Deaktivieren", "info");
if (schedules[nameExt]) clearSchedule(schedules[nameExt]);
schedules[nameExt] = schedule('55 23 * * *', function() {
telegramMike(nameExt)
setState('0_userdata.0.eg.ku.fensterWest.Steuern.SonnenschutzExt.Deaktivieren', true);
clearSchedule(schedules[nameExt]);
});
};
};
break;
case 5: // Fertig
break;
default:
log("Sonnenautomatik SchrittId West unbekannt!!", "error");
break;
};
}