This page last changed on Dec 24, 2013 by sattva.

Recently found timer-code from NIR AVIRY and JOE MILLER - Thank you guys!!!

It works fine, but I have very few brain and don't understand how to restart this timer.
The idea is: with every used liter of water we need to restart the timer.
As soon as the water will be no longer used, timer will countdown to zero.
At this time water volume in boiler will be 100% - full boiler.

Please help.

After Button pressed (button - water counter emulator) timer should restart and coundown some X seconds.

rule "Button pressed"
when
  Event (source == "SensButtonsOnOff") // Check if Water button was pressed
  CustomState (source == "SensImpulses", $Liters: value)
then
  System.out.println ( "Button pressed" );
  Integer iLitersCnt = Integer.parseInt($Liters.toString() );
  iLitersCnt = iLitersCnt + 1;
  String sLtCnt = String.format("%d", iLitersCnt);

  // $countdown.setTime_left(iLitersCnt*5);  // <==== Must be something like this. ;-)
  // iLitersCnt*5 = 5 seconds to boil every liter
end
//----------------------------------------------------------------------------------------

Working countdown code:

package org.openremote.controller.protocol

global org.openremote.controller.statuscache.CommandFacade execute;
global org.openremote.controller.statuscache.SwitchFacade switches;
global org.openremote.controller.statuscache.LevelFacade levels;
global org.openremote.controller.statuscache.RangeFacade ranges;
import org.openremote.controller.model.event.*
import java.io.*;
import java.util.Timer;
import java.util.TimerTask;
import java.lang.Float;
//------------------------------------------------------------------------------------------
declare WtTimer1
  time_left : int
end
declare WtTimer1_Prev
  wt_old_state : boolean
end
//----------------------------------------------------------------------------------------
rule "Boiler timer initialized"
no-loop
when
  eval (true)
then
  WtTimer1_Prev wt_state = new WtTimer1_Prev();
  wt_state.setWt_old_state (false);
  insert (wt_state);
  System.out.println ( "Boiler timer initialized" );
end
//----------------------------------------------------------------------------------------
rule "Boiler timer start" 
dialect "mvel"
no-loop
when
 CustomState (source == "SensImpulses", $Liters: value)
 $wt_status : WtTimer1_Prev()
 eval ($wt_status.wt_old_state == false )
 Switch ( source == "SensBoilerStatusOnOff", value == "on" )
then
 WtTimer1 status = new WtTimer1();
 status.setTime_left(60); // <============= Would be nice to set walue from variable Integer.parseInt($Liters.toString() ) * 5 sec per liter and restart the timer 
 insert (status);
 $wt_status.setWt_old_state(true);
 System.out.println ( "Water Tank was turned ON for 60 seconds." ); 
end
//-----------------------------------------------------------------------------------------
rule "Boiler Off CountDown" 
timer (cron: 0/20 * * * * ?) // Cron for every 20 seconds
when
  $countdown : WtTimer1()
  Switch ( source == "SensBoilerStatusOnOff", value == "on" )
then
  $countdown.setTime_left($countdown.getTime_left() - 20);
  System.out.println ( "Water Tank Time Left is: " + $countdown.getTime_left() +" sec");
  Integer iSecLeft = $countdown.getTime_left();
  String sSecLeft = String.format("%d", iSecLeft);
  execute.command ("Timer", sSecLeft +" sec");
  if ( $countdown.getTime_left() < 20) { execute.command ("BoilerOff", "off"); }
end
//----------------------------------------------------------------------------------------

Solution found!

Lower at the page we can see another part of NIR AVIRY's code:

rule "Button pressed"
no-loop
when
  $WTTimerReset : WtTimer1()  // <== WtTimer1 object created to access its property Time_left
then
  $WTTimerReset.setTime_left (80); // <== set new value to Time_left property of WtTimer1
  insert ($WTTimerReset);        // <== 
end

Complete code of the button:

rule "Button pressed"
no-loop
when
  $WTTimerReset : WtTimer1()  // <== WtTimer1 object created to access its property Time_left
  Event (source == "SensButtonsOnOff") // Check if Water button was pressed
  CustomState (source == "SensImpulses", $Liters: value) // Used to callculate new value of Time_left
then
  $WTTimerReset.setTime_left (80); // <== set new value to Time_left property of WtTimer1
  insert ($WTTimerReset);        // <== 
  
  System.out.println ( "Button pressed" );
  Integer iLitersCnt = Integer.parseInt ($Liters.toString() );
  iLitersCnt = iLitersCnt + 1;
  String sLtCnt = String.format("%d", iLitersCnt);
  execute.command ("Impulses",  sLtCnt);
  execute.command ("BoilerOn", "on");
end
Posted by sattva at Dec 24, 2013 15:31
Document generated by Confluence on Jun 05, 2016 09:39