Rule to reset at midnight not working?

II’m sure I’ve got something very simple very wrong here…

I’m counting up KwH usage and what I want to do is reset it back to 0.00 every night so that I get some nice per day charts. but the code below doesn’t seem to work? It should be running at 0:01 ever day but doesn’t work…

rule "Clear daily KwH consumption"
	when
		Time cron "0 1 0 * * ?"  // every day at midnight, 00:01
	then
		Power01KwHDaily.postUpdate(Power01KwHDay)
		Power01KwHDay.postUpdate(0.00)
	end

I use this for my daily resets…

rule "Reset daily energy counters"
when
    Time is midnight
then

Here is mine:

rule "Clear daily consumption"
when
Time cron "0 0 0 * * ?" // every day
then
	postUpdate(DailyEnergyConsumption, 0)
end

Thanks guys, I think my timed rule was working but there is something wrong with the first line after the “then”…

2016-08-17 00:21:00.218 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Clear daily KwH consumption 2
java.lang.IllegalStateException: Could not invoke method: org.openhab.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,org.openhab.core.types.State) on instance: null
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:738) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._featureCallOperation(XbaseInterpreter.java:713) ~[na:na]

Is there an easy way to do a NULL check somehow?

rule "init"
when
    System started or
    Time is midnight
then {
    createTimer(now.plusSeconds(120)) [|
        if (item.state == NULL) item.postUpdate(0)
    ]
}
end

I think you may need to use the following instead:

Power01KwHDaily.postUpdate(Power01KwHDay.state)
1 Like

Hello,

Sorry I didn’t catch this, could we say that

Power01KwHDaily.postUpdate(Power01KwHDay.state)

is equivalent to:

postUpdate(Power01KwHDay, 0)

?

Ummmm… I don’t think so.

  • Power01KwHDaily.postUpdate(Power01KwHDay.state)
    Power01KwHDaily is an item and Power01KwHDay.state is the current value of the Power01KwHDay item

is equivalent to:

  • postUpdate(Power01KwHDaily, Power01KwHDay.state)
    Power01KwHDaily is again an item and Power01KwHDay.state is the new value you are to the Power01KwHDaily.

In the first one you are calling to the postUpdate method of the Power01KwHDaily item, you can consider it as an action of the item itself. The second one is a generic postUpdate action, so you need to specify the item who’s value you want to update and the new value itself.

The syntax that you have used (postUpdate(Power01KwHDay, 0)) will update the status of the Power01KwHDay item to 0, and as you can see, the Power01KwHDaily items value is not referenced nor used.

Is it more clear now? :wink:

If you are still confused, don’t hesitate to ask!

yes thankyou :slight_smile: