This page last changed on Jul 08, 2014 by hennemarc.

Hello all,

using a fixed timer in a rule looks like this:

rule "Turn light off after 10s"
timer (int: 10s)
when
Event(source=="LED ON/OFF", value=="on")
then
System.out.println("LED turned off after 10 seconds");
execute.command("LED OFF");
end

Now I have created a slider ("Slider - Timer") which brings back via a sensor a number between 0...60. How can I bring this value into the timer, so that I can set the timer time in a flexible way?

rule "Turn light off after 0...60s"
Range(source == "Slider - Timer", $timer01 : value)
timer (int: $timer01)
when
Event(source=="LED ON/OFF", value=="on")
then
System.out.println("LED turned off after 10 seconds");
execute.command("LED OFF");
end

FYI, this rule does not work, but what is the correct syntax?

Marc

Marc,

Did you get this one working already? im also looking for a variable timer which can be set by a slider

i found this post but didnt tried it yet:
stackoverflow.com/questions/24208007/drools-how-to-use-a-variable-in-the-duration-specification

Rob

Posted by robnas at Oct 01, 2015 15:13

Hi

We're achieved this using Velbus hardware, if that helps you.

Posted by mdar at Nov 03, 2015 19:03

In the new Drools 6.x it is possible to do:

declare Bean
    delay   : String = "30s"
    period  : long = 60000
end

rule "Expression timer"
    timer( expr: $d, $p )
when
    Bean( $d : delay, $p : period )
then
end

As far as I'm aware, the new Drools version will be included in the Pro 1.4 controller. It is already available in one test builds http://svn.code.sf.net/p/openremote/code/workspace/rhitz/Controller_ProInstaller_1_1_0_IOT_SF/ and checked with Java 8 on Raspberry Pi 2.

Posted by aktur at Nov 04, 2015 12:47

I gathered together advice from all over this forum and came up with this:

// Block of Rules for Office Fan (socket)
//
rule "OfficeFanAuto_Off"
    timer (int: 1s) // "debounce" - let the user settle on a value
when
    Event( source == "VirtualTimedOfficeWallSocketValue", value == "0")
then
    execute.command("TimedOfficeWallSocketOff");
end

rule "OfficeFanAuto_On"
    timer (int: 1s) // "debounce" - let the user settle on a value
when
    Event( source == "VirtualTimedOfficeWallSocketValue", value > 0 )
then
    execute.command("TimedOfficeWallSocketOn");
end

rule "OfficeFanAuto_dec"
    timer (int: 1h) //
when
    Event( source == "VirtualTimedOfficeWallSocketValue", $TimeLeft : value > 0 )
then
    Integer iTl = Integer.parseInt($TimeLeft.toString()) ;
    iTl = iTl - 1 ;
    execute.command("VirtualTimedOfficeWallSocketStatus", iTl );
end

In my case, it's a Fan, so I want HOURS not minutes. The Internal Memory (Virtual) value "VirtualTimedOfficeWallSocketValue" is tested and decremented. Three Rules, 1 for On, 1 for Off and 1 for decrementing.

Posted by c.steele at Nov 15, 2015 23:14

Hello

Thanks for putting your rules example on here.

I've tried to use it in my design to get a timer to count down days while I'm away, so that my house wakes up again as I return.

For some reason when I load this version in, all my rules stop.

I know I've done something daft with it, but I just can't see the problem.

Can you spot anything?


// Block of Rules for Holiday away days
//
rule "Holiday_mode_Off"
    timer (int: 1s) // "debounce" - let the user settle on a value
when
    Event( source == "Holiday Days", value == "0")
then
    execute.command("01-34 Ch4 OFF");
end

rule "Holiday_Mode_On"
    timer (int: 1s) // "debounce" - let the user settle on a value
when
    Event( source == "Holiday Days", value > 0 )
then
    execute.command("01-34 Ch4 ON");
end

rule "Holiday_Mode_dec"
    timer (int: 10s) //
when
    Event( source == "Holiday Days", $TimeLeft : value > 0 )
