Issues with Energy Rules since OH3.1.0M3 and M4

I have 3 rules running in order to calculate Gas, Energy Consumption and Energy Production. These rules worked fine in OH3 until 3.1.0M3 and M4. I had to downgrade back to M2 in order to get the rules working again. The Openhab log shows the following errors:

2021-05-05 16:10:00.130 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-05 16:10:00.185 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-05 16:10:00.186 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'PowerConsumptionPerDay-2' failed: The argument 'state' must not be null. in PowerConsumptionPerDay
2021-05-05 16:10:00.885 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-05 16:10:00.887 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Gasusage-2' failed: The argument 'state' must not be null. in Gasusage
2021-05-05 16:11:00.169 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-05 16:11:00.203 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-05 16:11:00.204 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'PowerConsumptionPerDay-2' failed: The argument 'state' must not be null. in PowerConsumptionPerDay
2021-05-05 16:11:00.884 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-05 16:11:00.886 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Gasusage-2' failed: The argument 'state' must not be null. in Gasusage
2021-05-05 16:12:00.126 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-05 16:12:00.161 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-05 16:12:00.162 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'PowerConsumptionPerDay-2' failed: The argument 'state' must not be null. in PowerConsumptionPerDay
2021-05-05 16:12:00.928 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-05 16:12:00.929 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Gasusage-2' failed: The argument 'state' must not be null. in Gasusage

The rules I use are:

Gasusage:

rule "Update Gas Usage"
when
    Time cron "0 0 0 * * ? *"
then
//	logInfo('logGas', 'Rule Update GasUsage executing. DSMRM3GasMeter_Delivery = {}', DSMRM3GasMeter_Delivery.state)
	DSMRM3GasMeter_Delivery.postUpdate(DSMRM3GasMeter_Delivery.state as Number)
	GasUsageYesterday.postUpdate(DSMRM3GasMeter_Delivery.deltaSince(now.minusDays(1)))
	GasUsageWeek.postUpdate(DSMRM3GasMeter_Delivery.deltaSince(now.minusWeeks(1)))
	GasUsageMonth.postUpdate(DSMRM3GasMeter_Delivery.deltaSince(now.minusMonths(1)))
	GasUsageYear.postUpdate(DSMRM3GasMeter_Delivery.deltaSince(now.minusYears(1)))
end

rule "Update Gas Usage Today"
when
//	Item DSMRM3GasMeter_Delivery changed
	Time cron "0 * * ? * *"
then
	DSMRM3GasMeter_Delivery.postUpdate(DSMRM3GasMeter_Delivery.state as Number)
	GasUsageToday.postUpdate( DSMRM3GasMeter_Delivery.deltaSince(now.with(LocalTime.of(0,0,0,0))))
	val EnergyContractDate = ZonedDateTime.parse("2020-08-15T00:00:00.000Z").withZoneSameInstant(ZoneId.systemDefault())
	GasUsageContract.postUpdate( DSMRM3GasMeter_Delivery.deltaSince(EnergyContractDate))
end

PowerProduction:

rule "PowerProduction per Day"
when
    Time cron "0 0 0 * * ? *"
then
	Total_Power_Production_Group.postUpdate(Total_Power_Production_Group.state as Number)
	PowerProductionYesterday.postUpdate(Total_Power_Production_Group.deltaSince(now.minusDays(1))*-1)
	PowerProductionWeek.postUpdate(Total_Power_Production_Group.deltaSince(now.minusWeeks(1))*-1)
	PowerProductionMonth.postUpdate(Total_Power_Production_Group.deltaSince(now.minusMonths(1))*-1)
	PowerProductionYear.postUpdate(Total_Power_Production_Group.deltaSince(now.minusYears(1))*-1)	    
end

rule "Update PowerProductionToday"
when
//		Item Energie_verbruik_levering changed
	Time cron "0 * * ? * *"
