Ever since I got some Google Home’s, I felt the urge to ask Google to turn on a specific lamp for a certain amount of time, like “OK Google, Turn on the shed light for 10 minutes”, which would give me plenty of time to walk to the shed in the back of the garden, get my bike, get out of the garden and take off, without having to do that in complete darkness of while leaving on the lights until I got back home or remembered to turn off the lights again via the app upon arriving at my destination. Although this example might also be solved by other methods, I figured there might be a general use for this, and as the Google Home does not support this behaviour, I figured I would make a way around that.
Now lets assume that I have a light that is called “shed lamp”, with the main switch item called “Shedlamp_Switch”, which I have defined in for example Habmin or Paper UI. What I did next, is that I made the following items:
Switch ShedScene5 "Shed lamp for 5 minutes" [ "Switchable" ]
Switch ShedScene10 "Shed lamp for 10 minutes" [ "Switchable" ]
Switch ShedScene20 "Shed lamp for 20 minutes" [ "Switchable" ]
With this label exposed to the Google Home, and using the structure “Hey Google, Turn on/off X”, you could make Google flip the switches above, like “Hey Google, Turn on shed lamp for 10 minutes.”
These switches subsequently trigger the following rule:
var Timer ShedTimer = null
rule "Shed lamp timer activated"
when
Item ShedScene5 received command or
Item ShedScene10 received command or
Item ShedScene20 received command
then
logInfo("Shedlamp.Timers", "Shedlamp timer activated by " + triggeringItem.name + ", is set to " + receivedCommand + " for " + triggeringItem.name.substring("ShedScene".length, triggeringItem.name.length) + " minutes")
Shedlamp_Switch.sendCommand(receivedCommand)
if(ShedTimer !== null){
ShedTimer.cancel()
}
ShedTimer = createTimer(now.plusMinutes(Integer::parseInt(triggeringItem.name.substring("ShedScene".length, triggeringItem.name.length))), [|
logInfo("Shedlamp.Timers", "Shedlamp timer expired, now turning " + if(receivedCommand==ON) "OFF" else "ON")
Shedlamp_Switch.sendCommand(if(receivedCommand==ON) OFF else ON)
triggeringItem.postUpdate(if(receivedCommand==ON) OFF else ON)
])
end
Similarly, you could make items that switch on/off the light in x minutes time with an item definition like so:
Switch ShedSceneIn10 "Shed lamp in 10 minutes" [ "Switchable" ]
And a rule definition like so:
rule "Shedlamp timer activated 2"
when
Item ShedSceneIn10 received command
then
logInfo("Shedlamp.Timers", "Shedlamptimer activated by " + triggeringItem.name + ", is set to " + receivedCommand + " in " + triggeringItem.name.substring("ShedSceneIn".length, triggeringItem.name.length) + " minutes")
if(ShedTimer !== null){
ShedTimer.cancel()
}
ShedTimer = createTimer(now.plusMinutes(Integer::parseInt(triggeringItem.name.substring("ShedSceneIn".length, triggeringItem.name.length))), [|
logInfo("Shedlamp.Timers", "Shedlamp timer expired, now turning " + receivedCommand.toString)
Shedlamp_Switch.sendCommand(receivedCommand)
])
end
Personally I also like to be able to cancel the timer without adding extra items, for that I added this rule, so that I can just say “Turn off shed lamp”, and regardless of whether the lamp is on or off, this will cancel a timer that would change the state of the lamp:
rule "Shedlamp timer overrule"
when
Item Shedlamp_Switch received command
then
if(ShedTimer !== null){
ShedTimer.cancel()
}
end
Of course it is essential for this to work, to make it work with whatever language you are speaking, in my own language, Dutch, a grammatically more sensible way would be to say “Turn lamp x in y minutes off” or “Turn lamp x y minutes on” to turn the lamp off in a few minutes or to turn the lamp on for a few minutes, respectively.
I hope this helps some people to add some functionality to their Google Home or comparable systems, and any feedback is always appreciated.