Could not invoke method: BusEvent.postUpdate(java.lang.String,java.lang.String) on instance: null

Hoping for some help on what seems like a really trivial problem, which I just can’t work out: I’ve adapted some of the rules originally by @Dries for integration of a home alarm system with OpenHab, adding a history field to record the last update time for each of the alarm contacts. However I keep running into a

An error occurred during the script execution: Could not invoke method:org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(java.lang.String,java.lang.String) on instance: null

error in the logs. The rule is so simple I can’t work out where it’s going wrong:

rule "Alarm interface via UDP"
when   
    Item  Tex_UDP_Receive received update
then
   var in_UDPmsg = Tex_UDP_Receive.state.toString.trim
 
    if(in_UDPmsg.startsWith("\"Z0")) {
        var itemName = "Tex_Zone_"+in_UDPmsg.substring(1, 5)
	var historyName = "Tex_History_"+in_UDPmsg.substring(1, 5)
	logInfo("Texecom", historyName)
        postUpdate(itemName, in_UDPmsg.substring(5))
	postUpdate(historyName, new DateTimeType())	
    }

The first postUpdate (which simply posts a “1” or “0” works, but the second generates the error. The logInfo command shows that historyName is correct (for example Tex_History_Z001) and I have a DateTime item defined as follows:

DateTime Tex_History_Z001			"Time Zone 1 status changed [%1$td.%1$tm.%1$tY %1$tT]"   <time> (gAlarmhistory)

for each zone

The really weird thing is that if I replace the “historyName” variable with (for instance) Tex_History_Z001 it works!

Hoping for some pointers in the right direction - this is proving harder than I expected to debug!

When using postUpdate in that way, it requires two strings.
new DateTimeType()
unsuprisingly gives you a DateTimeType object, not a string.
Maybe
new DateTimeType().toString
although I really don’t know if that gives a format that a DateTime Item can digest.
This might help -

I’m certain that will work. In this case he’s not really using Joda’s DateTime at all, it’s all DateTimeType. But in either case, DateTime or DateTimeType’s toString returns an ISO 8601 formatted date time so it should work.

Thanks - changing the Tex_History_Zxxx items to strings, and then using now.toString in place of the DateTimeType works.

I’m curious though - why does postUpdate need two strings (rather than one string and one DateTime) variable?

Why on earth do that?

Because it is a simple little thing, and not the preferred way to update Items.

Because the postUpdate Action does not and cannot know what type of Item the first argument is. Therefore it only accepts an Object that makes sense for all Item types, and that’s a String. Therefore, if you can use the Item method you should use the Item method because in that case it knows what type the Item is and can handle appropriate types for that Item. But if you have to use the Action, you have to pass in two Strings.

But that doesn’t mean the Item can’t still be a DateTime Item. The String passed to the postUpdate Action will eventually be parsed by the Item it’s sent to so as long as the String is parsable by the Item you will be fine.

Thanks both for your quick responses & great explanations. Both approaches work. I hadn’t realised I could pass a date string to a DateTime Item. Really appreciate the help. I hope at some stage to stop feeling like a newbie, and know enough to start helping others