Dimm the light during nighttime

Hi,

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
    • openHAB version: 2.4.0

You are using the keyworrd now as a variable name
Don’t use int

Also, have a look at: Design Pattern: Time Of Day

I changed it, thanks.

The rule is now:

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

Look at the difference between your rule trigger and your log

1 Like

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.

Is something like changed 0 to x possible?

No, you’ll need to put the conditional in the body of the rule. Something like this could work…

rule "Bathroom_Dimm_Night"
when
  Item Light_GF_Bathroom_CeilingLights_MainLight_Brightness changed
then
    if (triggeringItem.state > 0) {

This will work, but it’s a little easier to just use…

Light_GF_Bathroom_CeilingLights_MainLight_Brightness.sendCommand("33")
1 Like

Thanks. It works now.

Only thing wich does not work: Logging something.

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 :smiley: .
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.

Do the logInfo come through, but not the logDebug? If so, you’ll need to set the log level…

log:set DEBUG org.eclipse.smarthome.model.script

Thanks. That was the problem.

Found also previousState, which gives me a solution for manually changing the brightness. Here is the working example:


val nightTimeStart = 2300
val nightTimeEnd = 0530
val currentTime = now.getHourOfDay * 100 + now.getMinuteOfHour

val nighttimeBrightness = '25'
val daytimeBrightness = '100'

rule "Bathroom_Dimm_Night"
when
  Item Light_GF_Bathroom_CeilingLights_MainLight_Brightness changed
then

  logDebug("Bathroom_Dimm_Night", "Bathroom light brightness changed")
  logDebug("Bathroom_Dimm_Night", "Previous brightness:" + previousState)

  if (triggeringItem.state > 0 && previousState ==0) {

    logDebug("Bathroom_Dimm_Night", "Bathroom light turned on")

    if (currentTime >= nightTimeStart || currentTime <= nightTimeEnd) {
      logDebug("Bathroom_Dimm_Night", "Dimm bathroom light because it is nighttime")
      Light_GF_Bathroom_CeilingLights_MainLight_Brightness.sendCommand(nighttimeBrightness)
    } else {
      logDebug("Bathroom_Dimm_Night", "Set bathroom light brightness to 100 because it is daytime")
      Light_GF_Bathroom_CeilingLights_MainLight_Brightness.sendCommand(daytimeBrightness)
    }

  }
end

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.

1 Like

in oh3 i get an error:

The field Tmp_licht_dimmen_testRules.currentTime refers to the missing type Object

do you have a working version for oh3?