New Rules

Hello, I am new at openhab2 and from the forum I have managed to connect my devices. I haven’t got knowledge of programming and it’s all unknown to me but at the same time I am happy to see myself creating. I would like to ask if anyone of you can help me to create a rule with which I want from 23:50 until 7:30 the Sbedroom switch will be from on to off then it will turn off the lights of Skitchen ,Skids , Slivingroom or by sending off, on, off ( 3 quick command) during the time period that I want .

After reading the official documentation

and

https://www.quartz-scheduler.org/documentation/quartz-2.2.2/tutorials/tutorial-lesson-06.html

What have you tried? Post some attempts here and we can help you troubleshoot & learn.

We are here to help you learn, not to provide free services to you.

Hello everyone. I am an amateur with this program and also I dont speak English and I have someone help me translate so as to understand the Rules. I have managed to set up some Things at home but I face some problems with the Rules.

The first case:
I face the same problem here as well. Ths switch that I want to control the other lights with for a specific time period works like that all Day.

val String filename = "Bend room time.rules"
// SBedroom

rule "bedroom off time "

when
Item SBedroom changed from ON to OFF
then
//  από τις 00:30 έως τις 7:30
if (now.getMinuteOfDay >= (60*1+5) || now.getMinuteOfDay <= (60*7+30)){
       Kitchen_Light.sendCommand("OFF")
       Dinning_room_light.sendCommand("OFF")
       House_corridor_Light2.sendCommand("OFF")
       House_corridor_Light1.sendCommand("OFF")
       Bathroom_Light.sendCommand("OFF")
       SKids_Room.sendCommand("OFF")
       Kids_switch.sendCommand("OFF")
       Bathroom_Light.sendCommand("OFF")
       logInfo(filename, "Bedroom Switch All Light off - Time 00:00 end 8:30")
}

else
          SBedroom.sendCommand("OFF")
end

The second case:
I have tried this. It changes the volume to 15% as it is now but it is contsantly at this volume and does not change for the time period that I want to be lower. What I want to do is for the volume to be at 23% all Day except for the specific time that I want it to be 15%.

val String filename = "BATHROOM PLAY MUSIC.rules"



rule "Music ON"
when
Item Bathroom_Light changed from OFF to ON
then
Control.sendCommand("PLAY")
logInfo(filename, "Music - Play")
end



rule "Music OFF"
when
Item Bathroom_Light changed from ON to OFF
then
Control.sendCommand("PAUSE")
logInfo(filename, "Music - Stop")
end








rule "MUSIC PLAY volume 15 time 23:30 to 8:30 "
when
Item Control changed to PLAY
then
if  (now.getMinuteOfDay >= (60*23+30) || now.getMinuteOfDay <= (60*8+30)) {
        Volume.sendCommand("15")
        logInfo(filename, "Music - Volume 15")
}
else
          Volume.sendCommand("23")
          logInfo(filename, "Music - Volume 23")
end






rule "MUSIC PLAY volume 15 time 15:30 to 17:30 "
when
  Item Control changed to PLAY
then
  if (now.getMinuteOfDay >= (60*15+30) || now.getMinuteOfDay <= (60*17+30)){
    Volume.sendCommand("15")
        logInfo(filename, "Music - Volume 15")
  }
  else
          Volume.sendCommand("23")
          logInfo(filename, "Music - Volume 23")
end

I will need to look in detail alter, but please use “code fences” when posting rules. Here is wher the btton is located.

If you do that your posts here are much shorter and more readable. Here, for example, is your second rule with “code fences”

val String filename = “BATHROOM PLAY MUSIC.rules”

rule “Music ON”
when
Item Bathroom_Light changed from OFF to ON
then
Control.sendCommand(“PLAY”)
logInfo(filename, “Music - Play”)
end

rule “Music OFF”
when
Item Bathroom_Light changed from ON to OFF
then
Control.sendCommand(“PAUSE”)
logInfo(filename, “Music - Stop”)
end

rule "MUSIC PLAY volume 15 time 23:30 to 8:30 "
when
Item Control changed to PLAY
then
if (now.getMinuteOfDay >= (6023+30) || now.getMinuteOfDay <= (608+30)) {
Volume.sendCommand(“15”)
logInfo(filename, “Music - Volume 15”)
}
else
Volume.sendCommand(“23”)
logInfo(filename, “Music - Volume 23”)
end

