[SOLVED] Total running time

Hello Everybody

  • OpenHab 2.3.0.005 running on Synology 6.2.1

I have rules that calculates how long my air conditionning works in a day. I calculate the time between the starting hour and the finnishing hour. After that, I would like to add the difference to the previus value

val DateTimeType startVMC = vmcStart.state as DateTimeType
	val DateTimeType stopVMC = vmcStart.state as DateTimeType
	val Number workTime = Minutes::minutesBetween(new DateTime(startVMC.calendar.timeInMillis), now).getMinutes()
	vmcDuree.postUpdate(workTime)

What I would like to do is

vmcDuree = vmcDuree + workTime

but, I am not able to get the value

Thanks for your help

val Number duration = vmcDuree.state + workTime
vmcDuree.postUpdate(duration) 

Thank you Vincent. Unfortunately it is not working.

In the log I have an error

Rule ‘VMC Last Stop’: An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(org.eclipse.smarthome.core.items.Item,java.lang.Number) on instance: null

Add some logInfo statements after each line to see where the rule fails

The problem comes from

	vmcDuree.postUpdate(workTime)
	
	val Number vmcduration = vmcDureeTot.state //This line gives me a probleme
	val Number duration = workTime + vmcduration
	vmcDureeTot.sendCommand(duration)

If I replace vmcDureeTot.state by a number (12 for exemple), everything works well

What is vmcDureeTot?

It is

String    vmcDuree "Dernière durée de fonctionnement [%s]"    <vmcduree> (gRestore)   
String   vmcDureeTot  "Durée de fonctionnement [%s]"    <vmcduree> (gRestore)

Change these to Number

Unfortunately, it is still not working
My items :

String 				vmcStart	"Dernier démarrage [%1$td.%1$tm.%1$tY %1$tH:%1$tM]"	<vmcon> (gRestore) 
String 				vmcStop		"Dernier arrêt [%1$td.%1$tm.%1$tY %1$tH:%1$tM]"		<vmcoff> (gRestore) 
String 				vmcDuree	"Dernière durée de fonctionnement [%s]"		 <vmcduree> (gRestore) 
Number 				VmcDureeTot	"Durée de fonctionnement [%s]"		 <vmcduree> (gRestore) 

My actual rules :

rule "Last Start"
when
    Item VMCslow changed to ON
then
	vmcStart.sendCommand(new DateTimeType)
end

rule "VMC Last Stop"
when
    Item VMCslow changed to OFF
then

	vmcStop.sendCommand(new DateTimeType)

	val DateTimeType startVMC = vmcStart.state as DateTimeType
	val DateTimeType stopVMC = vmcStart.state as DateTimeType
	val Number workTime = Minutes::minutesBetween(new DateTime(startVMC.calendar.timeInMillis), now).getMinutes()

	vmcDuree.postUpdate(workTime)
	
	val Number vmcduration = VmcDureeTot.state
	val Number duration = workTime + vmcduration
	VmcDureeTot.sendCommand(duration)
end

and logs :

Rule 'VMC Last Stop': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.lib.NumberExtensions.operator_plus(java.lang.Number,java.lang.Number) on instance: null

Add some logs to see where it breaks and the values of your variables

Here the rules with Logs

rule "Last Start"
when
    Item VMCslow changed to ON
then
	vmcStart.sendCommand(new DateTimeType)
	logInfo("vmc.rules", "Start")
end

rule "VMC Last Stop"
when
    Item VMCslow changed to OFF
then

	vmcStop.sendCommand(new DateTimeType)

	val DateTimeType startVMC = vmcStart.state as DateTimeType
	val DateTimeType stopVMC = vmcStart.state as DateTimeType
	val Number workTime = Minutes::minutesBetween(new DateTime(startVMC.calendar.timeInMillis), now).getMinutes()

	vmcDuree.postUpdate(workTime)
	logInfo("vmc.rules", "1")
	val Number vmcduration = VmcDureeTot.state
	logInfo("vmc.rules", "2")
	val Number duration = workTime + vmcduration
	logInfo("vmc.rules", "3")
	VmcDureeTot.sendCommand(vmcduration)
	logInfo("vmc.rules", "4")
end

Logs show me “2”

and after the error

I found my mistake

My “Number” VmcDureeTot was not defined. So the calculation did not Work. I had to set the value to 0 and after it works.

Thank you Vinvcent for your help

Hi Cyril,

Sorry but i didnt understand what you meant with " My “Number” VmcDureeTot was not defined." Can you explain a little bit?

Thanks Costa

I can only guess that the item VmcDureeTot was never assigned a value, so it remained in UNDEF.