[SOLVED] Rule to convert Time to String

Platform information:

  • Hardware: Raspberry Pi 3B
  • OS: Raspbian Buster
  • Java Runtime Environment: Zulu (openjdk version “1.8.0_152”)

I guess this is a rather silly question and similar problems have been discussed here before. I’ve done a lot of reading but for some reason I don’t get it to work.

I have a rule that is supposed to combine the time of the last update with the state of a device into a string. So the desired output in the sitemap is something like

Water Meter: 19:17:23 - OK

So I have a string item

String  sWaterMeterStatus     "Water Meter: [%s]"

and a rule

rule "rule_waterMeter_status"
        Item nWaterMeterReading received update
      var String sWaterMeterStatus = sWaterMeterStatus.state
      //var String sWaterMeterLastUpdate = new DateTimeType().toString("HH:mm:ss")
      var String sWaterMeterLastUpdate = new DateTimeType().toString()
    logInfo("rule_waterMeter_status", "sWaterMeterStatus = " + sWaterMeterStatus )
    logInfo("rule_waterMeter_status", "sWaterMeterLastUpdate = " + sWaterMeterLastUpdate)
    sWaterMeterStatus.postUpdate(sWaterMeterLastUpdate  + sWaterMeterStatus )

I’ve tried a lot of different suggestions from the community but I always get errors when converting the dateTime to a string.
Just an example:

Rule ‘rule_waterMeter_status’: An error occurred during the script execution: Cannot resolve proxy: java:/Objects/org.joda.time.format.DateTimeFormatter#org.joda.time.format.DateTimeFormatter

Do I need a special include / import file?

Any help is greatly appreciated!

This might help you

1 Like

Thanks, but I’ve read that information before only to be left with more confusion.

Originally I wanted to use the item.lastUpdate mechanism. But I didn’t get it to work. So instead I just use the current time when an item is updates.

After a lot of more trial and error I now came up with this which seems to work:
rule “rule_waterMeter_status”
Item sWaterMeterStatus received update
var String wmStatus = sWaterMeterStatus.state.toString
var String wmLastUpdate = new DateTimeType().format("%1$tH:%1$tM:%1$tS")
sWaterMeterLastUpdateStatus.postUpdate(wmLastUpdate + " - " + wmStatus)


That is only available when you have configured persistence, the Item is configured to persist, and the persistence type supports lastUpdate.

Trying to get item.lastUpdate to work I found this exact hint that you’ve posted to someone else’s question.
The item is included in RRD4J persistence, but it still didn’t work. I assume the reason again was that the conversion to a string variable failed.

Because I have a working solution now there’s no need to go further into this except for ‘nice to know’ reasons.

1 Like