then
	Total_Power_Production_Group.postUpdate(Total_Power_Production_Group.state as Number)
	PowerProductionToday.postUpdate( Total_Power_Production_Group.deltaSince(now.with(LocalTime.of(0,0,0,0)) )*-1)
	val EnergyContractDate = ZonedDateTime.parse("2020-08-15T00:00:00.000Z").withZoneSameInstant(ZoneId.systemDefault())
	PowerProductionContract.postUpdate( Total_Power_Production_Group.deltaSince(EnergyContractDate)*-1)
end

And PowerConsumption:

rule "PowerConsumption per Day"
when
    Time cron "0 0 0 * * ? *"
then
	Total_Power_Delivery_Group.postUpdate(Total_Power_Delivery_Group.state as Number)
	PowerConsumtionYesterday.postUpdate(Total_Power_Delivery_Group.deltaSince(now.minusDays(1)))
	PowerConsumtionWeek.postUpdate(Total_Power_Delivery_Group.deltaSince(now.minusWeeks(1)))
	PowerConsumtionMonth.postUpdate(Total_Power_Delivery_Group.deltaSince(now.minusMonths(1)))
	PowerConsumtionYear.postUpdate(Total_Power_Delivery_Group.deltaSince(now.minusYears(1)))
end

rule "Update PowerUsageToday"
when
//	Item Energie_verbruik_levering changed
	Time cron "0 * * ? * *"
then
	Total_Power_Delivery_Group.postUpdate(Total_Power_Delivery_Group.state as Number)
	PowerConsumtionToday.postUpdate( Total_Power_Delivery_Group.deltaSince(now.with(LocalTime.of(0,0,0,0))))
	val EnergyContractDate = ZonedDateTime.parse("2020-08-15T00:00:00.000Z").withZoneSameInstant(ZoneId.systemDefault())
	PowerConsumtionContract.postUpdate( Total_Power_Delivery_Group.deltaSince(EnergyContractDate))
end

I can’t figure out what has changed in M3 and higher why a state should be null. As soon as I downgrade to 3.1.0M2 everything runs fine again.

It’s about persistence. Are these Items being successfully persisted? What type of Items are they?

Hi Rossko. Thanks for your answer.

Shall we look at Gas only for now? If we can fix this issue, I can fix them all.

DSMRM3GasMeter_Delivery → Number
GasUsageToday → Number
GasUsageContract → Number
GasUsageYesterday → Number
GasUsageWeek → Number
GasUsageMonth → Number
GasUsageYear → Number

So all items are number values. Not something like Number:Volume.

The numers should have been successfully persisted (I suppose) because if I downgrade, I get rid of all the errors and the numbers are more or less the same compared to the reports I get from my Energy supplier.

1 Like

This is some of the information in Influx:

1620223980274000000 DSMRM3GasMeter_Delivery 2534.951
1620224040274000000 DSMRM3GasMeter_Delivery 2534.951
1620224100274000000 DSMRM3GasMeter_Delivery 2534.951
1620224160273000000 DSMRM3GasMeter_Delivery 2534.951
1620224220274000000 DSMRM3GasMeter_Delivery 2534.951
1620224280274000000 DSMRM3GasMeter_Delivery 2534.951
1620224340275000000 DSMRM3GasMeter_Delivery 2534.951
1620224397422000000 DSMRM3GasMeter_Delivery 2534.952
1620224400273000000 DSMRM3GasMeter_Delivery 2534.952
1620224460272000000 DSMRM3GasMeter_Delivery 2534.952
1620224520272000000 DSMRM3GasMeter_Delivery 2534.952
1620224580272000000 DSMRM3GasMeter_Delivery 2534.952
1620224640272000000 DSMRM3GasMeter_Delivery 2534.952
1620224700272000000 DSMRM3GasMeter_Delivery 2534.952
1620224760273000000 DSMRM3GasMeter_Delivery 2534.952
1620224820273000000 DSMRM3GasMeter_Delivery 2534.952
1620224880272000000 DSMRM3GasMeter_Delivery 2534.952
1620224940273000000 DSMRM3GasMeter_Delivery 2534.952
1620225000272000000 DSMRM3GasMeter_Delivery 2534.952
1620225060272000000 DSMRM3GasMeter_Delivery 2534.952
1620225120272000000 DSMRM3GasMeter_Delivery 2534.952
1620225180272000000 DSMRM3GasMeter_Delivery 2534.952
1620225240272000000 DSMRM3GasMeter_Delivery 2534.952
1620225300272000000 DSMRM3GasMeter_Delivery 2534.952
1620225360272000000 DSMRM3GasMeter_Delivery 2534.952
1620225420272000000 DSMRM3GasMeter_Delivery 2534.952
1620225480274000000 DSMRM3GasMeter_Delivery 2534.952
1620225540272000000 DSMRM3GasMeter_Delivery 2534.952
1620225600273000000 DSMRM3GasMeter_Delivery 2534.952
1620225660273000000 DSMRM3GasMeter_Delivery 2534.952
1620225720273000000 DSMRM3GasMeter_Delivery 2534.952
1620225780272000000 DSMRM3GasMeter_Delivery 2534.952
1620225840275000000 DSMRM3GasMeter_Delivery 2534.952
1620225900273000000 DSMRM3GasMeter_Delivery 2534.952
1620225960273000000 DSMRM3GasMeter_Delivery 2534.952
1620226020272000000 DSMRM3GasMeter_Delivery 2534.952
1620226080272000000 DSMRM3GasMeter_Delivery 2534.952
1620226140272000000 DSMRM3GasMeter_Delivery 2534.952
1620226200272000000 DSMRM3GasMeter_Delivery 2534.952
1620226260272000000 DSMRM3GasMeter_Delivery 2534.952
1620226320273000000 DSMRM3GasMeter_Delivery 2534.952

1620220860435000000 GasUsageToday 3.146
1620220920416000000 GasUsageToday 3.146
1620220980431000000 GasUsageToday 3.146
1620221040432000000 GasUsageToday 3.146
1620221100412000000 GasUsageToday 3.146
1620221160439000000 GasUsageToday 3.146
1620221220437000000 GasUsageToday 3.146
1620221280434000000 GasUsageToday 3.146
1620221340410000000 GasUsageToday 3.146
1620221400421000000 GasUsageToday 3.146
1620221460411000000 GasUsageToday 3.146
1620221460570000000 GasUsageToday 3.147
1620221520405000000 GasUsageToday 3.147
1620221580423000000 GasUsageToday 3.147
1620221640418000000 GasUsageToday 3.147
1620221700415000000 GasUsageToday 3.147
1620221760406000000 GasUsageToday 3.147
1620221820417000000 GasUsageToday 3.147
1620221880411000000 GasUsageToday 3.147
1620221940419000000 GasUsageToday 3.147
1620222000406000000 GasUsageToday 3.147
1620222060424000000 GasUsageToday 3.147
1620222120421000000 GasUsageToday 3.147
1620222180424000000 GasUsageToday 3.147
1620222240420000000 GasUsageToday 3.147
1620222300410000000 GasUsageToday 3.147
1620222360425000000 GasUsageToday 3.147
1620222420417000000 GasUsageToday 3.147
1620222480408000000 GasUsageToday 3.147
1620222540417000000 GasUsageToday 3.147
1620222600419000000 GasUsageToday 3.147
1620222660420000000 GasUsageToday 3.147
1620222720411000000 GasUsageToday 3.147
1620222780414000000 GasUsageToday 3.147
1620222840423000000 GasUsageToday 3.147
1620222900406000000 GasUsageToday 3.147
1620222960415000000 GasUsageToday 3.147
1620223020415000000 GasUsageToday 3.147
1620223080420000000 GasUsageToday 3.147
1620223140405000000 GasUsageToday 3.147
1620223200442000000 GasUsageToday 3.147
1620223260405000000 GasUsageToday 3.147
1620223560315000000 GasUsageToday 3.147
1620223620294000000 GasUsageToday 3.147
1620223680291000000 GasUsageToday 3.147
1620223740286000000 GasUsageToday 3.147
1620223800286000000 GasUsageToday 3.147
1620223860290000000 GasUsageToday 3.147
1620223920287000000 GasUsageToday 3.147
1620223980288000000 GasUsageToday 3.147
1620224040287000000 GasUsageToday 3.147
1620224100287000000 GasUsageToday 3.147
1620224160286000000 GasUsageToday 3.147
1620224220290000000 GasUsageToday 3.147
1620224280289000000 GasUsageToday 3.147
1620224340290000000 GasUsageToday 3.147
1620224400281000000 GasUsageToday 3.147
1620224400907000000 GasUsageToday 3.148
1620224460279000000 GasUsageToday 3.148
1620224520279000000 GasUsageToday 3.148
1620224580280000000 GasUsageToday 3.148
1620224640280000000 GasUsageToday 3.148
1620224700283000000 GasUsageToday 3.148
1620224760289000000 GasUsageToday 3.148
1620224820284000000 GasUsageToday 3.148
1620224880279000000 GasUsageToday 3.148
1620224940283000000 GasUsageToday 3.148
1620225000280000000 GasUsageToday 3.148
1620225060280000000 GasUsageToday 3.148
1620225120279000000 GasUsageToday 3.148
1620225180280000000 GasUsageToday 3.148
1620225240279000000 GasUsageToday 3.148
1620225300279000000 GasUsageToday 3.148
1620225360282000000 GasUsageToday 3.148
1620225420282000000 GasUsageToday 3.148
1620225480289000000 GasUsageToday 3.148
1620225540280000000 GasUsageToday 3.148
1620225600289000000 GasUsageToday 3.148
1620225660280000000 GasUsageToday 3.148
1620225720306000000 GasUsageToday 3.148
1620225780280000000 GasUsageToday 3.148
1620225840294000000 GasUsageToday 3.148
1620225900280000000 GasUsageToday 3.148
1620225960279000000 GasUsageToday 3.148
1620226020279000000 GasUsageToday 3.148
1620226080280000000 GasUsageToday 3.148
1620226140283000000 GasUsageToday 3.148
1620226200279000000 GasUsageToday 3.148
1620226260279000000 GasUsageToday 3.148
1620226320282000000 GasUsageToday 3.148
1620226380280000000 GasUsageToday 3.148
1620226440289000000 GasUsageToday 3.148
1620226500279000000 GasUsageToday 3.148