then
    Integer iTl = Integer.parseInt($TimeLeft.toString()) ;
    iTl = iTl - 1 ;
    execute.command("Holiday_Days set", iTl );
end

"Holiday Days" is a sensor attached to an in memory command that has a value set from a slider.
This works because I have the sensor shown on the panel.

Just for information,

I'm running OR1.3, with Java 1.6 on a Windows 7 Netbook.

Many thanks in advance.

Posted by mdar at Nov 18, 2015 20:39

I don't see anything, and when I paste your code into my Rules file, it loads fine, no errors. That's what I expected actually... "all rules stop" tells me there was a load error and the entire file was discarded. I'm on a Mac, so there will be differences but look in boot.log and verify the file loaded. I get this:

DEBUG 2015-11-18 14:13:42,412 : Initializing event processor: Drools Rule Engine
DEBUG 2015-11-18 14:13:42,466 : Adding Rule 'my_home_rules.drl'...
DEBUG 2015-11-18 14:13:48,784 : Adding rule definitions from 'my_home_rules.drl'...

Remove this block of code, and create a new file with only it.. then your regular stuff will load but the fail in the new file will still fail. I have two files in ../webapps/controller/rules/*.drl

I have this at the top of my file... maybe yours is missing one...

package org.openremote.controller.protocol

import org.openremote.controller.model.event.*;
global org.openremote.controller.statuscache.CommandFacade execute;
global org.openremote.controller.statuscache.SwitchFacade switches;
global org.openremote.controller.statuscache.LevelFacade levels;

That's all three of my thoughts.

Posted by c.steele at Nov 18, 2015 22:23

Thanks a million for your input.

I must confess that once you'd suggested there was nothing wrong, I simply synchronized my controller again.

From that point...

Everything is working really well.

The only thing to do now is change the code from 10s to 24h.

Thank you so much for creating this set of rules.

Posted by mdar at Nov 19, 2015 15:29

well... if one code snippet helps, maybe two is "more better"?

I had a problem to solve since I moved into this house.. putting trash into the outdoor trash bins in the dark. I needed a light to even see the bins, but I'd forget to turn it off. Long before I heard Z-Wave I changed the light switch to have a mechanical timer. That twisty type that tick tick ticks til it gets to zero and clicks off. But that meant the light would be on for 10+ mins, for a 20 second task. So then I thought, I know, I'll spend about $70 to save 9 mins of electricity a day!

I added a Aeon Z-Wave Microswitch behind that mechanical one. Then slapped on a door sensor. Now I can turn the light on just by opening the door AND it stays on for only 20 seconds. If I need it longer, the mechanical timer switch (OR the phone app,) turns it on/off. Most importantly, if I twist the mechanical, the light does NOT turn off after 20 seconds.

// Block of Rules for Timed Rear-Yard door Light
//
rule "RearDoorAuto_Off"
when
    Event( source == "VirtualTimedRearBackDoorLightValue", value == "0")
then
    execute.command("TimedRearBackDoorSwitch_Off");
end

rule "RearDoorAuto_On"
when
    Event( source == "DateTimeIsNitetime_sensor", value == "true" )
    Event( source == "VirtualTimedRearBackDoorLightValue", value > 0 )
then
    execute.command("TimedRearBackDoorSwitch_On");
end

rule "RearDoorAuto_dec"
    timer (int: 1s) //
when
    Event( source == "VirtualTimedRearBackDoorLightValue", $TimeLeft2 : value > 0 )
then
    Integer iTl2 = Integer.parseInt($TimeLeft2.toString()) ;
    iTl2 = iTl2 - 1 ;
    execute.command("VirtualTimedRearBackDoorLightStatus", iTl2 );
end

rule "DoorSensorTrigger"
when
    Event( source == "TimedGarageBackDoorSwitch_Status", value == "0" )
    Event( source == "RearBackDoor_sensor", value == "on")
then
    execute.command("VirtualTimedRearBackDoorLightStatus", 20 );
end

Very simple. Door Sensor sets a timer value (instead of a slider doing that,) leaving off, on, or decrement Rules to be pretty much the same. Two additions: 1) Door Sensor doesn't initiate this if the switch is already on (from the mechanical,) therefore it doesn't shut off automatically; 2) adding "IsNight" to the equation so that the light only comes on automatically if it's night.

Posted by c.steele at Nov 19, 2015 17:33

Wow!

That's a very impressive set of rules you've created.

I'm afraid to say, all of which are beyond me.

However... I have got similar actions and conditions setup in my home, and a number of client's homes, but they are far easier to create using Velbus hardware.

That said, Velbus isn't really a retro fit technology like ZWave and other 'wireless' technologies.

Personally speaking, I really like the way that OpenRemote can sit above a combination of hardware and create some pretty amazing solutions.

Posted by mdar at Nov 20, 2015 15:27

It is great at sitting above, but in 2015, almost 2016, the list of protocols supported is looking not modern. Where's Bluetooth? (Bluetooth MESH will be available in 2016 and that will be interesting Zwave/zigbee competition.) Where's support for a Zigbee dongle? Or Lutron support for SmartBridge (vs the antique Homeworks) ?

Even Z-Wave is under represented. Products released a year ago still have no utility via OpenRemote.

I suspect the answer to ALL of the above is money. ZWave is costly to Develop. Same with Lutron. They reveal their API only to "Partners" and I assume that's a money gateway.

I really wish we could zoom by this era, this proprietary phase, and move straight to the days when the protocol isn't the #1 purchasing decision.

Posted by c.steele at Nov 20, 2015 18:23

You're preaching to the converted my friend

MDAR Ltd has just funded the code writing to include the Velbus protocol

But on that subject, speaking from many years of experience, it will be a dark day when I completely rely on a wireless technology, of any kind.

If you are looking for something (in any area) to work predictably and reliably, then make sure it has a bit of cable (Copper or Fibre Optic) to transfer the data.

I've got an associate who selected LightwaveRF for a small housing estate. 2 years on, they still haven't got them all working, at all, let alone all at the same time.

Whereas a hardwired solution..... Etc etc etc

You only have to count how many times your mobile phone drops a call, doesn't have a data connection or doesn't receive a text quickly in a day.

Posted by mdar at Nov 20, 2015 19:05

While I can't disagree on the initial run of copper, rodents sure love it. So, reliability over a period of say.. 3 years. How many outages wireless vs how many wired? I think the cable TV people would be the best source of those numbers. Can't remember how many times I've changed an connector on my cable system, but "lots."

I also think there's a HUGE difference between a 300 ft mesh of Zwave/Bluetooth/Zigbee wireless and 2km Cell service. I have been amazed by the reliability of my first use of Zwave - An under stair closet. I installed a battery operated door sensor and a Zwave wall socket (with wall mounted light strip plugged in.) I assumed it would be slow to turn on and would work 50%. I lived for 3 years without a light in there so 50% seemed ok at the time. Now, if it didn't work everytime, I'd pull some (more) hair. It's been installed for at least a year, and I have never seen it fail. And it works in the half second range. Open the door, the light goes on. Close the door, it goes off. Amazing!

Same goes for the Pico Remotes. I got the first one and thought it would work 50%. And initially I was ok with that. It was for a printer's power. I could hear it switch on or off, so clicking the Pico's button 2-3 times didn't seem like a big deal. Again, it's never failed. Same half second response too.

That's Lutron ClearConnect to the Hub, then Zwave out of the hub to the power socket. Amazing!

Posted by c.steele at Nov 20, 2015 20:44

As far as I'm aware, the new Drools version will be included in the Pro 1.4 controller. It is already available in one test builds http://svn.code.sf.net/p/openremote/code/workspace/rhitz/Controller_ProInstaller_1_1_0_IOT_SF/ and checked with Java 8 on Raspberry Pi 2

Interesting. Unfortunately it's been postponed to Pro 1.5. according to Jira. I am curious how well it runs on a Raspi2

Posted by pz1 at Feb 25, 2016 19:39

Hi

Carrying on with my holiday countdown thing, can anyone help me again please?

The whole countdown thing works really well with a slider, or even better with some preset buttons that simply set the virtual command to a value.

But there is a situation where the slider isn't accurate enough and the presets don't hold the values I need.

So, what I thought about was adding a +1 day and -1 day pair of buttons.

The only way I could think of doing this was to put two in memory switches on the panel, then get a pair of rules to monitor the state of these switches and add or subtract a day from the in memory value.
(As well as switching off the in memory switch)

I've tried the following rules, but with no luck.

I'm sure it's because I don't have a clue what I'm doing.



package org.openremote.controller.protocol;

global org.openremote.controller.statuscache.CommandFacade execute;

import java.util.*;


rule "Holiday+1"
when
    $evt:Event(source matches "Holiday+1", $source : source) // NOTE SENSOR NAME IS WHAT IS BEING COMPARED HERE

then
    String buttonStatus = $evt.getValue().toString();
    boolean isActive = buttonStatus != null && buttonStatus.equalsIgnoreCase("ON");

    System.out.println("Holiday days plus one '" + $source + "': " + buttonStatus);

    source == "Holiday-Days", $TimeLeft : value ;

    // EXECUTE ANY COMMANDS BY USING THEIR NAME AND VALUE TO SET
    // ADD ADDITIONAL COMMANDS AS REQUIRED

    if (isActive)
    {
        execute.command("Holiday+1-OFF", ""); // Uncomment to send command
    Integer iTl = Integer.parseInt($TimeLeft.toString()) ;
    iTl = iTl + 1 ;
    execute.command("Holiday_Days_Set", iTl );

    }
    else
    {
//        execute.command("Something_Else", ""); // Edit and uncomment to send command
//        execute.command("Something_Else", ""); // Edit and uncomment to send command
    }
end

rule "Holiday-1"
when
    $evt:Event(source matches "Holiday-1", $source : source) // NOTE SENSOR NAME IS WHAT IS BEING COMPARED HERE

then
    String buttonStatus = $evt.getValue().toString();
    boolean isActive = buttonStatus != null && buttonStatus.equalsIgnoreCase("ON");

    System.out.println("Holiday days minus one '" + $source + "': " + buttonStatus);

    source == "Holiday-Days", $TimeLeft : value ;

    // EXECUTE ANY COMMANDS BY USING THEIR NAME AND VALUE TO SET
    // ADD ADDITIONAL COMMANDS AS REQUIRED

    if (isActive)
    {
        execute.command("Holiday-1-OFF", ""); // Uncomment to send command
    Integer iTl = Integer.parseInt($TimeLeft.toString()) ;
    iTl = iTl - 1 ;
    execute.command("Holiday_Days_Set", iTl );

    }
    else
    {
//        execute.command("Something_Else", ""); // Edit and uncomment to send command
//        execute.command("Something_Else", ""); // Edit and uncomment to send command
    }
end



Thanks in advance for any help.

Cheers,

Stuart

Posted by mdar at Mar 02, 2016 18:43

But there is a situation where the slider isn't accurate enough and the presets don't hold the values I need

Not sure what you mean by accurate. IIRC you can use the slider for a coarse setting, and fine tune that with the icons on top or bottom of a veritcal slider. In this draft howto I did use only the top and bottom icons to set the hour that the dishwasher should start. (somewhere half way the page)

The problem with OR sliders often is the sluggish behaviour. I still have on my todo list a slider to an In memory Virtual Command. Leave a bit of time to manipulate the setting, and fire a command after that time has expired. Not nice for dimming lights, but useable for setting thermostats.

Posted by pz1 at Mar 02, 2016 18:58

Hi PZ1

I didn't realise the top and bottom icons had events associated with them.

fine tune that with the icons on top or bottom of a veritcal slider

I'll give that a try as it would be a really simple solution.

Cheers,

Stuart

Posted by mdar at Mar 02, 2016 19:55
Document generated by Confluence on Jun 05, 2016 09:39