Design Pattern: Associated Items

Please see Design Pattern: What is a Design Pattern and How Do I Use Them for how to read and use DPs.

Problem Statement

Often one will have a number of separate Items which are all related to each other in some way. For example, one might have a Contact on a door and a DateTime to represent when the last time the door was opened. This is easy enough to keep track of if there are only one or two such Items, but if one has a lot of similar Items or is using a lot of generically coded rules where one cannot hard code the names of the associated Items inside the rule mapping between the Items becomes difficult.


Name Items that are associated with each other such that given the name of one Item, the names of the other Items can be created with just a little bit of String manipulation. With the associated Item’s name, one can get a reference to that Item by pulling it out of a Group’s members using the findFirst method. Or one can pull it out of the Item registry directly.


postUpdate Action

In this example there are two door Contacts and two associated DateTime Items to store the last time the Door was opened or closed. A Rule triggers when one of the Contacts is updated and it updates the associated DateTime Item.


Group gDoors:Contact

Contact Front "Front Door" <frontdoor> (gDoors)

DateTime Front_LastUpdate "Front Door Last Update [%1$tm/%1$td %1tH:%1tM]" <clock> 

Contact Back "Back Door" <frontdoor> (gDoors)

DateTime Back_LastUpdate "Back Door Last Update [%1$tm/%1$td %1tH:%1tM]" <clock> 


from core.rules import rule
from core.triggers import when
from org.joda.time import DateTime

