I’d like to dimm my lights in the bathroom automatically during nighttime. I have never worked before with rules, so I’m a bit stuck here.
What my rule looks like:
rule "Bathroom_Dimm_Nighttime"
when
Item Light_GF_Bathroom_CeilingLights_MainLight_Brightness changed from OFF to ON
then
logDebug("bathroom", "Bathroom light turned on")
var int nightTimeStart = 2300
var int nightTimeEnd = 0530
var int now = now.getHourOfDay * 100 + now.getMinuteOfHour
if (now >= nightTimeStart || now <= nightTimeEnd) {
logDebug("bathroom", "nighttime")
Light_GF_Bathroom_CeilingLights_MainLight_Brightness.sendCommand(new PercentType('33'))
} else {
logDebug("bathroom", "daytime")
Light_GF_Bathroom_CeilingLights_MainLight_Brightness.sendCommand(new PercentType('100'))
}
end
I just placed the rule in /etc/openhab2/rules/bathroom.timed.rules. Do I have to activate it somewhere? The openhab log says: [INFO ] [del.core.internal.ModelRepositoryImpl] - Loading model 'bathroom.timed.rules', so I think it is loaded and does not contain an error.
I’m a bit confused, because neither the light is dimmed nor a log output is created by the rule.
What am I doing wrong?
Platform information:
Hardware: RPi 3
OS: raspbian/debian stretch
Java Runtime Environment: which java platform is used and what version
rule "Bathroom_Dimm_Night"
when
Item Light_GF_Bathroom_CeilingLights_MainLight_Brightness changed from OFF to ON
then
logDebug("bathroom", "Bathroom light turned on")
var nightTimeStart = 2300
var nightTimeEnd = 0530
var currentTime = now.getHourOfDay * 100 + now.getMinuteOfHour
if (currentTime >= nightTimeStart || currentTime <= nightTimeEnd) {
logDebug("bathroom", "nighttime")
Light_GF_Bathroom_CeilingLights_MainLight_Brightness.sendCommand(new PercentType('33'))
} else {
logDebug("bathroom", "daytime")
Light_GF_Bathroom_CeilingLights_MainLight_Brightness.sendCommand(new PercentType('100'))
}
end
I know that the rule does not change the brightness now, but i think it should log something. I switched the lights on and off but there is no log output. Why?
11:13:59.697 [INFO ] [del.core.internal.ModelRepositoryImpl] - Refreshing model ‘bathroom.timed.rules’
11:14:55.030 [INFO ] [smarthome.event.ItemStateChangedEvent] - Light_GF_Bathroom_CeilingLights_MainLight_Brightness changed from 0 to 100
11:15:00.759 [INFO ] [smarthome.event.ItemStateChangedEvent] - Light_GF_Bathroom_CeilingLights_MainLight_Brightness changed from 100 to 0
Do you mean that my trigger is ON/OFF and the log says it changed from 0 to 100?
I took ON/OFF for some kind of placeholder for 0/1 (if there’s no dimmer) and 0/1-100 (if theres a dimmer).
How can I rewrite that? Since the light might not always be 100.
rule "Bathroom_Dimm_Night"
when
Item Light_GF_Bathroom_CeilingLights_MainLight_Brightness changed
then
if (triggeringItem.state > 0) {
logInfo("Bathroom_Dimm_Night", "Bathroom light turned on")
var nightTimeStart = 2300
var nightTimeEnd = 0530
var currentTime = now.getHourOfDay * 100 + now.getMinuteOfHour
if (currentTime >= nightTimeStart || currentTime <= nightTimeEnd) {
logDebug("Bathroom_Dimm_Night", "nighttime")
Light_GF_Bathroom_CeilingLights_MainLight_Brightness.sendCommand('33')
} else {
logDebug("Bathroom_Dimm_Night", "daytime")
Light_GF_Bathroom_CeilingLights_MainLight_Brightness.sendCommand('100')
}
}
end
I’m monitoring the logs with log:tail after connecting to openhab with ssh -p 8101 openhab@localhost . Is that correct?
Edit:
Is it possible to check which state the Item was before the change? Because now the brightness is fixed to either 0 when off and 33 or 100 when on. I can’t change it manually, because every change is intercepted by the rule and overruled .
So, I’d like to check if the state was 0/off and change it to 33/100, then. But if the light is already on I’d like to do nothing in order to be able to set any value between 1 and 100 - if that is possible.
As Vincent suggested, it may be helpful for you to look into using Time of Day. If you use the new rule engine, I’ve built a full solution for automating lighting and other things. Once setup, you just adjust metadata and group membership… no more coding of rules.