From 8d74d6090811b61c9fd64751cec5725b19dbf247 Mon Sep 17 00:00:00 2001 From: simatec Date: Wed, 7 Aug 2024 11:10:27 +0200 Subject: [PATCH] (simatec) Code revision and improvements --- lib/sunProtect.js | 162 ++++++++++++++++++++++++++++------------------ 1 file changed, 99 insertions(+), 63 deletions(-) diff --git a/lib/sunProtect.js b/lib/sunProtect.js index 670d1d4..7ba428b 100644 --- a/lib/sunProtect.js +++ b/lib/sunProtect.js @@ -42,7 +42,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { const _autoSunState = await adapter.getStateAsync(`shutters.autoSun.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoSunState && _autoSunState.val === true) { + if (_autoSunState?.val === true) { let currentValue = ''; let _triggerState; let mustValue = ''; @@ -56,7 +56,6 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; mustValue = ('' + shutterSettings[s].triggerState); mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); - currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; if ((currentValue === mustValue || @@ -101,13 +100,16 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].lightSensor != '' && shutterSettings[s].valueLight < sunLight) { - adapter.log.debug('Stopping sunprotect delay for ' + shutterSettings[s].shutterName); + adapter.log.debug(`Stopping sunprotect delay for ${shutterSettings[s].shutterName}`); clearTimeout(shutterSettings[s].sunProtectEndtimerid); shutterSettings[s].sunProtectEndtimerid = ''; } - if (currentValue === mustValue || currentValue === mustValueTilted || - (currentValue != mustValue && currentValue != mustValueTilted && shutterSettings[s].autoDrive != 'onlyUp') || + if (currentValue === mustValue || + currentValue === mustValueTilted || + (currentValue != mustValue && + currentValue != mustValueTilted && + shutterSettings[s].autoDrive != 'onlyUp') || (shutterSettings[s].triggerID == '')) { if (insideTemp > shutterSettings[s].tempInside) { @@ -125,11 +127,14 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { if (_shutterState?.val) { adapter.log.debug(shutterSettings[s].shutterName + ': Check basis for sunprotect. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' > HeightDownSun: ' + heightDownSun + ' AND Height:' + Math.round(_shutterState.val / vRound) * vRound + ' == currentHeight:' + shutterSettings[s].currentHeight + ' AND currentHeight:' + shutterSettings[s].currentHeight + ' == heightUp:' + shutterSettings[s].heightUp); - if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && (shutterSettings[s].currentHeight == shutterSettings[s].heightUp || - (shutterSettings[s].heatProtection == true && shutterSettings[s].tempHeatProtection < outsideTemp))) { + (shutterSettings[s].heatProtection == true && + shutterSettings[s].tempHeatProtection < outsideTemp))) { shutterSettings[s].currentAction = 'sunProtect'; shutterSettings[s].lastAutoAction = 'down_Sunprotect'; @@ -154,7 +159,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'OpenInSunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('Set sunprotect mode for ' + shutterSettings[s].shutterName + '. Currently closed. Set to sunprotect if shutter will be opened automatically'); } @@ -168,7 +173,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'sunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug(shutterSettings[s].shutterName + ': Shutter is in position sunProtect. Reset mode sunProtect to cancel sunProtect automatically. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' HeightDownSun:' + heightDownSun); } @@ -181,7 +186,10 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { } } } - if (currentValue != mustValue && currentValue != mustValueTilted && shutterSettings[s].driveAfterClose == true) { + if (currentValue != mustValue && + currentValue != mustValueTilted && + shutterSettings[s].driveAfterClose == true) { + if (insideTemp > shutterSettings[s].tempInside) { if (shutterSettings[s].tempOutside < outsideTemp && (shutterSettings[s].lightSensor != '' && @@ -199,7 +207,8 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && shutterSettings[s].currentHeight == shutterSettings[s].heightUp && shutterSettings[s].triggerAction != 'down' && @@ -225,7 +234,9 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { } if (currentValue === mustValue || currentValue === mustValueTilted || - (currentValue != mustValue && currentValue != mustValueTilted && shutterSettings[s].autoDrive != 'onlyDown') || + (currentValue != mustValue && + currentValue != mustValueTilted && + shutterSettings[s].autoDrive != 'onlyDown') || (shutterSettings[s].triggerID == '')) { const hysteresisOutside = (((100 - shutterSettings[s].hysteresisOutside) / 100) * shutterSettings[s].tempOutside).toFixed(2); @@ -235,18 +246,25 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { if (shutterSettings[s].sunProtectEndtimerid === '' && shutterSettings[s].lightSensor != '' && parseFloat(hysteresisLight) > sunLight && - (shutterSettings[s].currentAction == 'sunProtect' || (shutterSettings[s].currentAction == 'triggered' && shutterSettings[s].lastAutoAction == 'down_Sunprotect')) && + (shutterSettings[s].currentAction == 'sunProtect' || + (shutterSettings[s].currentAction == 'triggered' && + shutterSettings[s].lastAutoAction == 'down_Sunprotect')) && shutterSettings[s].KeepSunProtect === false) { adapter.log.debug(`#1 Started sunprotect end delay for ${shutterSettings[s].shutterName} with ${shutterSettings[s].sunProtectEndDely} minutes`); + shutterSettings[s].sunProtectEndtimerid = setTimeout(async function () { adapter.log.debug(`#1.1 End sunprotect delay for ${shutterSettings[s].shutterName}`); shutterSettings[s].sunProtectEndtimerid = '0'; }, shutterSettings[s].sunProtectEndDely * 60000, s); } if (insideTemp < parseFloat(hysteresisInside) || - (parseFloat(hysteresisOutside) > outsideTemp || shutterSettings[s].lightSensor != '' && parseFloat(hysteresisLight) > sunLight && shutterSettings[s].sunProtectEndtimerid === '0') || - (parseFloat(hysteresisOutside) > outsideTemp && shutterSettings[s].lightSensor == '')) { + (parseFloat(hysteresisOutside) > outsideTemp || + shutterSettings[s].lightSensor != '' && + parseFloat(hysteresisLight) > sunLight && + shutterSettings[s].sunProtectEndtimerid === '0') || + (parseFloat(hysteresisOutside) > outsideTemp && + shutterSettings[s].lightSensor == '')) { if (pendingAlarm == false) { const _shutterState = await adapter.getForeignStateAsync(shutterSettings[s].name).catch((e) => adapter.log.warn(e)); @@ -273,7 +291,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].sunProtectEndtimerid = '' shutterSettings[s].currentAction = 'none'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('OpenInSunProtect for ' + shutterSettings[s].shutterName + ' is not longer active'); } @@ -334,6 +352,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { adapter.log.debug('Temperature inside: ' + insideTemp + ' < ' + hysteresisInside + ' OR ( Temperature outside: ' + outsideTemp + ' < ' + hysteresisOutside + ' OR Light: ' + sunLight + ' < ' + hysteresisLight + ' )'); adapter.log.debug('save new trigger height: ' + shutterSettings[s].triggerHeight + '%'); adapter.log.debug('save new trigger action: ' + shutterSettings[s].triggerAction); + } else if (shutterSettings[s].triggerAction == 'sunProtect' && shutterSettings[s].KeepSunProtect === false && (parseFloat(shutterSettings[s].triggerHeight) == parseFloat(shutterSettings[s].heightUp) || @@ -449,8 +468,10 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { if (_shutterState?.val) { adapter.log.debug(shutterSettings[s].shutterName + ': Check basis for sunprotect. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' > HeightDownSun: ' + heightDownSun + ' AND Height:' + Math.round(_shutterState.val / vRound) * vRound + ' == currentHeight:' + shutterSettings[s].currentHeight + ' AND currentHeight:' + shutterSettings[s].currentHeight + ' == heightUp:' + shutterSettings[s].heightUp); - if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && (shutterSettings[s].currentHeight == shutterSettings[s].heightUp || (shutterSettings[s].heatProtection == true && @@ -491,7 +512,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'sunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug(shutterSettings[s].shutterName + ': Shutter is in position sunProtect. Reset mode sunProtect to cancel sunProtect automatically. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' HeightDownSun:' + heightDownSun); } @@ -525,8 +546,11 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { if (_shutterState?.val) { adapter.log.debug(shutterSettings[s].shutterName + ': Check basis for sunprotect. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' > HeightDownSun: ' + heightDownSun + ' AND Height:' + Math.round(_shutterState.val / vRound) * vRound + ' == currentHeight:' + shutterSettings[s].currentHeight + ' AND currentHeight:' + shutterSettings[s].currentHeight + ' == heightUp:' + shutterSettings[s].heightUp + ' AND triggerAction:' + shutterSettings[s].triggerAction + ' != down '); - if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + + if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && shutterSettings[s].currentHeight == shutterSettings[s].heightUp && shutterSettings[s].triggerAction != 'down' && @@ -611,7 +635,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].sunProtectEndtimerid = '' shutterSettings[s].currentAction = 'none'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('OpenInSunProtect for ' + shutterSettings[s].shutterName + ' is not longer active'); } @@ -642,6 +666,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].KeepSunProtect === false) { adapter.log.debug(`#4 Started sunprotect end delay for ${shutterSettings[s].shutterName} with ${shutterSettings[s].sunProtectEndDely} minutes`); + shutterSettings[s].sunProtectEndtimerid = setTimeout(async function () { adapter.log.debug(`#4.1 End sunprotect delay for ${shutterSettings[s].shutterName}`); shutterSettings[s].sunProtectEndtimerid = '0'; @@ -674,6 +699,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { adapter.log.debug('Temperature inside: ' + insideTemp + ' < ' + hysteresisInside + ' OR ( Temperature outside: ' + outsideTemp + ' < ' + hysteresisOutside + ' OR Light: ' + sunLight + ' < ' + hysteresisLight + ' )'); adapter.log.debug('save new trigger action: ' + shutterSettings[s].triggerAction); adapter.log.debug('save new trigger height: ' + shutterSettings[s].triggerHeight + '%'); + } else if (shutterSettings[s].triggerAction == 'sunProtect' && shutterSettings[s].KeepSunProtect === false && (parseFloat(shutterSettings[s].triggerHeight) == parseFloat(shutterSettings[s].heightUp) || @@ -716,7 +742,6 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; mustValue = ('' + shutterSettings[s].triggerState); mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); - currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; if (currentValue === mustValue || @@ -779,8 +804,10 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { if (_shutterState?.val) { adapter.log.debug(shutterSettings[s].shutterName + ': Check basis for sunprotect. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' > HeightDownSun: ' + heightDownSun + ' AND Height:' + Math.round(_shutterState.val / vRound) * vRound + ' == currentHeight:' + shutterSettings[s].currentHeight + ' AND currentHeight:' + shutterSettings[s].currentHeight + ' == heightUp:' + shutterSettings[s].heightUp); - if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && (shutterSettings[s].currentHeight == shutterSettings[s].heightUp || (shutterSettings[s].heatProtection == true && @@ -809,7 +836,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'OpenInSunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('Set sunprotect mode for ' + shutterSettings[s].shutterName + '. Currently closed. Set to sunprotect if shutter will be opened automatically'); } @@ -823,7 +850,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'sunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug(shutterSettings[s].shutterName + ': Shutter is in position sunProtect. Reset mode sunProtect to cancel sunProtect automatically. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' HeightDownSun:' + heightDownSun); } @@ -856,8 +883,10 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { if (_shutterState?.val) { adapter.log.debug(shutterSettings[s].shutterName + ': Check basis for sunprotect. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' > HeightDownSun: ' + heightDownSun + ' AND Height:' + Math.round(_shutterState.val / vRound) * vRound + ' == currentHeight:' + shutterSettings[s].currentHeight + ' AND currentHeight:' + shutterSettings[s].currentHeight + ' == heightUp:' + shutterSettings[s].heightUp); - if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && shutterSettings[s].currentHeight == shutterSettings[s].heightUp && shutterSettings[s].triggerAction != 'down' && @@ -941,7 +970,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { else if (shutterSettings[s].currentAction == 'OpenInSunProtect') { shutterSettings[s].currentAction = 'none'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('OpenInSunProtect for ' + shutterSettings[s].shutterName + ' is not longer active'); } @@ -1044,7 +1073,6 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; mustValue = ('' + shutterSettings[s].triggerState); mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); - currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; if (currentValue === mustValue || @@ -1073,8 +1101,10 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { adapter.log.debug(shutterSettings[s].shutterName + ': Check basis for sunprotect. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' > HeightDownSun: ' + heightDownSun + ' AND Height:' + Math.round(_shutterState.val / vRound) * vRound + ' == currentHeight:' + shutterSettings[s].currentHeight + ' AND currentHeight:' + shutterSettings[s].currentHeight + ' == heightUp:' + shutterSettings[s].heightUp); - if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && shutterSettings[s].currentHeight == shutterSettings[s].heightUp) { @@ -1101,7 +1131,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'OpenInSunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('Set sunprotect mode for ' + shutterSettings[s].shutterName + '. Currently closed. Set to sunprotect if shutter will be opened automatically'); } @@ -1115,7 +1145,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'sunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug(shutterSettings[s].shutterName + ': Shutter is in position sunProtect. Reset mode sunProtect to cancel sunProtect automatically. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' HeightDownSun:' + heightDownSun); } @@ -1173,7 +1203,8 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { } if (currentValue === mustValue || currentValue === mustValueTilted || - (currentValue != mustValue && currentValue != mustValueTilted && + (currentValue != mustValue && + currentValue != mustValueTilted && shutterSettings[s].autoDrive != 'onlyDown') || (shutterSettings[s].triggerID == '')) { @@ -1202,7 +1233,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { else if (shutterSettings[s].currentAction == 'OpenInSunProtect') { shutterSettings[s].currentAction = 'none'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('OpenInSunProtect for ' + shutterSettings[s].shutterName + ' is not longer active'); } @@ -1278,7 +1309,6 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; mustValue = ('' + shutterSettings[s].triggerState); mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); - currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; if (currentValue === mustValue || @@ -1339,8 +1369,10 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { if (_shutterState?.val) { adapter.log.debug(shutterSettings[s].shutterName + ': Check basis for sunprotect. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' > HeightDownSun: ' + heightDownSun + ' AND Height:' + Math.round(_shutterState.val / vRound) * vRound + ' == currentHeight:' + shutterSettings[s].currentHeight + ' AND currentHeight:' + shutterSettings[s].currentHeight + ' == heightUp:' + shutterSettings[s].heightUp); - if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && (shutterSettings[s].currentHeight == shutterSettings[s].heightUp || (shutterSettings[s].heatProtection == true && @@ -1369,7 +1401,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'OpenInSunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('Set sunprotect mode for ' + shutterSettings[s].shutterName + '. Currently closed. Set to sunprotect if shutter will be opened automatically'); } @@ -1383,7 +1415,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'sunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug(shutterSettings[s].shutterName + ': Shutter is in position sunProtect. Reset mode sunProtect to cancel sunProtect automatically. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' HeightDownSun:' + heightDownSun); } @@ -1414,8 +1446,10 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { if (_shutterState?.val) { adapter.log.debug(shutterSettings[s].shutterName + ': Check basis for sunprotect. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' > HeightDownSun: ' + heightDownSun + ' AND Height:' + Math.round(_shutterState.val / vRound) * vRound + ' == currentHeight:' + shutterSettings[s].currentHeight + ' AND currentHeight:' + shutterSettings[s].currentHeight + ' == heightUp:' + shutterSettings[s].heightUp); - if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && shutterSettings[s].currentHeight == shutterSettings[s].heightUp && shutterSettings[s].triggerAction != 'down' && @@ -1473,7 +1507,11 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { const _shutterState = await adapter.getForeignStateAsync(shutterSettings[s].name).catch((e) => adapter.log.warn(e)); if (_shutterState?.val) { - if (shutterSettings[s].currentAction == 'sunProtect' && shutterSettings[s].KeepSunProtect === false && (Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(heightDownSun) || Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight))) { + if (shutterSettings[s].currentAction == 'sunProtect' && + shutterSettings[s].KeepSunProtect === false && + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(heightDownSun) || + Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight))) { + shutterSettings[s].sunProtectEndtimerid = ''; shutterSettings[s].currentAction = 'up'; shutterSettings[s].currentHeight = shutterSettings[s].heightUp; @@ -1490,7 +1528,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].sunProtectEndtimerid = '' shutterSettings[s].currentAction = 'none'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('OpenInSunProtect for ' + shutterSettings[s].shutterName + ' is not longer active'); } @@ -1590,7 +1628,6 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; mustValue = ('' + shutterSettings[s].triggerState); mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); - currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; if ((currentValue === mustValue || @@ -1624,8 +1661,10 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { const _shutterState = await adapter.getForeignStateAsync(shutterSettings[s].name).catch((e) => adapter.log.warn(e)); if (_shutterState?.val) { - if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && shutterSettings[s].currentHeight == shutterSettings[s].heightUp) { @@ -1651,7 +1690,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'OpenInSunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('Set sunprotect mode for ' + shutterSettings[s].shutterName + '. Currently closed. Set to sunprotect if shutter will be opened automatically'); } @@ -1665,7 +1704,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { shutterSettings[s].currentAction = 'sunProtect'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug(shutterSettings[s].shutterName + ': Shutter is in position sunProtect. Reset mode sunProtect to cancel sunProtect automatically. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' HeightDownSun:' + heightDownSun); } @@ -1694,8 +1733,10 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { adapter.log.debug(shutterSettings[s].shutterName + ': Check basis for sunprotect. Height:' + Math.round(_shutterState.val / vRound) * vRound + ' > HeightDownSun: ' + heightDownSun + ' AND Height:' + Math.round(_shutterState.val / vRound) * vRound + ' == currentHeight:' + shutterSettings[s].currentHeight + ' AND currentHeight:' + shutterSettings[s].currentHeight + ' == heightUp:' + shutterSettings[s].heightUp); - if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && convertShutter == true)) && + if (((Math.round(parseFloat(_shutterState.val) / vRound) * vRound > parseFloat(heightDownSun) && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / vRound) * vRound < parseFloat(heightDownSun) && + convertShutter == true)) && Math.round(parseFloat(_shutterState.val) / vRound) * vRound == parseFloat(shutterSettings[s].currentHeight) && shutterSettings[s].currentHeight == shutterSettings[s].heightUp && shutterSettings[s].triggerAction != 'down' && @@ -1751,7 +1792,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { else if (shutterSettings[s].currentAction == 'OpenInSunProtect') { shutterSettings[s].currentAction = 'none'; - await adapter.setStateAsync('shutters.autoState.' + nameDevice, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); + await adapter.setStateAsync(`shutters.autoState.${nameDevice}`, { val: shutterSettings[s].currentAction, ack: true }).catch((e) => adapter.log.warn(e)); adapter.log.debug('OpenInSunProtect for ' + shutterSettings[s].shutterName + ' is not longer active'); } @@ -1790,6 +1831,7 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { adapter.log.debug('save new trigger height: ' + shutterSettings[s].triggerHeight + '%'); adapter.log.debug('Sunprotect for ' + shutterSettings[s].shutterName + ' is not active anymore'); adapter.log.debug('save new trigger action: ' + shutterSettings[s].triggerAction); + } else if (shutterSettings[s].triggerAction == 'sunProtect' && shutterSettings[s].KeepSunProtect === false && (parseFloat(shutterSettings[s].triggerHeight) == parseFloat(shutterSettings[s].heightUp) || @@ -1867,17 +1909,11 @@ async function sunProtect(adapter, elevation, azimuth, shutterSettings) { const _autoSunState = await adapter.getStateAsync(`shutters.autoSun.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoSunState && _autoSunState.val === true) { - let currentValue = ''; - + if (_autoSunState?.val === true) { const _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; - const mustValue = ('' + shutterSettings[s].triggerState); const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); - - if (typeof _triggerState != undefined && _triggerState != null && _triggerState.val != undefined) { - currentValue = ('' + _triggerState.val); - } + const currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; if ((currentValue === mustValue || currentValue === mustValueTilted) ||