now.minusMinutes(5) string issues

I am trying to get the avg GPM of my water meter, this worked with db4o, but changing to MySQL now breaks it:

rule "Water Rate"
  Time cron "0/5 * * * * ?"
  var ro_meter = RO_Water_Meter.deltaSince(now.minusMinutes(5), "mysql")
  if (ro_meter !== null) {
    logInfo("Water Meter", ro_meter)
  } else {
    logInfo("Water Meter", "null")

If I add .toString after my var such as:

var ro_meter = RO_Water_Meter.deltaSince(now.minusMinutes(5), "mysql").toString

It works, but I now can’t do the math I need (divide by 5).

Try something like this:

var ro_meter = RO_Water_Meter.deltaSince(now.minusMinutes(5), "mysql") as Number

Thanks, I did try that and got:

2021-13-06 05:13:10.010 [ERROR] [org.eclipse.smarthome.model.rule.runtime.internal.engine.ExecuteRuleJob ] - Error during the execution of rule 'Water Rate': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

However, I see from my MySQL log that it is not null:

06-Jan-2021 17:14:03.889 [DEBUG] [persistence.mysql.internal.MysqlPersistenceService] - mySQL: query: INSERT INTO Item6 (TIME, VALUE) VALUES(?,?) ON DUPLICATE KEY UPDATE VALUE=?;
06-Jan-2021 17:14:05.002 [DEBUG] [persistence.mysql.internal.MysqlPersistenceService] - mySQL query: item is RO_Water_Meter
06-Jan-2021 17:14:05.002 [DEBUG] [persistence.mysql.internal.MysqlPersistenceService] - mySQL: query:SELECT Time, Value FROM Item11 WHERE TIME<'2021-01-06 17:09:05' ORDER BY Time DESC LIMIT 0,1
06-Jan-2021 17:14:05.004 [DEBUG] [persistence.mysql.internal.MysqlPersistenceService] - mySQL: query returned 1 rows in 2ms

Try to check the individual steps with some logInfos:

logInfo("0", RO_Water_Meter.state)
logInfo("0.5", RO_Water_Meter.state.toString)
logInfo("1", RO_Water_Meter.deltaSince(now.minusMinutes(5), "mysql"))
logInfo("2", RO_Water_Meter.deltaSince(now.minusMinutes(5), "mysql").toString)
logInfo("3", RO_Water_Meter.deltaSince(now.minusMinutes(5), "mysql").toString as Number)
logInfo("4", RO_Water_Meter.deltaSince(now.minusMinutes(5), "mysql") as Number)

This will help to see what the status is and where exactly the problem appears.

The basic logInfo(X,Y) requires two strings. It’s not smart enough to deal with a non-string.

This will blow up- it’s a number.

This should work(except that the rule has already blown upon the previous line)

This does indeed work, but the problem I have is its a String and not a number so I can’t / by 5 like I need to.

Well, yes. For the logInfo() you want it as a string, to go in a message. If you want to do maths, yep you need a number. It’s up to you how you treat it on each occasion.

var myresult = RO_Water_Meter.deltaSince(now.minusMinutes(5), "mysql") as Number
// variable is a number
logInfo("result1", myresult.toString)
// we get the string version for our text message
// but the variable is still a number
var otherresult  = myresult / 5 
// so we can do maths on that
logInfo("result2", otherresult.toString)

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