Create a string which contains text and time&date (short format)

Hi,
just try to modify a rule which I found to “monitor” a letterbox.
(to monitor the (real) letterbox I build two contact into the letterbox; one contact send signal when new letter received, other send signal when the letter was removed from letterbox (door openend))

In one part from a “if” cause the string (“Letterbox_String”) should be like
“New letter received at 9:15 (27.12.)”
in the else-part: “Letterbox cleared at 10:35 (27.12.)”

Thats all :wink:

I found examples to get date & time (… (now.toString())…) and to format a date variable (like: “Text [%1$td.%1$tm. %1$tT]”

But how to create a simple string variable which contains a text part and a date/time -part (as part from the string) so I can use it in the sitemap?

Text item=Letterbox_String

val newText = String::format("Letterbox cleared at %1$td.%1$tm. %1$tT", DateTimeItem.state)

Thx for the code, but 2 questions:

  • DateTimeItem must be set before, correct?
    I try: “val DateTimeItem strDateTime = now” but as exepcted thats wrong syntax …

  • And “newText”, I can use later in the sitemap as : Text item=newText ?

Well, you do not provide nearly enough details above to give you anything more specific so you got the bare minimum response.

You need a Rule. The Rule needs to trigger on an event where the text should change. You need to have a String Item that gets the String and which you put on your sitemap. The Rule needs to get the date time from somewhere (now? and Item?), format the output with the other text, and post it as an update to the String Item.

No. That is a variable that only lives inside a Rule. you need to post it’s contents to an Item and put that Item on your sitemap.

Morning,
the dateTime can be set in the rule (actual time, date).
The rule was triggered from a contact, thats already in place.
Only the message, what the item on the sitemap than should display, should be bether formatted. Thats my question here :wink:

Actuall the item shows only the time: Letterbox_Last_Filling.postUpdate(now.toString())

And this should be replace that the (bether named) Letterbox_String have the content like:
Letterbox cleared at 10:35 (27.12.)

Maybe the first solution is fine, only the need to use this “string” (variable) in the sitemap.

Udo gave me support here,
the code

Letterbox_String.postUpdate("Letterbox cleared at " + Letterbox_Last_Filling.state.format("%1$tH:%1$tM (%1$td.%1$tm.)"))

gave me what I was looking for.

Thank Udo, thx, Rich