[SOLVED] Createtimer with calculated variable from multiple items states

Hi All,

first of all, I’m not a coder so what I’m doing is a lot of trial and mostly error :slightly_smiling_face:

anyway, to set the scene of what I’m trying to do: I have some blinds without a position feedback. this means that I’m trying to maintaiN. virtual position by keeping track off the run time up and down and from there calculating a new run time (up or down) based on the homekit target position.

I have 2 items per blind one linked to homekit and one linked to my thing.

the problem I now have is the runtime variable is not working what ever I try. the rest of the rule works fine if I had code the runtime values.


rule “Blindsl”

when
Item bl changed
then
var delta = (Blindsl.state as DecimalType) - (bl.state as DecimalType)
var runtimeup = Math::round(5 * 0.5).intValue
var runtimedown = Math::round(5 * -0.5).intValue
logInfo(“default.rules”, "Delta: " + delta )

if (delta > 0) {
	Blindsl.sendCommand(UP)
	createTimer(now.plusSeconds(runtimeup))
	[|
		logInfo("default.rules", "Timer finishes")
	Blindsl.sendCommand(STOP)
	]
	logInfo("default.rules", "Timer activated" + runtimeup)
	}
else {
	Blindsl.sendCommand(DOWN)
	createTimer(now.plusSeconds(runtimedown))		
	[|
		logInfo("default.rules", "Timer finishes")
	Blindsl.sendCommand(STOP)
	]
	logInfo("default.rules", "Timer activated " + runtimedown)
	}
Blindsl.sendCommand(bl.state)

end

it takes the blinds approximately 50 second for a full up or down.

but if I replace the 5 in var runtimeup = Math::round(5 * 0.5).intValue with delta which is the variable indicating the distance to travel from current to new position then I get the following error.

2021-01-03 14:05:27.240 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Blindsl’: An error occurred during the script execution: Could not invoke method: java.lang.Math.round(float) on instance: null

your help is much appreciated!!!

ps apologies if I haven’t followed the forum rules in terms of highlighting code or other requirements. I’m pretty new in all of This stuff.

I would deal in as Number types to begin with,it’s kind of the “native” form for DSL rules.
If that’s not an immediate fix, expand your operations to one step at a time and log out intermediate values.

How I love this community!!!

thanks for the tip it works now with the following code:

rule “Blindsl”

when
Item bl changed
then
var delta = (Blindsl.state as Number) - (bl.state as Number)
var runtimeup = (((Blindsl.state as Number) - (bl.state as Number)) * 0.5).intValue
var runtimedown = (((Blindsl.state as Number) - (bl.state as Number)) * -0.5).intValue

if (delta > 0) {
Blindsl.sendCommand(UP)
createTimer(now.plusSeconds(runtimeup))
[|
Blindsl.sendCommand(STOP)
]}
else {
Blindsl.sendCommand(DOWN)
createTimer(now.plusSeconds(runtimedown))
[|
Blindsl.sendCommand(STOP)
]}
Blindsl.sendCommand(bl.state)
end

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.