"Energy rule" ended with error when using deltaSince

* Platform information:
  * Hardware: Raspberry Pi 4 Model B Rev 1.1, 4 GB RAM, 256 GB SSD
  * OS: Linux/4.19.118-v7l+ (arm)
  * Java Runtime Environment: Azul Systems 11.0.8 (Zulu11.41+75-CA)
  * openHAB version: 3.0.0 Build #1958

Hi all,
i have a Homematic HM-ES-PMSw1-Pl Wireless Switch Actuator 1-channel with power metering and i want to collect the power usage per day, month and year based on a “energy rule” in which i use the deltaSince extension of the rrd4j persistence service.

Unfortunately the rule ends with an error “Could not invoke method … on instance null”.

log snippet

Summary

This text will be hidden

2020-10-07 12:14:55.399 [INFO ] [enhab.core.model.script.HM_FSD.rules] - HM_FSD ENERGY_COUNTER Änderung - Anfang
2020-10-07 12:14:55.411 [INFO ] [enhab.core.model.script.HM_FSD.rules] - HM_FSD ENERGY_COUNTER Änderung - Funk_Steckdose_EnergyCounter: 48785.40 Wh
2020-10-07 12:14:55.416 [INFO ] [enhab.core.model.script.HM_FSD.rules] - HM_FSD ENERGY_COUNTER Änderung - strom_verbrauch_tag: 0
2020-10-07 12:14:55.419 [INFO ] [enhab.core.model.script.HM_FSD.rules] - HM_FSD ENERGY_COUNTER Änderung - tagesanfang: 2020-10-07T00:00
2020-10-07 12:14:55.422 [ERROR] [internal.handler.ScriptActionHandler] - Script execution failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.persistence.extensions.PersistenceExtensions.deltaSince(org.openhab.core.items.Item,java.time.ZonedDateTime) on instance: null in HM_FSD

HM_FSD.rules

//**************************************************************************
// HM_FSD ENERGY_COUNTER Änderung
//**************************************************************************
//

rule "HM_FSD ENERGY_COUNTER Änderung"
	when
		Item Funk_Steckdose_EnergyCounter changed
	then
		val 	String 		rulename = "HM_FSD ENERGY_COUNTER Änderung - "

		var	LocalDateTime	tagesanfang
		var 	Number	strom_verbrauch_tag


		logInfo (FILENAME, rulename + "Anfang")

		strom_verbrauch_tag = 0
		tagesanfang = LocalDate.now().atStartOfDay()

		logInfo (FILENAME, rulename + "Funk_Steckdose_EnergyCounter: " + Funk_Steckdose_EnergyCounter.state)
		logInfo (FILENAME, rulename + "strom_verbrauch_tag: " + strom_verbrauch_tag)
		logInfo (FILENAME, rulename + "tagesanfang: " + tagesanfang)

		nFSD1_Strom_Verbrauch_Tag.postUpdate (Funk_Steckdose_EnergyCounter.deltaSince(tagesanfang) )

		logInfo (FILENAME, rulename + "nFSD1_Strom_Verbrauch_Tag: " + nFSD1_Strom_Verbrauch_Tag.state)

		logInfo (FILENAME, rulename + "Ende")
end

OH3 Settings > Items panel

Funk_Steckdose_EnergyCounter is of type Number:Energy

The rrd4j persistence service of the item Funk_Steckdose_EnergyCounter also have values for the required time frame from start of day.

I dont understand the error “Could not invoke method … on instance null” because the variable tagesanfang is not null and the item Funk_Steckdose_EnergyCounter is also not null.

Any help is appriciated.

1 Like

Have you any reason to think that deltaSince uses rrd4j? (That’s a hint about default persistence)

Add a line to fetch and display historicState to see if that works.

I’ve not seen people use LocalDateTime in connection with persistence before, I wonder if that is a suitable datetime type.

Have you any reason to think that deltaSince uses rrd4j? (That’s a hint about default persistence)

rrd4j is the default persistence service but for sure i insert the string “rrd4j” into the command.

I’ve not seen people use LocalDateTime in connection with persistence before, I wonder if that is a suitable datetime type.

The usage of now.withTimeAtStartOfDay() instead of LocalDate.now().atStartOfDay() results in an error message

2020-10-07 15:01:47.475 [ERROR] [internal.handler.ScriptActionHandler] - Script execution failed: 'withTimeAtStartOfDay' is not a member of 'java.time.ZonedDateTime'; line 37, column 17, length 26 in HM_FSD

so i decided to use LocalDate.now().atStartOfDay() .

Add a line to fetch and display historicState to see if that works.

To fetch a value with historicState also results in the error message

2020-10-07 15:21:37.997 [INFO ] [enhab.core.model.script.HM_FSD.rules] - HM_FSD ENERGY_COUNTER Änderung - Anfang
2020-10-07 15:21:38.011 [ERROR] [internal.handler.ScriptActionHandler] - Script execution failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.persistence.extensions.PersistenceExtensions.historicState(org.openhab.core.items.Item,java.time.ZonedDateTime,java.lang.String) on instance: null in HM_FSD

