Dynamic timer for heating

Hello,

I’m trying to start an heater for x time before for y time, depending on the actual temperature, so temperature should be reached at an moment i want. I found a script on this forum wich was very helpfull, but i’m not able to pass an argument to the function in my script. I’m no progammer, so i make my rules out of examples found on the web. In the examle I found there was no parameter used, and i found createTimerWithArgument on a info page (ScriptExecution (openHAB Core 3.1.0-SNAPSHOT API)). I’m probably not using it the right way, and mayby it’s not possible at all.

var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution"); 
var ZonedDateTime   = Java.type("java.time.ZonedDateTime");  
var TimerMinutes    = items["BadkamerVerwarmingTimer"];
var SetpointTemp     = items["BadkamerTemperatuur"];
var SetpointTemp    = items["setpointBadkamerTemperatuur"];
var x, y, verschil;

if (this.timer != undefined) {
  this.timer.cancel();
}

function StelVerwIn() {
  z = arguments[0];
  events.sendCommand("BadkamerVerwarmingTimer", z);
  events.sendCommand("SW01", ON);
  this.timer = undefined;
}

verschil = SetpointTemp-SetpointTemp;
x = (verschil-3)*60+20;
if (x>140) { x=140; }
y = 150-x;
x = x + 10;
this.timer = ScriptExecution.createTimerWithArgument(ZonedDateTime.now().plusMinutes(parseInt(y)), parseInt(x), StelVerwIn);

For example I want to execute the rule at 5:00.
when the actual temperature is 16 degrees and the setpoint is 20 degrees, I want it to start at 6:10 and run for 90 minutes (it ends at 7:40)
if actual temp is 16.5 degrees, it should start at 6:40 and run for 60 minutes (it ends at 7:40)
if actual temp is 17.0 degrees it should start at 7:10 and run for 30 minutes (it ends at 7:40)

Hope this makes clear what i’m trying to accomplish.

I have an other rule wich starts the heater for a time set in item BadkamerVerwarmingTimer wich is allready working as desired.

I get this error:
java.lang.IllegalArgumentException: The argument ‘command’ must not be null.

and this is because z is not passed to the function on:

events.sendCommand("BadkamerVerwarmingTimer", z);

Regards,

Peter Hazelaar.

I’ve never used createTimerWithArgument in JavaScript but I suspect you have to define the function that get’s called to accept the argument.

function StelVerwIn(z) {

Always, when trying to figure out problems like this add lots and lots of logging and log out every variable at every step. That will show you where it goes wrong. If you log out z I bet you will see it’s either null or undefined.

Thank you Rich for the fast response. I was looking in the wrong direction. I added the logging, and variables were all NaN. there was still something wrong with the item names.

I think the following is now doing exactly what I wanted;

var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution"); 
var ZonedDateTime   = Java.type("java.time.ZonedDateTime");  
var Log             = Java.type("org.openhab.core.model.script.actions.Log");
var TimerMinutes    = items["BadkamerVerwarmingTimer"];
var Temp            = items["BadkamerTemperatuur"];
var SetpointTemp    = items["ZWaveNode009ZMNHIDFlushonoffthermostat_Setpointheating"];
var x, y, verschil;

if (this.timer != undefined) {
  this.timer.cancel();
}

function StelVerwIn() {
  z = arguments[0];
  Log.logInfo("script","variable (int) z =  " + z);
  events.sendCommand("BadkamerVerwarmingTimer", z);
  events.sendCommand("BadkamerThermostaat", 1);
  this.timer = undefined;
}

verschil = SetpointTemp-Temp;
x = (verschil-3)
if ( x < 0 ) { x = 0; }
x = x*60+20;
if (x>140) { x=140; }
y = 150-x;
x = x + 10;
Log.logInfo("script","variable verschil =  " + verschil);
Log.logInfo("script","variable (int) x =  " + x);
Log.logInfo("script","variable (int) y =  " + y);
this.timer = ScriptExecution.createTimerWithArgument(ZonedDateTime.now().plusMinutes(parseInt(y)), parseInt(x), StelVerwIn);

for completeness I will add the other script which is turns off the heater after timer (z) finished: This is almost 1:1 copy from a post on this forum, maybe usefull for someone else. This script is started when the heater is turned on.

var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution"); 
var ZonedDateTime   = Java.type("java.time.ZonedDateTime");  
var TimerMinutes    = items["BadkamerVerwarmingTimer"];
if (this.timer != undefined) {
  this.timer.cancel();
}

function turnOff() {
  events.sendCommand("ZWaveNode009ZMNHIDFlushonoffthermostat_Thermostatmode", 0);
  this.timer = undefined;
}

this.timer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(TimerMinutes), turnOff);

Now I added the other script aswell i’m thinking that the fact that the timers have the same name this posssibly give a conflict.?

Thanks again,

Peter.

1 Like