First ever rule ,need guidance

looking to make my first rule but honestly dont know where or how to start .
Yes i have tryed to learn from documentations and other users examples,but i have a hard time understanding them and knowing how to use the info. Secondly there seems to be different ways of doing it .

Not even sure its possible

So i hope a friendly soul will help a noob.

But what i would like is to dim my hue spots in the office at night lets say after 22 to 06. .Lights are controlled by pir ,AND pressing the light switch should make spots go to 100%.

Please please :blush:

Bozidar

Do you want a DSL rule? If you have no clue and have read the rules documentation, I’m guessing yes. The rule will need to be in a rules file and placed in the rules directory of OpenHAB’s conf directory
The rule takes the form of:

rule “unique rule name in quotes”
when
some trigger condition
then
some cool stuff happens
end

So you pir reporting motion would probably be what you wanted as a trigger condition. You’ll need an item linked to the motion channel on the pir so your when would be

Item myitem changed

in the then put some logic to check the time

if (now.getHourOfDay() >= 06 && now.getHourOfDay() < 22) {
mylightdimmer.sendCommand(40)
}
so something like:

rule “unique rule name in quotes”
when
Item myitem changed
then
if (now.getHourOfDay() >= 06 && now.getHourOfDay() < 22) {
mylightdimmer.sendCommand(40)
}
end

this is an over simple rule and you will need another to turn the spots to 100% but you get the idea.Watch the case of things rule, when, then and end are all lower case only. Item in the when is upper case ‘Item’
good luck, have fun and come back when you get stuck

1 Like

You may want to start with having the lights turn on with motion then edit to set a certain value.

Example to turn on light:

rule "pir light"
when
    Item pir changed to ON
then
    if(OfficeLight.state == OFF){
        OfficeLight.sendCommand(ON)
    }
end

First of all thx once again for helping out.

Maybe i need to give more info. Our house is equiped with IHC (intelligent house control) that is not really very intelligent :slight_smile:
Pir is part of IHC and controling lights thru that.but i dont have IHC dimmers so want to use HUE spots to do dimming part.
So on/off is handled outside of Openhab.
So i would like Openhab only to handle the dimming part.,so on/off works even if openhab is down.
Does it ,make sence ?

Just adding to what @Andrew_Rowe posted to help with the switch and light to be 100%.

rule “unique rule name in quotes”
when
Item myitem changed
then
if (now.getHourOfDay() >= 06 && now.getHourOfDay() < 22 && LightSwitch.state != ON) {
mylightdimmer.sendCommand(40)
}
else {
    mylightdimmer.sendCommand(100)
}
end

Just make sure to use the correct item names. :wink:

1 Like

Can OH connect to IHC? If not then you can use a cheap photocell to detect if the light is on or off. Use that input in the rule to control the dimming.

For the photo cell an ESP8266 device communicating via mqtt with OH will work well. I have this in my garage so Alexa will tell me if the garage door is closed and the light was left on.

You might not need 30 of them but here’s a link:

Can OH connect to IHC?

YES there is even a very nice binding . pir does "report " into Openhab. Openhab also knows if light is on/off .I can control the on/off thru Openhab aswell.

Sounds good. :+1: Just add the correct item names to the rule example above and see if it acts as expected.

EDIT:
May want to change the trigger to specify ON else the light may want to toggle ON/OFF.

rule "unique rule name in quotes"
when
Item myitem changed to ON
then
if (now.getHourOfDay() >= 06 && now.getHourOfDay() < 22 && LightSwitch.state != ON) {
mylightdimmer.sendCommand(40)
}
else if (LightSwitch.state == ON) {
    mylightdimmer.sendCommand(100)
}
end

before digging in i need to ask a few dump question

I guess lightswitch is refering to the actual “lightswitch” 
Lightswitch is a “toggle switch” and not an on/off if that makes sence. Does that make a difference.

and lights are a group of 3 spots. does that change anything

That is the switch to turn the lights to 100% brightness.

This might affect how the rule behaves but give it a try and see what happens. Playing with rules there will be lots of trial and error but that’s part of learning. :wink:

So it’s three separate bulbs, or items in OH, correct? If so then check the doc’s about how to use groups or you will need to list each light inside the rule.

I really appreciate your help,will give it a try later.and post my “findings”.

That’s what the community is for. :wink:

I will most likely not be online later so I will give you a hint on the toggle switch but try the rule first just to see what happens.

Here’s part of a post I found and hope you find it as a good hint for solving your toggle switch issue.

var Timer tToggle = null

rule "toggle frequently"
when
    Item myTriggerItem changed
then
    tToggle?.cancel
    if(myTriggerItem.state == ON)
        tToggle = createTimer(now.plusMillis(200),[ | 
            myTogglingItem.sendCommand(if(myTogglingItem.state != ON) ON else OFF)
            tToggle.reschedule(now.plusSeconds(2))
        ])
