- Win 10
Running on laptop
Java Runtime Environment: 8 Build 181
Openhab 1.8
This rule works if I use a day of the 1st but fails if I use the 20th.
How can I use the 20th of last month if its 1-20th this month?
Thanks
rule "Update gas Costs"
when
Item Gas_usage changed //received update //for testing
then
// all gas cost rules using db40
var Number MonthagoReadingtemp
var Number midnightReading
var Number yesterdayMidnightReading
var Number days2agoMidnightReading
var Number currentReading
var Number days2agoUsageCF
var Number yesterdayUsageCF
var Number todayUsageCF
var Number yesterdayCost
var Number days2agoCost
var Number todayCost
var Number CFCost = 3.3120
if (Gas_usage.state instanceof DecimalType) {
MonthagoReadingtemp = (Gas_usage.deltaSince(now.toDateMidnight().withDayOfMonth(20)))
// MonthagoReadingtemp = (Gas_usage.sumSince(now.withDayOfMonth(20), "db4o"))
// MonthagoReadingtemp = (Gas_usage.sumSince(now.toDateMidnight().withDayOfMonth(1), "db4o"))
// MonthagoReadingtemp = (Gas_usage.historicState(now.withDayOfMonth(1)).state as DecimalType)
days2agoMidnightReading = (Gas_usage.historicState(now.toDateMidnight.minusDays(2)).state as DecimalType)
yesterdayMidnightReading = (Gas_usage.historicState(now.toDateMidnight.minusDays(1)).state as DecimalType)
midnightReading = (Gas_usage.historicState(now.toDateMidnight).state as DecimalType)
currentReading = Gas_usage.state as DecimalType
days2agoUsageCF = ((yesterdayMidnightReading - days2agoMidnightReading)/1000)
yesterdayUsageCF = ((midnightReading - yesterdayMidnightReading)/1000)
todayUsageCF = ((currentReading - midnightReading)/1000)
todayCost = todayUsageCF*CFCost
days2agoCost = days2agoUsageCF*CFCost
yesterdayCost = yesterdayUsageCF*CFCost
//postUpdate(MonthagoReading,MonthagoReadingtemp)
//logInfo("MonthagoReadingtemp before math", (MonthagoReadingtemp.toString))
MonthagoReadingtemp = MonthagoReadingtemp/1000
postUpdate(GasCFMthismonth,MonthagoReadingtemp)
//logInfo("MonthagoReadingtemp after math", (MonthagoReadingtemp.toString))
MonthagoReadingtemp = MonthagoReadingtemp*CFCost
// postUpdate(MonthagoReading,MonthagoReadingtemp)
logInfo("MonthagoReadingtemp new cost", (MonthagoReadingtemp.toString))
postUpdate(GasTodayUsage, todayUsageCF)
postUpdate(GasTodayCost, todayCost)
postUpdate(GasYesterdayUsage, yesterdayUsageCF)
postUpdate(GasYesterdayCost, yesterdayCost)
postUpdate(Gas2DaysAgoUsage, days2agoUsageCF)
postUpdate(Gas2DaysAgoCost, days2agoCost)
postUpdate(MonthagoReading,MonthagoReadingtemp)
}
end
this is the error
2020-01-01 10:43:40.065 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule âUpdate gas Costsâ
java.lang.NullPointerException: null
at org.openhab.model.script.lib.NumberExtensions.operator_divide(NumberExtensions.java:46) ~[na:na]
at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:729) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._featureCallOperation(XbaseInterpreter.java:713) ~[na:na]
at sun.reflect.GeneratedMethodAccessor46.invoke(Unknown Source) ~[na:na]