@rule("A Door's State Changed", description="Tracks the date/time when a door changes state", tags=["entry"])
@when("Member of gDoors changed")
def door_changed(event):
    if isinstance(items[event.itemName], UnDefType): return
    events.postUpdate(event.itemName+"_LastUpdate", str( # TODO find non-joda equivalent

Rules DSL

rule "A Door's State Changed"
    Member of gDoors changed
    if(previousState == NULL) return;
    postUpdate("_LastUpdate", now.toString)

Theory of Operation

The Rule gets triggered when any member of gDoors changes. If the door changed from NULL we don’t care about this change so we exit the Rule. We then use the name of the door Item to create the LastUpdate Item’s name and send it and update with now.toString.

The key is the Item’s names and the line:

    # Python
    events.postUpdate(event.itemName+"_LastUpdate", str(
    // Rules DSL
    postUpdate("_LastUpdate", now.toString)

Note: This is one of the few examples where the postUpdate Action is recommended for use.

Group findFirst

In this example there are a bunch of Items that represent the online/offline status of devices and network services. When any of these Items change state to offline, generate an alert and set a flag indicating that the Item went offline. When that Item comes back online generate an alert and reset the flag. Use a timer to avoid flapping.


Group:Switch:AND(ON, OFF) gSensorStatus "Sensor's Status [MAP(]"

Group:Switch gOfflineAlerted

Switch vNest_Online "Nest Status [MAP(]"
    <network> (gSensorStatus)
    { nest="<[thermostats(Entryway).is_online]" }

Switch vNest_Online_Alerted (gOfflineAlerted)

Switch vNetwork_Cerberos "Cerberos Network [MAP(]"
  <network> (gSensorStatus, gResetExpire)
  { channel="network:servicedevice:cerberos:online", expire="2m" }

Switch vNetwork_Cerberos_Alerted (gOfflineAlerted)

// Dozens more Item pairs

Rules DSL

import java.util.Map

val Map<String, Timer> timers = newHashMap

rule "A sensor changed its online state2"
    Member of gSensorStatus changed
    if(previousState == NULL) return;

    var n = transform("MAP", "",
    val name = if(n == "") else n

    val alerted = gOfflineAlerted.members.findFirst[ a | =="_Alerted"] as SwitchItem
    if(alerted === null) {
        logError("admin", "Cannot find Item " +"_Alerted")
        aInfo.sendCommand( + " doesn't have an alerted flag, it is now " + transform("MAP", "", triggeringItem.state.toString) + "!")

    // If we are flapping, reschedule the timer and exit
    if(timers.get( !== null) {
        logWarn("admin", name + " is flapping!")

    // If alerted == OFF and triggeringItem == OFF then sensor went offline and we have not yet alerted
    // If alerted == ON and triggeringItem == ON then the sensor came back online after we alerted that it was offline
    if(alerted.state == triggeringItem.state) {
        val currState = triggeringItem.state
        // wait one minute before alerting to make sure it isn't flapping
        timers.put(, createTimer(now.plusMinutes(1), [ |
            // If the current state of the Item matches the saved state after 5 minutes send the alert
            if(triggeringItem.state == currState) {
                aInfo.sendCommand(name + " is now " + transform("MAP", "", triggeringItem.state.toString) + "!")
                alerted.postUpdate(if(currState == ON) OFF else ON)
            timers.put(, null)

Theory of Operation

See below for the JSR223 Python approach. There is no need to use findFirst in JSR223 Rules.

If the sensor changed from NULL or UNDEF we don’t care so immediately return.

Get the friendly name for the Item using Design Pattern: Human Readable Names in Messages.

Use the findFirst method to pull the associated Alerted Item from gOfflineAlerted based on the name of the triggeringItem. If the Item doesn’t exist, log the error and send an alert to indicate the configuration error (i.e. missing Alerted Item) and exit.

If there is already a timer set for this Item reschedule the timer.

If the alerted Item’s state equals the Item’s state it means we need to alert. Create a timer. If the timer doesn’t get cancelled and the triggering Item has remained unchanged send an alert.

Item Registry

Many thanks and full credit goes to @5iver for discovering this option. We can access Items from the Item registry directly using the ScriptServiceUtil. In JSR223 we have direct access to the Item registry.

In this example we have a Rule that turns on/off some lights depending on whether the weather says it is cloudy or not.


Switch vIsCloudy // set to ON when the weather says it is cloudy
String vTimeOfDay // represents the current time of day

Group gLights_ON_WEATHER // Group of lights to turn on when it is cloudy


from core.triggers import when
from time import sleep

@rule("Cloudy Lights", description="Turns ON or OFF some lights when it is cloudy during the day", tags=["lights"])
@when("Item vIsCloudy changed")
@when("Item vTimeOfDay changed")
def cloudy_lights(event):
    if items["vTimeOfDay"] != "DAY" or isinstance(items["vIsCloudy"], UnDefType): return

    if event.itemName == "vTimeOfDay": sleep(0.5)"It is {} and cloudy changed: {}".format(items["vTimeOfDay"], items["vIsCloudy"]))

    for light in ir.getItem("gLights_ON_WEATHER").members:
        if items["_Override"] != ON and light.state != items["vIsCloudy"]: events.sendCommand(light, items["vIsCloudy"])
        if items["_Override"] == ON: cloudy_lights("{} is overridden".format(

Rules DSL

import org.eclipse.smarthome.model.script.ScriptServiceUtil

// Thoery of operation: If it is day time, turn on/off the weather lights when cloudy conditions
// change. Trigger the rule when it first becomes day so we can apply cloudy to lights then as well.
rule "Turn on lights when it is cloudy"
    Item vIsCloudy changed or
    Item vTimeOfDay changed to "DAY"
    // We only care about daytime and vIsCloudy isn't NULL
    if(vTimeOfDay.state != "DAY" || vIsCloudy.state == NULL) return;

    // give the side effects of time of day time to complete
    if( == "vTimeOfDay") Thread::sleep(500)

    logInfo("lighting", "It is " + vTimeOfDay.state.toString + " and cloudy changed: " + vIsCloudy.state.toString +", adjusting lighting")

    // Apply the cloudy state to all the lights in the weather group
    gLights_ON_WEATHER.members.forEach[ SwitchItem l |

        val overrideName ="_Override"
        val override = ScriptServiceUtil.getItemRegistry.getItem(overrideName)

        if(override.state != ON && l.state != vIsCloudy.state) l.sendCommand(vIsCloudy.state as OnOffType)
        if(override.state == ON) logInfo("lighting", + " is overridden")

For Rules DSL, the key things of import are the Item’s name and the lines:

import org.eclipse.smarthome.model.script.ScriptServiceUtil
    val override = ScriptServiceUtil.getItemRegistry.getItem(overrideName)

For the Python version, an Item’s state is accessible by name using the items dict. If you have a need to get access to the Item itself, the equivalent of the ScriptServiceUtil line from above is ir.getItem("ItemName").

Alternative using Metadata

This is only an option for JSR223 Rules as Rules DSL does not provide a mechanism to access the metadata of an Item. See Design Pattern: Using Item Metadata as an Alternative to Several DPs for an example of the above complex example using metadata.

I particular, if you don’t need to show the last time that a door changed state on your sitemap, you could store that timestamp as metadata on the Item. In the findFirst example both the human friendly name access from the MAP and the alerted Item can be replaced with metadata (the example code at the link above is the JSR223 Python equivalent Rules using metadata for both of these). In the lights example, the Override flag could be replaces with metadata.

This does not mean that all cases where an associated Item would be used should be implemented as metadata. For example, if you have an irrigation system and you want to be able to configure the run time of each zone from your sitemap, this DP would apply.

Advantages and Disadvantages


  • Allows for the creation of very generic Rules
  • Allows for the addition and subtraction of Items that get processed by a given Rule using Group membership


  • Generates a proliferation of Items
  • Constrains Item naming schemes

Related Design Patterns

Design Pattern How It’s Used
[Rules DSL] Get item from string name! Source for the Item Registry Example
Design Pattern: Unbound Item (aka Virtual Item) Most associated Items are Unbound Items
Design Pattern: Working with Groups in Rules More examples of findFirst and forEach and other Group manipulations
Design Patterns: Generic Is Alive The Group findFirst example is a specific implementation of this DP
Design Pattern: Motion Sensor Timer The anti-flapping timer in the Group findFirst example is a specific implementation of this DP
Design Pattern: Human Readable Names in Messages Used in the Group findFirst example to transform the Item name to a more human readable name for logs and alerts.
Design Pattern: Separation of Behaviors Alerting implementation in Group findFirst and the setting of vIsCloudy in the Item Registry example
Design Pattern: Time Of Day Used to calculate the time of day in the Item Registry example
Design Pattern: Using Item Metadata as an Alternative to Several DPs Alternative approach using metadata.

Edit: added JSR223 Python examples, minor grammar updates, created a picture.

Design Pattern: Working with Groups in Rules
Design Pattern: Using Item Metadata as an Alternative to Several DPs
Design Pattern: Human Readable Names in Messages
Whole house lighting
How can I build a dynamic Item name for itemValue?
OpenHAB 2.0 Rules: Create list of HSBTypes
[SOLVED] 2 items in one line
Issue with working with Groups of Groups in Rules: Item not found when nesting groups
OH 3 Tips and Tricks
Reusable Functions: A simple lambda example with copious notes
Handling with Groups
Exec binding: Controlling two bluetooth Eqiva EQ3 radiator valves simultaneously
How to cancel/reset a createTimer in a generic rule
[Solved] JSON array? loop it in rule; rapid fire rule: concurrency?
Issues with implicit variables newState and triggeringItem in 3.0.0
Issues with implicit variables newState and triggeringItem in 3.0.0
[Solved] OH2: simplifying my rules...?
Is it possible to access the thing and location for an item in a rule?
Members of group item not recognized
[SOLVED] OH2: I am sure there is a better way to write this rule?
Global variables in OpenHAB and non-repetitive rules
Problem with manipulating a string for a rule
.members.filter DSL rule help request
Tutorial: How I make timers that show remaining execution time
Fetch another Item state, derived from a similar triggeringItem name
Weekly rotation through items in Sitemap
Optimization of rule
Design Pattern - Timer Management
CalDav usage and manually set events
Rollershutter up --> lights off
Patterns proposal: item states delay management and items dependencies (cascading)
Monitoring a lot of status informations
Hue bulbs and groups
Hue bulbs and groups
Help me optimize a simple rule (using items as variables, sending commands from an array)
Motion Sensor Advice
Converting group items into non-group items
How to set the light state back after blinking
How to store and use custom metadata for Items?
How to make a method / procedure from a rule
Heating and window contact - a (simple?) rule [SOLVED]
Struggles with stability
Item name as variable?
Is there a way to create "Animations" for devices?
[SOLVED] Rule retriggering Item state
Setup for home heating demand
Timer do not cancel properly
Smart Virtual Thermostat (beta version)
[solved] Item cast problem? Use string item to trigger Rollershutter item
How to implement 2-step motion sensor controlled lighting
Possibility to eliminate simple rules?
[SOLVED] Items definition in scripted automation
[SOLVED] Rule 'Extractor Fan sensor changed': null - another case
State of the item with combined name
[SOLVED] In sitemaps, how to create a view having item name and item value
Strange if/null behaviour
Help needed with groups
Magical light scenes
DP - Heating Boilerplate
[SOLVED] Put rule code into method and call it in different rules
Same rule for different rooms
Replacing a sensor, what is best practice
Sending command to indirectly referred item possible?
Make use of Group based rules
Reuseable sitemap configuration
Yet another Heating Setup
Area Triggers --> Area Actions
Combine different rules and triggers into 1 rule
Finding an Item's Thing and locate a different Item in Rules
Device Offline Rule
Howto: Generate similar item/sitemap configurations using templates
Create reusable 'function' for roller-shutters
Journey to JSR223 Python 2 of 9
Tags vs. Metadata
(SOLVED) Uninitialized var
Group based power consumption
Rollershutter Group positioning
Item Naming
Item Naming
Automation #4: Smart Radiator (Generic Rule)
My Garadget + MQTT + OpenHAB Setup
[SOLVED] How do I debounce or antiflap notifications for many devices in a group?
triggeringItem problem (concurrency?)
Grafana Image Charts
How to calculate the time of a specific azimuth
Qubino Smart Meters (ZMNHXD, ZMNHTD) endless problems
Design Pattern: State Machine Driven Groups
Doing it smarter - help with rule code rollershutter automation
[SOLVED] Making this rule generic
Thermostat rules with manual override timeout
Help with forEach command to grab one item and loop to another Item
Sending multiple pictures with Telegram Action
KNX - Different DPT for rollershutter position set and read GA
Magical light scenes
No Global Variables? Time to use Node-Red
Arguments to createTimer
Create multiple Rules with similar configuration for different items
Grouping items in rules - customer data structures like pojo
[SOLVED] Dummy item pointing to real item?
Concat Strings and create itemName out of it
Help with rule to find out item state
Magical light scenes
[SOLVED] Using groups to shorten code
[SOLVED] Reversing a Switch
[SOLVED] Reversing a Switch
Heating weekly schedule - more efficient way to implement this
Naming convention
Design Pattern: DRY, How Not to Repeat Yourself in Rules DSL
[SOLVED] Group Based Timestamps and durations with triggeringItem and Member of
Design Pattern: Gate Keeper
How can I access generic items in a function?
On/off switch with 2 buttons
Design Pattern: Encoding and Accessing Values in Rules
Best way to create outbound mqtt message with json content?
Design Pattern: Working with Groups in Rules
Class simulation in rules
[SOLVED] Rollershutter group & rule
[SOLVED] Lambda calling other lambda? (JSR223/JYTHON)
Generic Presence Detection
Creating "mode switches" for seasonal activities
Finally not called -> deadlock
Roadmap to Happiness - What is missing in the core framework
Roadmap to Happiness - What is missing in the core framework
Hot Tank monitoring, with energy calculations
Getting rule name in rule
Basics of a Lambda expression
[SOLVED] Can't set NEST structure away in OH2.3
[SOLVED] Groupping items properly
Arrays as Global Variables
Displaying time since last update of an item
Using part of an item's name to reference another item in a rule
Combine number and rollershutter items into a single item for use with rule?
Lambda functions fail (not thread safe?)
Convert String to Item
Rule which iterates through group(s) of similar items
Rule which iterates through group(s) of similar items
Rule which iterates through group(s) of similar items
[Loxone Binding] Update of Openhab item, does not update Loxone item, Why?
Timers in functions not possible?
Groups ... if state in group1 execute action in group2
How does OH know the state of a switch?
Design Pattern : Expire Binding based Countdown timer
Http1 binding using a base url
Setting up a cron based sprinkler
Creating rules: how to make them general ? Shoudl I use proxy-items, or variables?
Rule to calculate pump filtration time depending on temperature
Help shortening one of my persistence rules
Way to save system settings
Help with RPI Openhab full hang. (WAS Rule for longest ever uptime?)
Combining similar rules
Automatic lighting control without timers
Xtend Scripts vs JSR223?
Xtend Scripts vs JSR223?
Determine Object type in a Group (Dimmer or Switch)
Please test the new Expire Binding
[SOLVED] Question on the sendCommand()
Advice - New home, fresh install via Docker, fresh start
Item values and logical ranges at updates
[SOLVED] Openhab 2 Contact Sensor Time Rule
[SOLVED] Help me condense this rule
How I have automated my lights
Rules triggering at Group-Change of an Item
Feedback from a new openHAB(ian) user
Rules stop executing after a while
Issue with final parameters when using lambda to create "function"
What are your top 3 automations
Rule for Rollershutters - with proxy items and reed-contacts
Inactivity of items // no updates // automatically tracking
Lambda in rule fails with 'Error during the execution of rule '{RuleName}': null'
[SOLVED] Close Rollershutters when sun shines using groups
Generic rule to display last sensor update time
Lambda Procedure error: java.lang.NullPointerException
A More Clever System for "Locking" Lights?
Turning off devices based on Presence
How to make rules easy?
Help with better HVAC rule design
3 different methods to use scenes with Google Home & openHAB
Consolidation help
Cancel timer doesn`t work
Use an string item state as a name and state for another item
Suggestion for controlling my underfloor heating, Roth Touchline
Use variable for item
Cron Heating Rules
Alarm Notification "only" for the First OFF
Roku Support
Intergas Incomfort Lan2RF rules
Open the closed Rollershutter after Window opens
Rule optimization: Window OPEN reminder
Monoprice 6-zone Audio amp items, sitemap & rules
Simplifying Rule
Squeezebox Player WIP (Help Appreciated)
Array of type switch
Light switches
Make my rule files shorter --> working with groups
Iterating over a group, want to check an alternate item, sometimes
How to have a history of commands in Basic UI
Setting light themes in rules
Taking A Rule To The Next Level
How to store additional info in ITEM?
Type Conversions
Openhab.log goes crazy
Create a custom Item
Looking for optimal datastructure
JSR223 Jython Openhab Imports Erroring?
Match name of similiar items in two different groups
MQTT device initial state checking
YAGSA - Yet Another Group Structure Approach
Wildcard for Group.members.filter - Use String Patterns and ConfigItems
"First Time" Rule
Fire Rule when groupmember receives command
Best practice for .rules
[SOLVED] Persistence query in habpanel
How to create a complex light status check rule?
Basic switch questions, clarification about returned value
Design Pattern: Manual Trigger Detection
Notifications in group design pattern
How to model location of an item?
Item or Variable Name with Variable as part of the name
Design Pattern: Cascading Timers
Time and temperature spreadsheet schedule
No working trigger functions openHAB 2.1
Rule not working anymore
Switch with 2 SendCommand actions
[SOLVED] Style Guide for openHAB?
MQTT long press design pattern (dimmer)
Alert when item in a group has not been updated for x hours?
How to check if an item exists?
Identify itemname based on part of a name
Unable to get DateTime to work
Make rule with 196 if-statements more generic
Help Needed: sendCommand not working with arrayList of String
Construct Other Item Names from another
Current and Correct Documentation of Syntax for Things and Items Needed
An Approach for Better Light Timers
Automation/Orchestration Design Patterns
Individual Alarm clock for each Day of week, with adjustable duration
Arrays in Openhab 1.8.3
Set last tripped/alarm date, or last action date to an item - best practice?
Set last tripped/alarm date, or last action date to an item - best practice?
Return to earlier state
Send Modbus Data to File
Delay on item action exec binding
Add a property to a item or type
reelyActive Smart Spaces Revisited
[Deprecated] Design Pattern: Debounce
Creating Capabilities with Rule Templates: Time of Day
Enable/Disable Alexa Voice Control
Translate from javascript to rules DSL
Lambda randomly fails with 'Script execution of rule with UID ... failed: null in
Implementing a Script/Rule
Shutter control: "DOWN_BUT_TILT"
InfluxDB as an alternative to mytimer.reschedule?
Use String variable in if statement to change Item state
Generic rule for update HVAC
Concatonating strings and variables in a rule
Member of using Prefix and replace
OH3 - How to get the type of an item
Groups within Rules
Range in case of Roller position
String type - cut a part of

Nice! I did not know that’s possible… or at least I didn’t know how. That would totally simplify one workaround I have in my setup. Thanks

I wasn’t aware of this item.lastUpdate, is that an OH2 only thing?

It’s been around as long as I’ve been using OH so at least since 1.6. It’s one of the standard persistence methods on all Items along with maxSince, minSince, previousState, etc. See the “Persistence Extensions in Scripts and Rules” section of the [Persistence wiki page](Persistence Extensions in Scripts and Rules).

If you don’t have persistence setup for the Item it returns null though.

Great - learning every day! Thanks @rlkoshak. Does it work with any persistence, i.e. MapDB, or just historical based stuff like InfluxDB?

It works for any queryable persistence so MapDB yes as well as InfuxDB et al but not with write only persistence services like MQTT or my.openhab.

I primarily use lastUpdate with MapDB myself.

Great stuff - thanks again, and thanks for your very useful tutorials on rule design patterns. I am sure many are gaining a lot of value from these.

1 Like

Thank you for your very helpful post about associated items!
I am now trying to create a generic rule that works for any room in the house but I have one problem:
I want to use a Timer in that rule. You showed how to get an Item by name that had been defined in an Items file before (ie DateTime). But I cannot define a Timer in an Items file nor is it possible to add a Timer to a group programmatically (?).
I do not want to use a single hard coded timer variable that is shared between all rooms because it might be the case that multiple timers are active at the same time.
Any suggestions how I could solve this?

That is correct.

Create a hashMap of Timers using the Item name as the key.

import java.util.Map

val Map<String, Timer> timers = newHashMap

rule "Rule that creates some Timers"
    Item MyGroup received update // or whatever
    val i = blah blah blah // what ever you do to get the Item

    val Timer t = timers.get(
    if(t == null) {
        timers.put(, createTimer(now.plusMinutes(1), [|
            // do timer stuff
            timers.put(, null)
    else {
1 Like

I’m currently struggling with a rule. The rule needs to be triggered by any item in a specific group. So the trigger is similar as in your example.

However, in your example, the rule processes all items in that group. I would like my rule only to do stuff with the single item that was triggered.

So how do I identify the item (in group gDoors in your example) that triggered the rule?


I may have found a solution, not sure if it is “best practice”, but initial tests seem to point out it is reliable.

    Item gRaamcontact received update
	val LastWindowContact = gRaamcontact.members.sortBy[lastUpdate].last
	logInfo("Window","Last contact =" + LastWindowContact)

I’m not sure if it is always reliable when 2 contacts are changed at the same time…


It’s worth looking further in the Tutorials & Example forum section

Depending on the speed of your persistence you man need to add a sleep before the sortby.

It isn’t a best practice so much as the only way to do it in this case. The alternative is one rule per switch which each call a lambda.

Thank you both.

I thought I had read all the rule-tutorials by now, I guess I missed that one.

@rlkoshak: So far I didn’t had any persistence-issues. I guess my mapDB is fast enough. I just added a small sleep just to be sure (100ms). I didn’t want to make it too big, because then the chance of two contacts being changed at the same time will increase.

I’ve been working through a few of the Design Pattern articles - they’re helping me get a better understanding of Openhab2. Thank-you for taking the time to write them up.

I have been trying to add the LastUpdate feature to my setup, using this as a guide. I think I’m nearly there, but I get an error at the assocDT.postUpdate(new DateTimeType(door.lastUpdate)) stage;
This gives the output (full detail further below)

2017-07-02 18:40:24.019 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'A Door's State Changed': Could not invoke constructor: org.eclipse.smarthome.core.library.types.DateTimeType.DateTimeType(java.lang.String)

The code I am using is very similar to the original post in this article (and the associated one on persistence), with a few log lines for debugging and a temporary workaround for Groups following a recent OH update: Groups seem to be broken

2017-07-02 18:32:47.040 [INFO ] [rthome.model.script.associated items] - Door state change rule started
2017-07-02 18:32:47.190 [INFO ] [rthome.model.script.associated items] - dtStr = testDoor_LastUpdate
2017-07-02 18:32:47.215 [INFO ] [rthome.model.script.associated items] - assocDT = testDoor_LastUpdate (Type=DateTimeItem, State=NULL, Label=test Door Last Update, Category=clock, Groups=[gDoorsLastUpdate])
2017-07-02 18:32:47.279 [INFO ] [rthome.model.script.associated items] - door.lastUpdate = 2017-07-02T18:32:46.000+01:00
2017-07-02 18:32:47.330 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'A Door's State Changed': Could not invoke constructor: org.eclipse.smarthome.core.library.types.DateTimeType.DateTimeType(java.lang.String)

Key snippets below - wondering if anyone can suggest how to fix?;

From .rules


val logName = "associated items"

rule "A Door's State Changed"
    Item gDoors received update // NOTE: the rule will trigger multiple times per event
        logInfo(logName, "Door state change rule started")
        gDoors.members.forEach[door |
        // Get the associated DateTime Item
        val dtStr = + "_LastUpdate"
        val assocDT = gDoorsLastUpdate.members.filter[dt| == dtStr].head as DateTimeItem
        logInfo(logName, "dtStr = " + dtStr)
        logInfo(logName, "assocDT = " + assocDT )
        logInfo(logName, "door.lastUpdate = "+ door.lastUpdate)

        // Update assocDT with the door's lastUpdate
        assocDT.postUpdate(new DateTimeType(door.lastUpdate))


from .items


Group:Contact  gDoors                           // temporary workaround following recent OH update:
Group gDoorsLastUpdate

Contact  testDoor               "test Door"                             <frontdoor>     (gDoors,GarageDoorGroup,gHistory,gNewDoorGroup)   {mqtt="<[mysensorsMQTT:mysensors/in/100/2/1/0/16:state:MAP(]"}
DateTime testDoor_LastUpdate    "test Door Last Update [%1$tm/%1$td %1tH:%1tM]" <clock> (gDoorsLastUpdate)

This gives the output:

2017-07-02 18:40:23.807 [INFO ] [rthome.model.script.associated items] - Door state change rule started
2017-07-02 18:40:23.952 [INFO ] [rthome.model.script.associated items] - dtStr = testDoor_LastUpdate
2017-07-02 18:40:23.970 [INFO ] [rthome.model.script.associated items] - assocDT = testDoor_LastUpdate (Type=DateTimeItem, State=NULL, Label=test Door Last Update, Category=clock, Groups=[gDoorsLastUpdate])
2017-07-02 18:40:23.996 [INFO ] [rthome.model.script.associated items] - door.lastUpdate = 2017-07-02T18:40:23.000+01:00
2017-07-02 18:40:24.019 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'A Door's State Changed': Could not invoke constructor: org.eclipse.smarthome.core.library.types.DateTimeType.DateTimeType(java.lang.String)

To be honest I understand enough of the syntax for the assocDT.postUpdate to work out what’s causing the error.

Any ideas on how to fix, or dig deeper into the logs?



lastUpdate returns a Joda DateTime object. You can not update a DateTimeItem with a Joda DateTime object. You either need to create a new DateTimeType using the last update.millis or you can try using door.lastUpdate.toString in your call to postUpdate. I think the default toString is the right format for OH to parse it into a DateTimeType.

1 Like

Thanks the .toString seems to be working

Recently I wondered what more goodies OH had in store to surprise me with.
I think you just showed one. This is totally useful and will apply on many things other than just door updates. Thanks

Great article.

Could you advise how your design proposal could work if I need to use pairs of objects. Like temperature measuring (room_temp) and target temperature (room_target_temp)? if I have 8 rooms, I would like to have a rule iterating every room and comparing corresponding room temperature with target temperature.


Just like you describe. Just make sure you can name the Items in such a way that you can easily reconstruct the name of the associated Items using the name of the Item you are iterating over.

So, if you have a Bedroom_Temp name the associated Item Bedroom_Temp_Target and your loop would look something like:

Rooms.members.forEach[room |
    val target = TargetTemps.members.findFirst[room | == + "_Target"]

Thank you, Rich, makes perfect sense! as I understand in two loops the name variable “room” should not duplicate, so updated:

Rooms.members.forEach[room |
val target = TargetTemps.members.findFirst[r | == + “_Target”]