Hello
I would like to share a solution that I made in my Openhab and at the same time to discuss whether anyone can think of how to improve the solution.
Openhab is synchronized with the calendar. In the calendar I write planned activities such as vacantion, holiday, visit, home alone, etc. Using the CalDav addon - Openhab detects when an event started and ended, and sets the switch for that event. Subsequently, Openhab adapts the operation of the automatic household on the basis of a switch.
For example:
String VacantionEventName “Event: [%s]” { caldavPersonal=“calendar:program type:ACTIVE eventNr:1 value:NAME filter-name:‘Vacantion’”}
Switch Vacantion “Event Vacantion: [%s]”
rule "Change Vacantion"
when
System started or
Item VacantionEventName changed
then
if (VacantionEventName.state == NULL) {
// nothing
}
if (VacantionEventName.state == UNDEF) {
sendCommand(Vacantion, OFF)
} else
if (VacantionEventName.state.toString.length > 0) {
sendCommand(Vacantion, ON)
} else {
sendCommand(Vacantion, OFF)
}
end
This basic setting works nicely. My next request is that I would also like to set up unscheduled activities without setting up a calendar, but with just one click on the screen that the event has occurred / ended. That’s why I made another Item:
Switch VacantionManual “Set event Vacantion: [%s]”
And it extended the previous rule that when sendCommand(Vacantion,OFF/ON), it also executes sendCommand(VacantionManual,OFF/ON) to keep the states synchronized.
Then I added the rule that when I change a manual event, an calendar event will be created or expired according to the circumstances of the event, which will be set by the resulting Vacantion switch.
rule "Change Vacantion Manual"
when
Item VacantionManual changed
then
if (VacantionManual.state == ON) {
if (VacantionEventName.state == UNDEF) {
// create Event in calendar
val results = executeCommandLine("sudo@@/home/openhabian/createEvent.sh@@program@@Vacantion", 15000)
logInfo("createEvent", results)
}
} else {
if (VacantionEventName.state.toString.length > 0) {
// end Event in calendar
val results = executeCommandLine("sudo@@/home/openhabian/endEvent.sh@@program@@Vacantion", 15000)
logInfo("endEvent", results)
}
}
end
The scripts: createEvent.sh and endEvent.sh ensure the creation or termination of the calendar event. I implemented them as a bash shell by calling the CalDav API using the command curl. I have used Baikal as a server.
If I am interested, I can provide them, I am not a Linux programmer, so maybe some things could be written more optimally.
One more caveat: Item VacantionEventName must be saved, as it will result in an unwanted cancellation of a running event during a restart.