Old APC-UPS Script not working after OH3 Update - DateTime?

hi,
I just upgraded from OH2 to OH3 and got almost everything up and running again. Only an old copy-pasted rule for parsing the output of “apcaccess -u” to give me information about my APC USV is not working anymore.

Logfile says:

2021-05-17 09:30:44.263 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'apc-1' failed: An error occurred during the script execution: Could not invoke constructor: org.openhab.core.library.types.DateTimeType.DateTimeType(java.time.ZonedDateTime) in apc

Already back in OH2 I got the “DateTime is deprecated” warning, but it was still working.
Here ist the rule:

import java.io.BufferedReader
import java.io.StringReader
import java.util.Calendar
import java.text.SimpleDateFormat
import java.text.ParseException

rule "APC: Parse raw output from command-line tool"
when
	Item APC_Raw changed
then
	var output = APC_Raw.state.toString
	var String line
	var String[] buffer
	var String value
	var bufReader = new BufferedReader(new StringReader(output))
	var Calendar cal = Calendar.getInstance()
	var formatStrings = newArrayList('yyyy-MM-dd HH:mm:ss Z', 'yyyy-MM-dd')
		
	while((line = bufReader.readLine()) !== null) {
		buffer = line.split(':', 2)
		for (var i = 0; i < 2; i++) {
			buffer.set(i, buffer.get(i).trim())
    	}
    	
    	// check if there’s an item for this key (e.g. APC_STARTTIME)
    	var item = gAPC.members.findFirst[name.equals("APC_" + buffer.get(0))]

		if (item !== null) {
			value = buffer.get(1)
			
			// DateTime item: try to parse date
			if (item.type == 'DateTime') {
				var succeeded = false
				for (String formatString : formatStrings) {
					try {
						if (!succeeded) { // no `break` statement in openHAB :(
							cal.setTime(new SimpleDateFormat(formatString).parse(value))
							item.postUpdate(new DateTimeType(cal).toString)
							succeeded = true
						}
					} catch (ParseException e) { }
				}
			// String or Number: just update the value
			} else {
				item.postUpdate(value)	
			}
		}
	}

    // Calculate Watts
    APC_LOAD.postUpdate(APC_LOADPCT.state as Number * 5)
	
end

How can I adapt this rule to work again?

Thanks in advance!

Hi!
I have the same problem as you. Have you found a solution?

Thanks
Tom

Not really, just a workaround. DateTimeObjects do not work, but wattage etc. do.
I just commented out

item.postUpdate(new DateTimeType(cal))

to not run into that problem.

I’d suggest checking if your APC UPS can be used with Network UPS Tools.

Thanks Russ!
With OH2 it was working. It reads the UPS, all in one string, but I have some problems in reading the single values in the string. It gaves me an error with calendar.

Managed to correct the “Battery Date” but not the “Start Time”.
Here is the code, if it helps you

import java.io.BufferedReader
import java.io.StringReader
import java.util.Calendar
import java.text.SimpleDateFormat


rule "APC: Parse raw output from command-line tool"
when
	Item APC_Raw changed
then
	//var output = APC_Raw.state.toString
	var output = APC_Raw.state.toString.replace('~','\n')
	var String line
	var String[] buffer
	var String value
	var bufReader = new BufferedReader(new StringReader(output))
	var Calendar cal = Calendar.getInstance()
	//var formatStrings = newArrayList('yyyy-MM-dd HH:mm:ss Z', 'yyyy-MM-dd')
	var formatStrings = newArrayList('yyyy-MM-dd')
	
	while((line = bufReader.readLine()) !== null) {
		buffer = line.split(':', 2)
		for (var i = 0; i < 2; i++) {
			buffer.set(i, buffer.get(i).trim())
    	}
    	
    	// check if there’s an item for this key (e.g. APC_STARTTIME)
    	var item = gAPC.members.findFirst[name.equals("APC_" + buffer.get(0))]

		if (item !== null) {
			value = buffer.get(1)
			
			// DateTime item: try to parse date
			if (item.type == 'DateTime') {
				var succeeded = false
				for (String formatString : formatStrings) {
					try {
						if (!succeeded) { // no `break` statement in openHAB :(
							cal.setTime(new SimpleDateFormat(formatString).parse(value))
							val MyEpochFromJavaTime_VariantA = cal.toInstant.toEpochMilli
							//item.postUpdate(new DateTimeType(cal).toString)
							item.postUpdate(MyEpochFromJavaTime_VariantA)

							succeeded = true
						}
					} catch (ParseException e) { }
				}
			// String or Number: just update the value
			} else {
				item.postUpdate(value)	
			}
		}
	}
	
end