Copying value of an item to another item

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

Can anyone explain to me what’s the deal here?

The error seems to be about solar-rule2. What does this rule do?

It seems that the value used in solar-rule2 is NULL (e.g. does not have a value at all) and thats why it fails.

Well, rule2 contains the following:

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

Have you tried using “as Number” in both rules?

Ok, will try :slight_smile:

Using number in the rules, gives the following in the console:

Rule 'solar-rule2': Could not cast NULL to java.lang.Number; line 5, column 74, length 21

These Items must not be persisted. To work around this, check in your rule that the Item did not change to NULL…

if (triggeringItem.state == NULL || triggeringItem.state == UNDEF) return;

I think you need to debug this a bit.

Try logging into KARAF console (see here: https://www.openhab.org/docs/administration/console.html)

and then get the state of your items:

smarthome:status Solar_Total_Yield_kWh 
smarthome:status Yield_DayTotal
smarthome:status Yield

see if any of those are NULL.

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 :slightly_smiling_face:!

Thanks for explaining this. Learning something new!

1 Like

You guys lost me there… :frowning:

openhab> smarthome:status Solar_Total_Yield_kWh
3638.12
openhab> smarthome:status Yield_DayTotal
11.592
openhab> smarthome:status Yield
NULL

So Yield seems to be NULL at this time.

To troubleshoot, try saving this into a rule file and post the log…

rule "Test solar rule"
when 
    System started
then
    logInfo("Test", "Solar_Total_Yield_kWh: {}, Yield: {}, Yield_DayTotal: {}", Solar_Total_Yield_kWh.state, Yield.state, Yield_DayTotal.state)
    Yield.sendCommand(Solar_Total_Yield_kWh.state.toString)
    Thread:sleep(500)
    logInfo("Test", "Solar_Total_Yield_kWh: {}, Yield: {}, Yield_DayTotal: {}", Solar_Total_Yield_kWh.state, Yield.state, Yield_DayTotal.state)
end

@SpaceGlider, this is another example where you need to wait for the EventBus to catch up, so that the state of an Item matches the command sent.

Edit… I think I spotted the problem, but you did not report any errors in the log. Try using…

Yield.sendCommand(Solar_Total_Yield_kWh.state.toString)
``
1 Like

Hi, this solved my problem, thanks!

1 Like