Best pattern for tracking "last change" in OH3?

I’d like one item to maintain the time of last change of another.

I started by setting up the “timestamp on change” profile when linking the item to the channel (of an MQTT thing). The (DateTime) item never changed from NULL. I can’t find any reference to whether the behavior I was hoping for is the expected behavior for this setup or not.

Second though was a simple rule. Easy to set up the condition, then a couple of options for the “then” part of the rule:

  • Use an ECMAscript or DSL script to send the time. I know how to do this, but thought there should be a more straightforward way.
  • Use an Item action, and send the current time as the command. No indications I could find in the documentation or GUI as to how to do this. Can I use a formula in the “command to send” field? what engine processes that formula?
  • Use a Blockly script. This seems promising, "PostUpdate _now + 0 seconds)_ to _{item}_ looks like it generates the correct script, but at runtime, I get
    State '2022-11-01T21:45:17.689302-04:00[America/New_York]' cannot be parsed for item 'HVACSensors_HeatCallChange'

I’m on the 3.3.0 release build.

Appreciate any insight on the most elegant way to approach this.

This works fine for me in 3.3. When the channel changes, the item records a date time.

I just tried making a new item in the UI and connecting it to an MQTT device. The item immediately changed to the current date/time when I connected to an MQTT channel, and then changed again when I toggled the device. So if your item remains as NULL , then something’s not right.

1 Like

No, you need a script action to do this. If you use a rule this is as straight forward as it gets.

Item action only supports hard coded values. If you need a formula or to do math or reference something, you have to use a script action.

This is essentially the same thing as your Rules DSL solution except using Blockly instead of Rules DSL. I’m not sure the source of the error though. That would need further exploration.

Beyond that, as @rpwong indicates, the profile should work too. So something is going on that isn’t apparent from the information we’ve been given. Have you verified that the Channel is working? You have another Item linked to this same Channel and it’s changing as expected?

Yes, the other item works as expected. I’ll post more details when I get home.

What’s your use case for this? If it is to do some calculations on this in a rule, then I found, you don’t need a extra item for it. If you have persistence running (works with Standard-rrd4j also), you can use a plethora of meta-data as describe here in " Persistence Extensions in Scripts and Rules":

A simple informational display of “last time the boiler had a heat call”, so I need an item to support the display.

I’m curious what will happen if you try making a new item and link it to a channel for a different device. I used an outlet, so that I could toggle it manually.

As noted previously, the item immediately updated from NULL to the current time as soon as I linked to the device channel with the “Timestamp on change” profile. I assume that’s because openhab registered the new channel link as a change.

We need to figure out why yours didn’t update immediately.

Here’s the item definition from the channel link view. I also tested with the semantic “Point” removed.

Both items linked to the channel. Heat Call changed from “OPEN” to “CLOSED” when I turned the thermostat up, with no corresponding update to HeatCallChange

Can you try it with another channel? Any switch or outlet that you can control directly, and that is not a contact sensor.

It’s strange that your profile options are out of order.

image

If you look at my screenshot above, they’re listed alphabetically. I don’t know why yours would be different.

I may have been dealing with an artifact of an earlier version; the install was initially done just after the 3.0 release.
I unlinked the item from the channel and found I could not re-link it, as the item picker only allows items of type “contact”, and there’s no way to specify the profile before picking the item (I also tried the same on a dimmer channel with similar results). I created a new item from the link GUI, selected the appropriate profile and set. my item type to DateTime, and it worked. I subsequently deleted the original item and recreated it so I could reuse the name, and it appears to be working. Thanks for helping me along this journey; now I can go back to debugging the CSS on my widget.

1 Like