Dimmer Light Rules / Command Help

Hi all.

I am trying to learn / create a couple of simple dimmer rules / commands, but through various attempts, nothing happens.

I am using Philips Hue, and have all the binding etc, and they are controlled without any problems manually via openhab.

Firstly, I have been trying to create a Rule for a light which is over our Bird cages, and for it to go off at 6.00pm say, or a given time.
I used Cronmaker to create the time code, and followed various guides to try and get this to work, none of which did.

The most recent I tried was this:

rule "Turn Off Birds Light"
when
item Dimmer Light_IN_Frontroom_Birds received command or
Time cron "0 0 18 1/1 * ? *"
then
var Number percent = 0
if(Dimmer Light_IN_Frontroom_Birds.state instanceof DecimalType) percent = Dimmer Light_IN_Frontroom_Birds.state as DecimalType
postUpdate(Dimmer Light_IN_Frontroom_Birds, percent); 100
end

I have also tried even a simple direct command (found on the CinemaVision Forum for Kodi), which was the following:

http://192.168.0.5:8080/CMD?Light_IN_Frontroom_Birds_Dimmer=100

I have looked up many given rule examples, and tried many variations, but must be doing something wrong.

Any help would be appreciated.

Thanks.

I did also add these to the Rules File:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import java.lang.String

1st you have to use sendCommand instead of postUpdate as postUpdate only updates the state of an item, but does not send this state through the binding.
2nd you don’t have to use the word Dimmer in rules to specify an item, this is .items only.

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

rule "Turn Off Birds Light"
when
    Time cron "0 0 18 * * ?" // 18:00:00, every day, every month, every weekday, don't care about year
then
    Light_IN_Frontroom_Birds.sendCommand(0)
end 

If you want to do a (more or less) smooth fadeout instead of instant blackout, try this:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import org.joda.time.DateTime

var int percent = 0
var Timer fade_Timer = null

rule "Turn Off Birds Light"
when
    Time cron "0 0 18 * * ?" // 18:00:00, every day, every month, every weekday, don't care about year
then
    if (Light_IN_Frontroom_Birds.state instanceof DecimalType) {
        percent = ((Light_IN_Frontroom_Birds.state as DecimalType)/5).intValue * 5 //round to 5
        fade_Timer = createTimer(now.plusMillis(500)) [|
            Light_IN_Frontroom_Birds.sendCommand(percent)
            if (percent > 0) {
                percent = percent - 5
                fade_Timer.reschedule(now.plusMillis(500))
            }
        ]
    }
end 

Had to rework the rule :slight_smile:

4 Likes

This is great @Udo_Hartmann, thanks very much for your help and prompt response.

The slow dim explanation you have given looks great, and would be a far better option, was struggling just to get the light to go off!

I will put this to the test and let you know how it goes.

Still very new to all this, but am really enjoying learning it all.

Thanks again, appreciated.

Yes, home automation (especially openHAB) should be much fun for nerds :wink:

The first simple rule worked straight away, but the slower fade did not.

Really appreciate the help though @Udo_Hartmann, much appreciated.

Learning more all the time. :wink:

Did you see the corrections that I made? I tested the rule and can confirm that it works for me.

Yes, saw those, and just copied and pasted the whole code and tried again, but still nothing.

The quick simple one works though, every time.

Strange.

Really strange :slight_smile:
Is there any error log in openhab.log about the not working rule? You can force logging some values
set this line

logInfo ("fadeout","percent = {}",percent)

right below Light_GF_Living_Planets_1.sendCommand (percent) to see if percent is decremented.

Ah. I just saw a flaw… If the Light is ON, maybe the state isn’t an instance of DecimalType.

[...]
then
    if (Light_IN_Frontroom_Birds.state !instanceof DecimalType && Light_IN_Frontroom_Birds.state != OFF)
        Light_IN_Frontroom_Birds.sendCommand(95)
    percent = ((Light_IN_Frontroom_Birds.state as DecimalType)/5).intValue * 5 //round to 5
    fade_Timer = createTimer(now.plusMillis(500)) [|
        Light_IN_Frontroom_Birds.sendCommand(percent)
        if (percent > 0) {
            percent = percent - 5
            fade_Timer.reschedule(now.plusMillis(500))
        }
    ]