It seems that the latest entry from GasUsageToday is from 1 hour ago which is the time that I upgraded from M2 to M4.

I don’t know if that is what you expected,or if it is a problem.

We might get more info from a simpler query.

var fred = DSMRM3GasMeter_Delivery.historicState(now.minusDays(1))
if (fred !==null) {
   logInfo("test", "historic item {}", fred)
} else {
   logInfo("test", "Null return")
}

You’re relying on your system default persistence setting, but it is influxdb complaining so that would appear to be how it is set.

As an aside, what is the purpose of these “look up your own backside” statements?
DSMRM3GasMeter_Delivery.postUpdate(DSMRM3GasMeter_Delivery.state as Number)

As expected your script returns a FAILED in the Openhab logging.

2021-05-06 10:02:01.598 [INFO ] [org.openhab.core.model.script.test  ] - historic item [FAILED toString()]
2021-05-06 10:02:01.633 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-06 10:02:01.635 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Gasusage-2' failed: The argument 'state' must not be null. in Gasusage
2021-05-06 10:03:00.089 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-06 10:03:00.123 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry

I don’t know if that is what you expected,or if it is a problem

It’s a problem since it doesn’t update my GasUsageToday value anymore. Which is a value I use in Habpanel.

As an aside, what is the purpose of these “look up your own backside” statements?
DSMRM3GasMeter_Delivery.postUpdate(DSMRM3GasMeter_Delivery.state as Number)

This is a piece of legacy from my issue when upgrading to OH3. Before the upgrade I used Number:Volume but I ran into issues. Now all the values are just Number values which makes this line obsolete. I removed the line from the rule.

After the upgrade I removed the Influx Persistance from MainUI and reinstalled it again. I checked if Influx is configured as Default persistance service and this is the case (in fact, there is only one persistance configuration configured).