end

When the rule gets triggered (because the Item myTriggerItem changed its state), the first thing is, an existing timer is killed. The second step is, to create a new timer, but only if the current state is ON.
The timer will expire almost immediately and will toggle the Item myTogglingItem. Then the timer will reschedule itself two hours later.

Please be aware that myTogglingItem will keep its state when myTriggerItem is switched OFF, so the only thing which is switched, is the toggling!

I tried this.

rule "NatsĂŠnkning
when
Item ihc:controller:elko:output77659 changed to ON
then
if (now.getHourOfDay() >= 06 && now.getHourOfDay() < 1700 && LightSwitch.state != ON) {
hue:0220:0017884c7a9b:30:brightness.sendCommand(40)
hue:0220:0017884c7a9b:29:brightness.sendCommand(40)
hue:0220:0017884c7a9b:28:brightness.sendCommand(40)
}
else if (LightSwitch.state == ON) {
hue:0220:0017884c7a9b:30:brightness.sendCommand(100)
hue:0220:0017884c7a9b:29:brightness.sendCommand(100)
hue:0220:0017884c7a9b:28:brightness.sendCommand(100)
}
end

log states
2020-03-08 18:01:09.825 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘hus.rules’ has errors, therefore ignoring it: [102,6]: String literal is not properly closed

OK good thing to note
 EVERY every every time you edit a rule and save it, have your terminal open and watch the log tail for the model (rules file) being loaded correctly

that means your rules files is messed up, not going to work and the system is not going to load it at all. There is a mistake in the rule

this second bit is telling you where the error is and what is wrong
you are missing a closing quote apparently

found the error ,but there is still something

log states: no viable alternative at input ‘:’

rule "NatsĂŠnkning"
when
Item ihc:controller:elko:output77659 changed to ON
then
if (now.getHourOfDay() >= 06 && now.getHourOfDay() < 22 && ihc:controller:elko:output77659.state != ON) {
hue:0220:0017884c7a9b:30:brightness.sendCommand(40)
hue:0220:0017884c7a9b:29:brightness.sendCommand(40)
hue:0220:0017884c7a9b:28:brightness.sendCommand(40)
}
else if (LightSwitch.state == ON) {
hue:0220:0017884c7a9b:30:brightness.sendCommand(100)
hue:0220:0017884c7a9b:29:brightness.sendCommand(100)
hue:0220:0017884c7a9b:28:brightness.sendCommand(100)
}
end

I think it is you item definition it is choking on
that doesn’t look like an item, that looks like a channel, you need an item with that channel

Items are defined using the following syntax:

itemtype itemname "labeltext [stateformat]" <iconname> (group1, group2, ...) ["tag1", "tag2", ...] {bindingconfig}
  • Fields must be entered in the order shown
  • itemtype and itemname are mandatory
  • All other fields are optional
  • Fields may be separated by one or more spaces, or tabs
  • An Item definition may span multiple lines

Example:

Number ihcOutput  "my ihc thing output" {channel="ihc:controller:elko:output77659"}

save that in an items file, in the items folder with an extention .items
then your rule looks like this

rule "NatsĂŠnkning"
when
Item  ihcOutput changed
then
if (now.getHourOfDay() >= 06 && now.getHourOfDay() < 22 && ihc:controller:elko:output77659.state != ON) {

next problem
 all these guys look like channels too

are you sure you looked at this?

Hallo Andrew

first of all thank you very much for your help.

Believe it or not i have spend many hours with Openhab ,and have a decent size installation running quite nicely for more than a year with more than 100 items and things.

But you are totally right ,i have made a total rookie error .

Rule is accepted now.

thx again

1 Like
var Timer tToggle = null

rule "toggle frequently"
when
    Item myTriggerItem changed
then
    tToggle?.cancel
    if(myTriggerItem.state == ON)
        tToggle = createTimer(now.plusMillis(200),[ | 
            myTogglingItem.sendCommand(if(myTogglingItem.state != ON) ON else OFF)
            tToggle.reschedule(now.plusSeconds(2))
        ])
end

I realised i need to dig in my IHC system a bit more first
this is paused


Digging into the IHC system ,i realized that switch is actually not a toggle switch.

It is an “electronic” contact that is ON as long as it is pushed.,but can be setup to “mimic” toggle behaviur. The need for a second rule ,leads me to believe ,that toggle is not optimal.

Is the short ON signal better ?

for learning purpuses i am ignoring the need to be able to increase dim to 100%.

So i am trying this

rule "Natlys"
when
Item Kontor_spots changed to ON
then
     if (now.getHourOfDay() <= 06 && now.getHourOfDay() > 17 && Kontor_spots.state != ON) {
        Kontor1_Dimmer.sendCommand(40)
        Kontor2_Dimmer.sendCommand(40)
        Kontor3_Dimmer.sendCommand(40)
}
end

But no errors in log,and rules does not work.