rule "MUSIC PLAY volume 15 time 15:30 to 17:30 "
when
Item Control changed to PLAY
then
if (now.getMinuteOfDay >= (6015+30) || now.getMinuteOfDay <= (6017+30)){
Volume.sendCommand(“15”)
logInfo(filename, “Music - Volume 15”)
}
else
Volume.sendCommand(“23”)
logInfo(filename, “Music - Volume 23”)
end

My first guess is you need to put { } after your else statements.

else
SBedroom.sendCommand(“OFF”)
end

then becomes

else {
SBedroom.sendCommand(“OFF”)
}

or

else { SBedroom.sendCommand(“OFF”) }

Thank you for the information and Sorry I didnt know How to put my content up. I will try it. I I would also like to ask when changing the time shall I wait for the Day to change to see the changes or it happens immidiately?

I just realized the else section in the first rule serves no useful purpose. You should be able to just delete the else section. That section sends the OFF command to a switch that is already OFF since that is the rule trigger.
It may be useful to see your Items definitions to help make it clear how the rules function.

Thank you I will look this up. I also had it written without the else but my problem was that it continued to work like that all Day and thats why I used the else rule.

I will need to test for you later too. I have not assisted many people writing rules.

Thank you so much I appreciate it.

1 Like

I think the volume problem comes from your multiple rules.
Try grouping the volume condition in the same rule like:

If ( (time > 8:30 && time < 15:30) || (time > 17:30 && time < 23:30) ) {
  Volume.sendCommand(“23”)
} else {
  Volume.sendCommand(“15”)
}

Replace the condition statement with real now objects (that I was to lazy on my phone to fill correctly), and I believe you’ll be good.

What I did looks like it’s working… but could you help with the time? If the way that I have written the time period from 23:30 to 8:30 and from 14:30 to 17:30 is right?


val String filename = "BATHROOM PLAY MUSIC.rules"

rule "Music ON"
when
Item Bathroom_Light changed from OFF to ON
then
Control.sendCommand("PLAY")
logInfo(filename, "Music - Play")
end



rule "Music OFF"
when
Item Bathroom_Light changed from ON to OFF
then
Control.sendCommand("PAUSE")
logInfo(filename, "Music - Stop")
end


rule "MUSIC PLAY volume 15 time 23:30 to 8:30 && 14:30 to 17:30 "
when
Item Control changed to PLAY
then
if ((now.getMinuteOfDay >= (60*23+30) && now.getMinuteOfDay >= (60*14+30)) || (now.getMinuteOfDay < (60*17+30) && now.getMinuteOfDay < (60*7+30))) {
        Volume.sendCommand("17")
        //PlayFavorite.sendCommand("6")
}
else
         {Volume.sendCommand("27")}
          logInfo(filename, "Music - Volume 23")
end

I think you are looking for something like this…

rule "Music"
when
    Item Bathroom_Light changed
then
    val command = if (Bathroom_Light.state == ON) "PLAY" else "PAUSE"
    Control.sendCommand(command)
    logInfo(filename, "Music - {}", command)
end

rule "MUSIC PLAY volume 15 time 23:30 to 8:30 && 14:30 to 17:30"
when
    Item Control changed to PLAY
    or
    Time cron "0 30 8 * * ?"
    or
    Time cron "0 30 14 * * ?"
    or
    Time cron "0 30 17 * * ?"
    or
    Time cron "0 30 23 * * ?"
then
    if (now.getMinuteOfDay < 60 * 8 + 30 || (now.getMinuteOfDay > 60 * 14 + 30 && now.getMinuteOfDay < 60 * 17 + 30) || now.getMinuteOfDay > 60 * 23 + 30) {
        Volume.sendCommand("17")
        //PlayFavorite.sendCommand("6")
        logInfo(filename, "Music - Volume 17")
    } else {
        Volume.sendCommand("27")
        logInfo(filename, "Music - Volume 27")
    }
end

I added some cron triggers so that the volume will change at the specified times too.

1 Like

Thanks, Scott.

1 Like

Thank you I fixed it with the code you gave me. I would also like to ask if at the future I decide to change the time will I have to change

when Item Control change to Play or Time crom “0 30 8 * * ?”

at this part ?

Yes, you will need to change the times of the triggers to match the times in the rule.

Thanks, Scott