Every night at 23u59 I’m writing the value of an item to another item. It’s just a summary of the total yield of my solar panels. I use the value to subtract it from the totals of the day after. To achieve this I’m using this rule:
rule "solar-rule1"
when
Time cron "00 59 23 1/1 * ? *"
then
Yield.sendCommand(Solar_Total_Yield_kWh.state as DecimalType)
end
It has been working great for the last couple of months but there was always this error in Visual Studio Code that I don’t understand.
If I try to use this as rule, the Visual Studio code error goes away but then I get an error in the console.
Yield.sendCommand(Solar_Total_Yield_kWh.state as Number)
[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'solar-rule2': Could not cast NULL to org.eclipse.smarthome.core.library.types.DecimalType; line 5, column 79, length 26
rule "solar-rule2"
when
Item Solar_Total_Yield_kWh changed
then
Yield_DayTotal.postUpdate((Solar_Total_Yield_kWh.state as DecimalType) - (Yield.state as DecimalType))
end
Solar_Total_Yield_kWh basically holds the total acquired kwh (actually megawatthours) and subtracts yesterday’s total from it (Yield) to get the earnings for today = Yield_DayTotal
You could also add some log outputs into the scripts:
var yield = Yield.state as Number
logInfo("Test:", "yield: {}", yield);
And see what happens.
One solution could also be to add
Thread::sleep(500)
at the beginning of the rule (first line after ‘then’). The rule might be triggered, but the new value is not yet available on Solar_Total_Yield_kWh.state which may lead to wrong results. It is a strange behaviour of OH which sometimes happens, there are long threads explaining this.
This is the wrong solution! The only time an Item’s state might not be updated by the time the event triggers the rule, is when using the ‘received command’ trigger. But instead of using Thread::sleep, use the receivedComand implicit variable to get the command that was sent to the Item, which the state should be changing to. This is not a bug, is expected behavior, and it never just happens randomly !