Created
October 27, 2020 17:00
-
-
Save gersilex/7905106c199bd854f5709304041e0eed to your computer and use it in GitHub Desktop.
Tasmota-based virtual sunrise wakeup lights for openHAB (Rules DSL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val Number wakeupDurationSeconds = 3000 | |
val Number finishedStayOnDurationSeconds = 0 | |
var Timer wakeupTimer = null | |
rule "Alarm at 0 should be UNDEF" | |
when Member of alarm_clocks changed | |
then | |
if(triggeringItem.state == 0) triggeringItem.postUpdate(UNDEF) | |
end | |
rule "Update Wakeup Timer" | |
when Item alarm_clocks changed | |
or Item sleep_wakeup_lights_enabled changed to OFF | |
then | |
if(triggeringItem !== null){ | |
postUpdate( | |
triggeringItem.name + '_datetime', | |
(new DateTimeType(new DateTime((triggeringItem.state as DecimalType).longValue).toString).toString) | |
) | |
} | |
if(wakeupTimer !== null){ | |
logInfo('wakeup', 'Deleting timer ' + wakeupTimer.toString) | |
wakeupTimer.cancel() | |
wakeupTimer = null | |
} | |
val Number startingTimeMillis = alarm_clocks.state as Number - (wakeupDurationSeconds * 1000) - (finishedStayOnDurationSeconds * 1000) | |
val DateTime startingTime = new DateTime(startingTimeMillis.longValue) | |
logInfo('wakeup', 'Next Wakeup Timer:') | |
logInfo('wakeup', 'Starting at ' + startingTime.toString) | |
logInfo('wakeup', 'Fully on at ' + new DateTime((alarm_clocks.state as DecimalType).longValue).minusSeconds(finishedStayOnDurationSeconds.intValue).toString) | |
logInfo('wakeup', 'Finishing at ' + new DateTime((alarm_clocks.state as DecimalType).longValue).toString) | |
if(startingTime.isBeforeNow()){ | |
logWarn('wakeup', 'Start time is before now. Not creating timer and not starting wakeup sequence.') | |
return; | |
} | |
if(startingTime.getHourOfDay > 11){ | |
logWarn('wakeup', 'Start time is after 11:00. Not creating timer and not starting wakeup sequence because the alarm is not set to wake up in the morning.') | |
return; | |
} | |
if(sleep_wakeup_lights_enabled.state == OFF){ | |
logWarn('wakeup', 'Functionality disabled by setting. Not creating timer and not starting wakeup sequence.') | |
return; | |
} | |
wakeupTimer = createTimer(startingTime)[| | |
if(outside_darkness.state != ON){ | |
logInfo('wakeup', 'Not dark outside. Ignoring.') | |
return; | |
} | |
logInfo('wakeup', 'Starting wakeup sequence') | |
sendNotification('[email protected]', 'Starting wakeup sequence for alarm at ' + new DateTime((alarm_clocks.state as DecimalType).longValue).toString) | |
sleep_wakeup_lights_state.postUpdate("LIGHTS_WAKEUP") | |
sleep_wakeup_lights_backlog.sendCommand( | |
"Power OFF;" | |
+ "SetOption20 1;" // Update of Dimmer/Color/CT without turning power on | |
+ "Color ffff00ff;" | |
+ "Dimmer 100;" | |
+ "WakeupDuration " + wakeupDurationSeconds.toString + ";" | |
+ "Scheme 1;" | |
+ "SetOption20 0;" | |
) | |
createTimer(now.plusSeconds(5))[| | |
if(sleep_wakeup_lights_power.state != ON){ | |
logWarn('wakeup', 'Sent Backlog commands, but POWER not on: ' + sleep_wakeup_lights_power.toString) | |
logWarn('wakeup', 'Restarting timer ' + wakeupTimer.toString) | |
wakeupTimer.reschedule(now.plusSeconds(5)) | |
} | |
] | |
] | |
logInfo('wakeup', 'Created timer ' + wakeupTimer.toString) | |
end | |
rule "Leave Wakeup functionality when getting bright outside" | |
when Item outside_darkness changed to OFF | |
or Item sleep_wakeup_lights_enabled changed to OFF | |
then | |
sleep_wakeup_lights_state.postUpdate(UNDEF) | |
end | |
rule "Turn off LEDs when leaving wakeup or standup states" | |
when Item sleep_wakeup_lights_state changed from LIGHTS_WAKEUP to UNDEF | |
or Item sleep_wakeup_lights_state changed from LIGHTS_STANDUP to UNDEF | |
then | |
sleep_wakeup_lights_power.sendCommand(OFF) | |
end | |
rule "Wakeup Light running presence indicator" | |
when Item sleep_wakeup_lights_state changed | |
then | |
sleep_wakeup_lights_wakeup_running.sendCommand(if(sleep_wakeup_lights_state.state == "LIGHTS_WAKEUP") ON else OFF) | |
end | |
rule "Turn off sleeping lights at midnight" | |
when Time is midnight | |
then | |
sleep_wakeup_lights_backlog.sendCommand( | |
"" | |
+ "Speed 40;" | |
+ "Color ff000000;" | |
+ "Delay 300;" | |
+ "Power OFF;" | |
) | |
end | |
rule "default color" | |
when Item sleep_wakeup_lights_hsbcolor received command ON | |
then | |
sleep_wakeup_lights_hsbcolor.sendCommand(new HSBType("30,77,100") as Number) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
😍