That’s the persistence service telling us it is choking on all this, which we kind of knew anyway.

Aha, you’ve changed the Item type. influxdb will choke if you change say a String to a Number of course, you have to drop old data and start
over.
I think you are in the same boat.

I’d be inclined to overcome whatever issue you had with Number:Volume really.

I understand what you are saying, but of course I would like to do anything in order to keep my historic data. First of all because I would like to know my overcapacity from the Solar panels and second because I’m isolating my house piece by piece and I would like to monitor gas consumption over the year. There is more than 2 years of historic data in Influx.

The part above has nothing to do with the issue I face. I find it strange that after a patch update Influx seems to stop reacting. The data in Influx is no different than before. I’m not downgrading Influx, just down and upgrading Openhab in order to run these tests.

I’d be inclined to overcome whatever issue you had with Number:Volume really.

I’ve had some issues with calculations in the past. That’s why I always change the Item settings back to just Number values if I perform calculations or compare values.

Well, it’s up to you. You’re looking at data recorded for one Item type and trying to shoehorn it into a different Item type.
The same data gets recorded into influxdb for a Number or Number:Volume type, but gets handled differently by the persistence service on update or retrieval.
This seems like a pretty good fit to your problem, you could verify that by experimenting with just one Item.

Can’t help with secret calculations, you can always extract pure numeric values from Quantity type Items in rules scaled to the units of your choice,or calculate directly with Quantities.

Do you know if Number values should be stored as Integers in Influx? (They are float as I just found out). I have a script found on the internet which ‘converts’ values to Integers. I could try that before deleting the whole series and measurements.

#!/bin/bash

MEASUREMENTS=$(cat << EOF
DSMRM3GasMeter_Delivery
EOF
)

command="SELECT value::integer INTO temp FROM measurement; DROP MEASUREMENT measurement; SELECT value::integer INTO measurement FROM temp; DROP MEASUREMENT temp;"

for m in $MEASUREMENTS; do
	echo "Updating $m..."
	influx -database 'openhab_db' -username 'admin' -password '<password>' -execute "$(sed "s#measurement#$m#g" <<< $command)"
done

No idea. I really doubt it, since you can come along at any time and populate a Number Item with a decimal value.

Hi, Sorry maybe I bother you with stupid things, but in order to test things, I tried to edit your testrule with an item which I never changed since I’m running Openhab (last 3 years).

	var fred = LocalSun_StartTime.historicState(now.minusDays(1))
if (fred !==null) {
   logInfo("test", "historic item today {}", fred)
} else {
   logInfo("test", "Null return")
}

I thought, let’s check if I can read the sunrise event from yesterday. But the logs show exactly the same.

2021-05-06 15:14:27.231 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Gasusage.rules'
2021-05-06 15:15:00.062 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-06 15:15:00.112 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry
2021-05-06 15:15:00.114 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'PowerConsumptionPerDay-2' failed: The argument 'state' must not be null. in PowerConsumptionPerDay
2021-05-06 15:15:01.237 [INFO ] [org.openhab.core.model.script.test  ] - historic item today [FAILED toString()]
2021-05-06 15:15:01.260 [INFO ] [b.internal.InfluxDBStateConvertUtils] - Could not find item 'null' in registry

Influx data (just to prove there is something in the database:

1620306960988000000 LocalSun_StartTime 1620275400000
1620307020984000000 LocalSun_StartTime 1620275400000
1620307080986000000 LocalSun_StartTime 1620275400000
1620307140989000000 LocalSun_StartTime 1620275400000
1620307200984000000 LocalSun_StartTime 1620275400000
1620307260986000000 LocalSun_StartTime 1620275400000
1620307320984000000 LocalSun_StartTime 1620275400000
1620307380986000000 LocalSun_StartTime 1620275400000
1620307440984000000 LocalSun_StartTime 1620275400000

I just like to prove somehow that there is something wrong in de code of Openhab since M3.

Okay, your influx doesn’t work. What are you going to do now?

Might be some help here

The issue is fixed in OH 3.1.0M5. Thanks for the support.