end

Just the interesting part…

Also beware the typo. Shouldn’t that be fade_Timer.reschedule(now.plusMillis(500)) ?

yes, did not see this in first place :slight_smile:

Hey Udo,
Can you help me for the below topic?
I want to setup a Dimmer item, with which I waant to bind the http binding, I can use the rule present in the demo.rules file, but not sure how to send the percentage value of it to my http server running on Raspberry pi3. Any Ideas will be appriciated.

Well, given a dimmer item bound to http, this should be very easy…

Dimmer myDimmer "My http Dimmer is [%d %%]" {http=">[*:POST:http://www.domain.org/home/lights/23871/?status=%2$s&type=text] <[http://www.domain.org/weather/openhabcity/daily:60000:REGEX(.*?<title>(.*?)</title>.*)]"}

(Item definition from documentation)

Now to change the value, either use a slider in Basic UI or something similar, with “real” sliders, or use INCREASE/DECREASE commands and a rule to set the value:

rule "My http dimmer"
when
    Item myDimmer received command
then
   if (receivedCommand == DECREASE) {
        myDimmer.sendCommand((myDimmer.state as DecimalType) - 5)
}
    else if (receivedCommand == INCREASE) {
        myDimmer.sendCommand((myDimmer.state as DecimalType) + 5)
}
end
1 Like

Udo,
Thanks for your reply,
My setup is as below,

Dimmer Led_R “Red” (FF_Nishit, Lights){http=">[*:GET:http://192.168.0.108:5000/red3/%2$s]"}

and rules,

rule "RED LED"
when
Item Led_R received command
then
var Number b = 0
if(Led_R.state instanceof DecimalType) b = Led_R.state as DecimalType

	if(receivedCommand==INCREASE) b = b + 10
	if(receivedCommand==DECREASE) b = b - 10

	if(b<0)   b = 0
	if(b>100) b = 100
            
            sendCommand(Led_R, b)
			
end

can you point any mistake, currently it is not working

Hey Udo,
I tried your hings and I got the success bu I get never ending loops, can you suggest how to stop them?

I have no idea, why you get a loop. you can try to use

when
    Item myDimmer received command DECREASE or
    Item myDimmer received command INCREASE
then

as the trigger part, but my rule took care of this, because there would have been only a sendCommand if the received Command was INCREASE or DECREASE, this should suffice (in fact my rule will trigger itself, but the received command will be of Type Number, but not INCREASE/DECREASE, so when triggered from itself, it should do nothing at all).
On the contrary, your rule from posting #14 would possibly loop forever, as it will send a command, even if it did not receive INCREASE/DECREASE, but also ON/OFF or 0 to 100.

Thank you very much Udo, don’t know what I did wrong, but it is working with your rule.I changed nothing in it just wrote the whole code allover again and it is working now.

Udo,
when I do the following,

Dimmer Led_R   "Red"  (FF_Nishit, Lights){http=">[*:GET:http://192.168.0.108:5000/red3/?status=2$s&type=text]"}

it send the the whole link including’?status=2$s&type=text’ instead of the value of b
I want to send it as following,
192.168.0.108:5000/red3/50, instead of 50 there should be the value of variable created in the rule.

If you look at Udo’s example, it has a % symbol right about where you want to substitute the value.
You just have to take care with these things.

I also have included ‘%’ but it is not working.Here are my codes.

Item.

Dimmer Led_R			     "Red"		 (FF_Nishit, Lights){http=">[*:GET:http://192.168.0.108:5000/red3/?status=%2$s&type=text]"}

and the rules,

rule "dimmer Led-R"
when
    Item Led_R received command
then
   if (receivedCommand == DECREASE) {
        Led_R.sendCommand((Led_R.state as PercentType) - 5)
}
    else if (receivedCommand == INCREASE) {
        Led_R.sendCommand((Led_R.state as PercentType) + 5)
}
        
end

I want to send the following link via http binding to my server,
192.168.0.108:5000/red3/(the value from dimmer )
can you suggest any changes?