Say time and date

This rule gives the java error when the rule beeing executed: (no error when beeing saved).

rule    "Voice Testing Rule"
	when
		Item    Voice_Robot     changed from OFF to ON  
	then
 say("Seneste opdatering var kl. " + String::format("%1$tH:%1$tM %1$td.%1$tm.%1$tY", NetamoIndendoersMeasuretime.state.toString))

end

And the error:

2019-02-27 23:24:04.097 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Voice Testing Rule': H != java.lang.String
1 Like

Get rid of all the $

The same error.

1 Like

All I can recommend is look at the examples on the forum. I’m not in a position where I can research this right now.

1 Like

Thx Rich…
I searched for examples and found quite a few… I dont really see why it shouldnt work… The only thing I can think of is that the format is wrong in some way. But since I have no idea what the error message is all about, this is just a wild guess. So I have given up for now.

1 Like

Here is another example.

I think the issue is we need to convert the state to a Date first. It might work with a DateTime.

Try new Date(NetamoIndendoerMeasuretime.state as DateTimeType).zonedDateTime.timeInMillis

1 Like

finally had time to test this… Didn´t work…
This is the rule:

rule    "Voice Testing Rule"
	when
		Item    Voice_Robot     changed from OFF to ON  
	then
 say("Seneste opdatering var kl." + String::format( "%1tH:%1tM %1td.%1tm.%1tY", new Date(NetamoIndendoerMeasuretime.state as DateTimeType).zonedDateTime.timeInMillis ))

end

And when it fire:

2019-03-03 15:54:04.076 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Voice Testing Rule': An error occurred during the script execution: null
1 Like

Usually that is the result of a type error. Are you certain NetamoIndendoerMeasuretime isn’t NULL or UNDEF?

Also, check your parens. I don’t think they are right. They’re should be three on the end

  1. close the new Date
  2. close the String::format
  3. close the say
1 Like

110% possitive…

2019-03-03 22:27:41.305 [vent.ItemStateChangedEvent] - NetamoIndendoersMeasuretime changed from 2019-03-03T22:17:02.000+0100 to 2019-03-03T22:27:07.000+0100

7 minutes ago. Netamo updated every 10. minute.

There are three “(” and three “)”.

Yes but can’t just put them any old place. They define the scopes of the function calls. The scopes are

say -> String::format -> new Date

Therefore there should be three parens at the end.

I think you are missing an open paren before

NetamoIndendoerMeasuretime.state as DateTimeType)

and then the one at the end.

rule    "Voice Testing Rule"
	when
		Item    Voice_Robot     changed from OFF to ON  
	then
 say("Seneste opdatering var kl." + String::format( "%1tH:%1tM %1td.%1tm.%1tY", (new Date (NetamoIndendoerMeasuretime.state as DateTimeType).zonedDateTime.timeInMillis )))

end

Return with this error, when rule fires:

2019-03-04 23:05:36.537 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Voice Testing Rule': An error occurred during the script execution: null

Break the line up a bit and add some logging to see which of those calls is experiencing the type problem.

Which I knew how/where to breake it…
I actually thought off getting rid of the say, and just try to log insted. But I only see the NetamoIndendoerMeasuretime state, which makes no sense in logging, cause I already now what it is from the item…
Is it possible to log the new Date? I dont see its state anywhere?

It’s an Object, not an Item. It doesn’t have a .state. Just call .toString.

val st =  NetamoIndendoerMeasuretime.state as DateTimeType
logInfo("Test", "State = " + st)
val epoch = st.zonedDateTime.timeInMillis
logInfo("Test", "epoch = " + epoch)
val dt = new Date(epoch)
logInfo("Test", "Date = " + dt.toString)
val fmt = String::format( "%1tH:%1tM %1td.%1tm.%1tY", dt)
logInfo("Test", "Formatted = " + fmt)
val msg = "Seneste opdatering var kl. " + fmt
logInfo("Test", "Message = " + msg)
say(msg)

Are these linies suppose to go inbetween the line, you suggest I should break up?

These lines are the line I asked you to break up broken up. With logging in place so show the result at each stage of the operation.

Like this?

rule    "Voice Testing Rule"
	when
		Item    Voice_Robot     changed from OFF to ON  
	then
val st =  NetamoIndendoerMeasuretime.state as DateTimeType
logInfo("Test", "State = " + st)

 say("Seneste opdatering var kl." + String::format( "%1tH:%1tM %1td.%1tm.%1tY", (new Date (NetamoIndendoerMeasuretime.state as DateTimeType).zonedDateTime.timeInMillis )))

end

I tried, and it returned with a strange error, when fired…

2019-03-07 00:17:14.564 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Voice Testing Rule': The name 'NetamoIndendoerMeasuretime' cannot be resolved to an item or type; line 5, column 11, length 26

NetamoIndendoerMeasuretime is indeed an item, so I dont get it.

Not, just like I wrote it.

rule    "Voice Testing Rule"
	when
		Item    Voice_Robot     changed from OFF to ON  
	then
    val st =  NetamoIndendoerMeasuretime.state as DateTimeType
    logInfo("Test", "State = " + st)
    val epoch = st.zonedDateTime.timeInMillis
    logInfo("Test", "epoch = " + epoch)
    val dt = new Date(epoch)
    logInfo("Test", "Date = " + dt.toString)
    val fmt = String::format( "%1tH:%1tM %1td.%1tm.%1tY", dt)
    logInfo("Test", "Formatted = " + fmt)
    val msg = "Seneste opdatering var kl. " + fmt
    logInfo("Test", "Message = " + msg)
    say(msg)
end

I get the same error, and nothing else in the log.

2019-03-08 01:37:38.126 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Voice Testing Rule': The name 'NetamoIndendoerMeasuretime' cannot be resolved to an item or type; line 5, column 15, length 26

Check that the item make exactly matches how it is defined in your items file or PaperUI