Rounding af variables

I am trying to round a variable before I write it to an item but I keep getting errors.

This is the rule that is not working:

rule "Power Useage Per Week"
	Time cron "0,30 * * * * ?"
	var Number weekUse = (PC_ElectricMeterKWh.deltaSince(now().minusDays(7)))
	var outvar = String::format("%.2f", weekUse)
	logInfo("Week_Use", "Week Useage{}", PC_week.state)

I don’t know why this does not work as I have another rule which is similar that does work:

rule "Current Cost"
	Time cron "0 * * * * ?"
	var Number PCcurrentcost = (PC_ElectricMeterKWh.state as Number) * 0.283272
	var outvar = String::format("%.2f", PCcurrentcost)

The error I get is:

09:21:00.710 [ERROR] [untime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Power Useage Per Week': f != org.eclipse.smarthome.core.library.types.DecimalType

I think the issue is, you’re casting the .state as Number in the second rule explicitly, while you’re not doing the same in the first rule, so it’s not matching the type when you try to store it into the Number variable. You should just be able to change that line to:

var Number weekUse = (PC_ElectricMeterKWh.deltaSince(now().minusDays(7)) as Number)

Thanks Bartus

I still get the same error. :frowning_face:

11:38:30.002 [ERROR] [untime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Power Useage Per Week': f != org.eclipse.smarthome.core.library.types.DecimalType

I would place a loginfo statement after that line and see what you’re storing into the variable - somehow it’s not being read correctly from the persistence dB.

The weekUse variable has this in the log so it seems alright it just does not get past the next line.

21:37:30.406 [INFO ] [clipse.smarthome.model.script.weekuse] - Week Useage42.4297000000000216459739021956920623779296875
logInfo("weekuse", "Week Useage{}", weekUse)

Typically you should not round numbers until the last possible moment. This usually means only round when showing the number to a human or printing the value to the log, or when working with money. So the question is why do you need to round it before posting it to an Item?

Calling postUpdate is not a transaction and it does not happen instantly. It is unlikely that PC_week.state will equal the new state you just postUpdated when the logInfo executes. You already know what it should be so log out outvar instead.

It probably can’t figure out how to convert PC_week.state to a String. Try adding toString.

Thanks Rich not exactly sure what you are talking about with the toString.

I use the value to send an email report each week and I also display it on a hab panel so I thought I would just round it in one spot so i don’t have to do it twice. I had no trouble rounding the number on the hab panel but just don’t seem to be able to get it to work with the weekly email report.

Doesn’t this need to be

logInfo("Week_Use", "Week Useage " + PC_week.state)

logInfo(“Week_Use”, “Week Useage{}”, PC_week.state.toString)