Display date/time change occurred in sitemap

  • Platform information:
    • Hardware: i5-4690k/16GB RAM
    • OS: Windows 10 Home
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: 2.5.4

Hello All!
I am trying to display the date and time that something occurred and display said date and time on my sitemap. I have two items I want to do this with:

Switch MultiSensor_Motion "Motion" <presence> (Virtual) {channel="zwave:device:44dc47e2:node3:alarm_motion"}

Contact Slide_Door_Sensor_Contact "Front Door" (Kitchen) {channel="zwave:device:44dc47e2:node4:sensor_door"}

So when the motion detector triggers, or when the door sensor state is changed, I’d like to display this under a “Motion Last Detected …” or “Door Last Opened…” sitemap frame.

I have tried adapting other’s rules to my situation, with no luck so far. Here’s the current nonworking rule:

import java.text.SimpleDateFormat
import java.util.Date

rule "Multisensor 6 Motion"
        Item MultiSensor_Motion changed
        var epoch = new DateTime((MultiSensor_Motion.state as Number).longValue)
        val DateTime datetime = new DateTime(epoch)
        val String time = datetime.toLocalTime().toString("HH:mm")
        val String date = datetime.toLocalDate().toString("dd-MM-yyyy")
        logInfo("motion.detection.rules", "Trial Part 1" +  time.toString + " op " + date.toString)
        MultiSensor_Motion.sendCommand(datetime , new DateTimeType() ) 

Any help with this would be greatly appreciated. In addition, this is my first post, so I apologize if I broke any community rules.

Would the method described in this post do what you want? No rules needed!

The root of your problem here is that MultiSensor_Motion is a Switch type Item. You can’t send a Switch a datetime for a command. You especially cannot send it two datetimes for one command.

As you’ll find in design note @hafniumzinc suggested, you really need to use a separate Item.

The limitation with a profile method is that it always updates the timestamp. If you instead choose to use a rules based method, you get more control so that for example you can truly record when the door opens (and not when it closes).

Thank you both for this! The profile method worked flawlessly; I am now working to display the date and time in a more user-friendly format. Currently it’s “2020-07-19 20:11”, and I’d like it to be a bit more ‘friendly.’

@rossko57, what would a rule like that look like? Do you have a template or another forum post you could link me to?

Thank you.

It would not be a complicated rule. All the necessary parts are in your first post, just a bit jumbled up.

rule ‘xx’
something happens - you have choices here
post a “now” value to your timestamp Item

And/or you can use if() within your rule to act only under certain conditions.

Look at examples of date time formatting in the [state presentation] part of your timestamp Item’s label

Thank you again @rossko57! This was massively helpful.