Turn on light, based on Time of day

I would really like if someone could explain to me how to get openhab to know the time of day, (e.g 10am, 11am, 1pm, 2pm) and based on the time, do an action, such as turn on a light if it is 9pm.

1 Like

item:
Switch Testlight "Testlight" { <your hardware binding goes here> }

sitemap:

Switch item=Testlight

rule:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.java.math.*
import org.joda.time.*

rule "turn Testlight on"
when   
Time cron "0 0 9 ? * *"
then
Testlight.sendCommand(ON)
end

rule "turn Testlight off"
when   
Time cron "0 15 9 ? * *"
then
Testlight.sendCommand(OFF)
end

This is a “quick and dirty” solution, turns light on at 9AM and turns light off at 09:15 AM.
Take a look at the demo rules to suite your advanced needs for turning lights on and off (and do a lot of other cool stuff)

Thank You, is there a way to disable the switch in the UI, so the user can’t turn of the switch during that time?

Yes, take a look at the visibility section in the sitemaps wiki.

Edit: this should work:

Switch item=Testlight visibility=[Testlight!=ON]

So, I can only change it’s visibility? Is there anyway to sort of “grey it out” kind of like a disabled button in a windows form?

And would you recommend using the GCAL google calendar binding, if I wanted to setup a schedule to turn on and off this light?

[quote=“AzureCoder, post:6, topic:5288, full:true”]
And would you recommend using the GCAL google calendar binding[/quote]

I don’t use that, try it out and write down your experience.

Good luck.

Thank You

You might be interested in this thread.

It shows how to use the Astro binding along with cron triggers to control lighting and other Items that change how they work based on the time of day.

You cannot really gray it out. The best you could do is have two versions of it, one that is a switch and is visible when the switch should be enabled and another that is a text item configured to use gray text that is visible when the switch should be disabled.

Switch item=TestLight visibility=[TestLightControl!=ON]
Text item=TestLight labelcolor=[TestLightControl==OFF="gray"] visibility=[Testlight==ON]
1 Like