HM_FSD.rules

/**************************************************************************
// HM_FSD ENERGY_COUNTER Änderung
//**************************************************************************
//

rule "HM_FSD ENERGY_COUNTER Änderung"
	when
		Item Funk_Steckdose_EnergyCounter changed
	then
		val 	String 		rulename = "HM_FSD ENERGY_COUNTER Änderung - "

		var	LocalDateTime	tagesanfang
		var 	Number		strom_verbrauch_tag


		logInfo (FILENAME, rulename + "Anfang")

		nFSD1_Strom_Verbrauch_Tag.postUpdate (Funk_Steckdose_EnergyCounter.historicState(LocalDate.now().minusDays(1), "rrd4j") )


		strom_verbrauch_tag = 0
		tagesanfang = LocalDate.now().atStartOfDay()

		logInfo (FILENAME, rulename + "Funk_Steckdose_EnergyCounter: " + Funk_Steckdose_EnergyCounter.state)
		logInfo (FILENAME, rulename + "strom_verbrauch_tag: " + strom_verbrauch_tag)
		logInfo (FILENAME, rulename + "tagesanfang: " + tagesanfang)

		nFSD1_Strom_Verbrauch_Tag.postUpdate (Funk_Steckdose_EnergyCounter.deltaSince(tagesanfang, "rrd4j") )

		logInfo (FILENAME, rulename + "nFSD1_Strom_Verbrauch_Tag: " + nFSD1_Strom_Verbrauch_Tag.state)

		logInfo (FILENAME, rulename + "Ende")
end

Thank you very much for your support.

Maybe your problem with that is about java 11

Joda-Time was removed in OH3. Use ZonedDateTime instead. This is one of the more impacting breaking changes that will affect people still trying to use legacy rules DSL. With scripted automation, you can just add it back in.

Joda-Time was removed in OH3. Use ZonedDateTime instead. This is one of the more impacting breaking changes that will affect people still trying to use legacy rules DSL. With scripted automation, you can just add it back in.

This is the solution. After using ZonedDateTime the extension deltaSince of the persistence service rrd4j delivered a value and the rule ended without an error.

log file snippet

2020-10-08 10:55:47.461 [INFO ] [enhab.core.model.script.HM_FSD.rules] - HM_FSD ENERGY_COUNTER Änderung - Anfang
2020-10-08 10:55:47.465 [INFO ] [enhab.core.model.script.HM_FSD.rules] - HM_FSD ENERGY_COUNTER Änderung - Funk_Steckdose_EnergyCounter: 50373.90 Wh
2020-10-08 10:55:47.469 [INFO ] [enhab.core.model.script.HM_FSD.rules] - HM_FSD ENERGY_COUNTER Änderung - start_of_day: 2020-10-08T00:00+02:00[Europe/Berlin]
2020-10-08 10:55:47.475 [INFO ] [enhab.core.model.script.HM_FSD.rules] - HM_FSD ENERGY_COUNTER Änderung - nFSD1_Strom_Verbrauch_Tag: 642.199999999996
2020-10-08 10:55:47.478 [INFO ] [enhab.core.model.script.HM_FSD.rules] - HM_FSD ENERGY_COUNTER Änderung - Ende

HM_FSD.rules snippet

//**************************************************************************
// HM_FSD ENERGY_COUNTER Änderung
//**************************************************************************
//

rule "HM_FSD ENERGY_COUNTER Änderung"
	when
		Item Funk_Steckdose_EnergyCounter changed
	then
		val 	String 		rulename = "HM_FSD ENERGY_COUNTER Änderung - "

		val  ZonedDateTime 	zdt = ZonedDateTime.now()
		var  ZonedDateTime 	start_of_day = zdt.toLocalDate().atStartOfDay(zdt.getZone())


		logInfo (FILENAME, rulename + "Anfang")

		logInfo (FILENAME, rulename + "Funk_Steckdose_EnergyCounter: " + Funk_Steckdose_EnergyCounter.state)
		logInfo (FILENAME, rulename + "start_of_day: " + start_of_day)

		nFSD1_Strom_Verbrauch_Tag.postUpdate (Funk_Steckdose_EnergyCounter.deltaSince(start_of_day, "rrd4j") )

		logInfo (FILENAME, rulename + "nFSD1_Strom_Verbrauch_Tag: " + nFSD1_Strom_Verbrauch_Tag.state)

		logInfo (FILENAME, rulename + "Ende")
end

Many thanks for your support.

Hello.
This helps me. thank you.
But is there a method to get Start of Month?

monatsanfang = zdt.toLocalDate().atStartOfMonth(zdt.getZone())

Thx in Advance.

i found it:

start_of_month = start_of_day.minusDays(now.getDayOfMonth()-1) gets the 01.01.2022 today.