Show of the item in group, that last updated the time stamp

I have a group of motion detectors, that shows the time stamp of the device that detected the motion last. How can I dosplay the name and location in the footer of the corresponig label cell?

This is a more complicated question than you might think at first. There’s no simple shortcut to that information built in to the widget system or even in OH in general. It is possible. In fact, there are several ways of doing it, but none of them are going to be the straightforward answer your likely hoping for. Here are just a few of the options off the top of my head from best to worst.

  1. Honestly, the best way to do this would be to use a rule. You can easily trigger a rule every time the state of your group changes (or every time a member of the group changes) and with a simple script you could pass the information about the item that has changed and it’s location to a string item created specifically to hold this information. Then the widget is as simple as possible, because the footer just displays the state of this string item. You could pretty easily build this script with blockly. Or, a JSscripting example with recent version of the helper library would look something like this:
var lastItem = items.getItem(event.itemName)
var itemLabel = lastItem.label
var itemLocation = lastItem.semantics.location
items.getItem('stringInfoItem').sendCommand(`Item: ${itemLabel} in ${itemLocation}`)
  1. Through the use of a repeater you could automatically collect most of this information in a custom widget and then have the repeater populate the label cell. This would require some more advanced use of the oh-repeaters including filters and fetching metadata, and it would no longer be a simple oh-label-cell you are adding to your pages, but a full custom widget.

  2. It is possible to construct a single widget expression in the cell footer that extracts this information completely within the widget. This is not the best answer for a couple reasons. First, depending on how many items in your group the expression is going to get very long and hard to maintain. Second much of the information would have to be hard coded into the expression so if, for example you added another item to the group later on, you have to go in and update the expression manually.

Thanks for the detailed description. The rule thing totally makes sense. However, my Openhab does not execute even a simple script in rules. Boiled it dow to

items.getItem(‘lastActivity’).sendCommand(‘red’);

and the non-semantic string item ‘lastActivity’ is not being changed. Do I have to install anything additional to run rules?

Thanks

WR

  1. When testing a rule, always add a some logging so that you can see what the rule is doing, and always monitor your openhab logs (that’s where rule log output will be and that’s where error will be shown). Without these you are just guessing at whether the rule runs at all and what the possible error could be. It is, quite frankly, nearly impossible to successfully debug a script without this information. If you are sticking with the javascript example above logging would look like this:
console.log('some info here')

If I were having trouble with the script sample above to debug it I would do something like this:

console.log(`Script runs. Event item is ${event.itemName}`)
var lastItem = items.getItem(event.itemName)
console.log('Item info retrieved')
var itemLabel = lastItem.label
console.log(`Item label is ${itemLabel}`)
var itemLocation = lastItem.semantics.location
console.log(`Item location is ${itemLocation}`)
items.getItem('stringInfoItem').sendCommand(`Item: ${itemLabel} in ${itemLocation}`)
console.log('Script complete. New command sent.')

That looks like overkill at first, but what that does is tell you exactly, if the script fails, how far through the process it got before it failed and which script line to target for debugging.

  1. Yes. Each rule script has its own add-on that must be installed for those scripts to run. Assuming you are on a recent version of OH, then some should already be installed, but it’s possible they are not. To use the javascript example above (or to do this with blockly) Go to Add-on storeAutomation and make sure that JSScripting is installed.

Had to install JS scripts as an ad-on. Now it runs. Thanks!