This page last changed on Mar 14, 2014 by russ.

What would be the simplest way to accomplish the following?

Upon a button press, if "IsNight" then execute one macro or group of commands, else (i.e., "IsDay") execute another macro or different group of commands?

The simplest would be to use rules.

Posted by aktur at Mar 14, 2014 13:41

I'm not fully understanding the relationship of buttons (on the UI) to rules. I think what needs to be done is define a sensor, link it to a button, possibly linking it an in-memory variable, then defining a rule based on the in-memory variable and IsNight to trigger the commands I need. Am I thinking correctly?

Posted by russ at Mar 14, 2014 21:23

You can not run a macro from a rule. So you must write the respective commands into your rule code.

Posted by pz1 at Mar 15, 2014 07:02

A button can execute command -> command can set in-memory variable -> another command can read this in-memory variable -> this command can be used for sensor -> sensor can trigger rule execution.
IsNight can be another sensor, the you would have 2 rules:

rule "During night"
when
  Event(source=="inMemorySensor", value=="on")
  Event(source=="isNightSensor", value=="true")
then
  execute.command("inMemoryWriteCommand", "off");  // Clear button press
  // Follow with rest of your commands
end

rule "During day"
when
  Event(source=="inMemorySensor", value=="on")
  Event(source=="isNight", value=="false")
then
  execute.command("inMemoryWriteCommand", "off");  // Clear button press
  // Follow with rest of your commands
end
Posted by aktur at Mar 15, 2014 10:28

Thanks Michal. It's all starting to make sense now.

Posted by russ at Mar 15, 2014 14:34

Thanks to your help it's working now. One thing I'd like to comment on is that I needed to change an Event statement to a CustomState statement.

This does not work:

rule "all-off button night"
when
  Event(source=="AllOff-Sensor", value=="on")
  Event(source=="IsNight", value=="true")
then
  execute.command("AllOff-Off");  // Clear button press
  // Follow with rest of your commands
  execute.command("kitchen outlet off"); 
  execute.command("paper lamp off"); 
  execute.command("dining room lite off"); 
  execute.command("kitchen sink off"); 
  execute.command("kitchen lites off"); 
  execute.command("foyer lite off"); 
  execute.command("porch lite on"); 
  execute.command("deck lites on"); 
end

This works:

rule "all-off button night"
when
  Event(source=="AllOff-Sensor", value=="on")
  CustomState(source=="IsNight", value=="true")
then
  execute.command("AllOff-Off");  // Clear button press
  // Follow with rest of your commands
  execute.command("kitchen outlet off"); 
  execute.command("paper lamp off"); 
  execute.command("dining room lite off"); 
  execute.command("kitchen sink off"); 
  execute.command("kitchen lites off"); 
  execute.command("foyer lite off"); 
  execute.command("porch lite on"); 
  execute.command("deck lites on"); 
end

I'm guessing using Event is proper when evaluating a switch sensor, and CustomState is for a custom sensor? Is there a further distinction?

Posted by russ at Mar 26, 2014 03:46

I'm guessing using Event is proper when evaluating a switch sensor, and CustomState is for a custom sensor? Is there a further distinction?

This is not the case. I use "IsNight" custom sensor with Event() without problems. I think that the issue that you have is that Drools sometimes does not check all when statements correctly – this problem was reported on this forum few times. For example you will notice that behaviour will change when you reverse the order of checking. Your workaround works for you and this is great. My private solution to this is to add the following definition:

declare Event
  @role(event)
end
Posted by aktur at Mar 26, 2014 09:23

I'm guessing using Event is proper when evaluating a switch sensor, and CustomState is for a custom sensor? Is there a further distinction?

The question about distinction between Event and (Custom)State has been asked before, but sofar I haven't seen a formal answer from the designers. It is good that people like Michal do their best to find and describe work-arounds, but it leaves me with an awkward feeling about this unreliable drools implementation/version.

Posted by pz1 at Mar 26, 2014 12:39
Document generated by Confluence on Jun 05, 2016 09:34