[SOLVED] Rule with Math Formula Throwing an Error

Hi all, been messing with this and keep getting errors. I have a math formula that I am trying to run in a rule. I have setup and item called g_TemperatureSensors_AverageCelsius and then run the following rule:

rule "Saturation Vapor Pressure"
when
	Item g_TemperatureSensors_AverageCelsius received update
then
	Saturation_Vapor_Pressue.postUpdate (610.78 * 2.71828 (g_TemperatureSensors_AverageCelsius.state as Number / (g_TemperatureSensors_AverageCelsius.state as Number + 238.3) * 17.2694))
	logInfo("Saturation Vapor Pressure",Saturation_Vapor_Pressue.state.toString)
end```

I get this error:

```csv
Configuration model 'vps.rules' has errors, therefore ignoring it: [5,56]: missing ')' at '('
[5,183]: mismatched input ')' expecting 'end'

I search where it says the issue is and thats in the middle of one of the numbers. I count the () and everything looks good to me.

thoughts?

Thanks!

First, are these “plain” Numbers or are there units involved?

This part seems to be missing an operator
... * 2.71828 (g_TemperatureSensors_AverageCelsius ...
Do you want to add or multiply or subtract between number and brackets?

Maybe do the calculation step by step until you have it working, and only then jam everything in one line.

Be aware that postUpdate is asynchronous, sending off an update request that will get actioned shortly. It does not stop and wait before executing the following lines, so your logInfo will most likely show you the “old” state of Saturation_Vapor_Pressue (did you mean that spelling?)

Thanks @rossko57 i’ll work through it just one piece at a time. great idea!

yes, spelled it wrong so corrected in all places.

Will post an update!

This has really been a bear. I started through trying to break it down. Easy just to * the items, got that.

But I kept getting other errors so finally went all the way to this…

Item

Saturation_Vapor_Pressure.postUpdate (g_TemperatureSensors_AverageCelsius.state as Number / (g_TemperatureSensors_AverageCelsius.state as Number + 238.3)

Group Item

Group:Number:AVG g_TemperatureSensors_AverageCelsius "[%.1f °C]" <temperature> (g_TemperatureSensors)

Rule

rule "Saturation Vapor Pressure"
when
	Item g_TemperatureSensors_Average received update
then
//	Saturation_Vapor_Pressure.postUpdate (610.78 * 2.71828) 
//	Saturation_Vapor_Pressure.postUpdate (g_TemperatureSensors_AverageCelsius.state as Number / (g_TemperatureSensors_AverageCelsius.state as Number + 238.3) * 17.2694))
    Saturation_Vapor_Pressure.postUpdate (g_TemperatureSensors_AverageCelsius.state as Number)
	logInfo("Saturation Vapor Pressure",Saturation_Vapor_Pressure.state.toString)
end

Error:

2019-08-14 16:10:22.272 [INFO ] [me.model.script.Greenhouse Temp Rule] - 101.1
2019-08-14 16:10:22.273 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Saturation Vapor Pressure': Could not cast NULL to java.lang.Number; line 8, column 47, length 51
2019-08-14 16:10:22.283 [INFO ] [e.model.script.Greenhouse Thermostat] - 40

Have to get buy this to get to the next test…

Thanks!

I don’t understand this. This is not an Item definition, it’s more like a line in a rule.

Okay, this is plain enough. There’s only one place in that rule where something is being cast to Number
(g_TemperatureSensors_AverageCelsius.state as Number)

Every Item is born with state NULL at system boot time. That’s the way they stay, unless something gets done to them, usually by binding or rule.
You can’t cast state NULL to a Number, not even zero, it’s just unknown. So that’s the reported error.

Your Item g_TemperatureSensors_AverageCelsius happens to be a Group type Item. Groups can have states like ordinary Items, but they cannot be set by rules or bindings, only by the aggregation function (if one is given).

Here you’ve given the AVG average function. That’s only going to give a meaningful answer if the group’s member Items have meaningful numeric states themselves. Perhaps there are no members, perhaps they are still NULL, perhaps they are non-numeric.

You can test for state NULL in and a rule and skip trying to process it.

if (g_TemperatureSensors_AverageCelsius.state != NULL) {
   // do stuff
} else {
   logInfo("surprise", "Unexpected NULL state"}
}

Ahh ok, getting a little clearer.

Correct on the item, copied the wrong thing in.

I will back up a little. I have two sensors in the greenhouse (one front, one back) that I use to create an average with. Here are the items and then the average…

Number Back_Indoor_Temperature "Back Indoor Temperature [%.1f °F]" <temperature> (g_TemperatureSensors, g_TemperatureSensors_Average) ["CurrentTemperature"] {mqtt="<[broker:tele/sonoff-00th161/SENSOR:state:JSONPATH($.AM2301.Temperature)]"}

Number Front_Indoor_Temperature "Front Indoor Temperature [%.1f °F]" <temperature> (g_TemperatureSensors, g_TemperatureSensors_Average) ["CurrentTemperature"] {mqtt="<[broker:tele/sonoff_7F5C1F/SENSOR:state:JSONPATH($.AM2301.Temperature)]" }

Group:Number:AVG g_TemperatureSensors_Average "Indoor Average Temperature" <temperature> (g_TemperatureSensors)

I use that g_Temperature to fire of controls in the greenhouse.

So in my infinite wisdom, I decided if I created an item like this

Group:Number:AVG g_TemperatureSensors_AverageCelsius "[%.1f °C]" <temperature> (g_TemperatureSensors)

That it would create the average temp in Celsius. That is what is not working, I think…
My thought was, when I created the g_TemperatureSensors_Average it would also create it in Celsius.

I honestly know little about how UoM units work with Groups.

To have any chance of working the way you wanted, you would need the Group states to be dimensioned types to begin with, and then you would need to specify units - that part I think you have got right in the label [format] part.

You would have to give the Group some members though - it cannot guess what you want.

I don’t know what happens if a dimensioned Group has non-dimensioned members.

Relevant

Yeah! Ok, at least got the formula figured out. Help’s when you have some data in the item you are trying to use. This is the final formula:

        Saturation_Vapor_Pressure.postUpdate (((610.78 * 2.71828) * (g_TemperatureSensors_AverageCelsius.state as Number / ((g_TemperatureSensors_AverageCelsius.state as Number + 238.3) * 17.2694))))

Now, just the one problem, @rossko57 I cannot get the temp to convert. I checked out the link you sent and it seems to be exactly the same as what I am doing. What it logs is the temp in F just like the other group item. The C does not seem to be changing it. I may need to post that in a new thread.

Thanks for your help on this!

“it” ?
Maybe you mean a group of your temperature sensor Items?
That’s not going to work because your temperature sensor Items are just numbers. A group wouldn’t know they’re supposed to represent temperatures, so it’s not going to do any conversion.

I ended up figuring it out. I had to create a rule to convert to Celsius. I am sure there was a better way to do it but its working! Now I know I have some Vapor Pressure Deficit issues in the greenhouse! Dang, one more thing to fix…

Thanks for your help @rossko57