Energy Card

Nice card. Just to share how some HA folks are approaching this

Besides KW consumption there’s also cost calculation.

Perhaps I wasn’t quite clear. I use rrd4j as a default service and I persist manually some Items to influxDB.

It seems that the history rule works now (after deleting the rule and add-on) but I still get an error message for the Daily usage rule:

2023-11-06 14:48:33.558 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '9c3fb9bcb7' failed: org.graalvm.polyglot.PolyglotException: SyntaxError: <eval>:6:0 Expected eof but found }

I don’t see the Blockly blocks either.

Could be a missing dependeny still.
please install this blockly extension

That did the trick. Now I can see the Blockly blocks in the Edit script page. Many thanks for your help.

1 Like

Thanks for your feedback and sorry for the inconvenience.
I am new to rule templates, that’s why I forget about the dependency…
I will update the template post so that nobody else falls into that trap…

Edit: done
@rlkoshak We solved it :wink:

You are referring to both Actual energy consumption [;] and Historical energy consumption [;], correct?

I can install them without error.

Looking at the historical rule it looks reasonable.

I don’t have all these Items so I can’t create an instance of the rule that makes sense in my system right now (I do have an energy meter so stay tuned) but looking at the JS code in the template the only thing that stands out is that first line doesn’t actually do anything:

items.getItem('{{energyUseageDaily}}').postUpdate((items.getItem('{{energyCounter}}').history.deltaSince(, '{{persistenceService}}')));

It pulls the deltaSince but doesn’t save it to a variable so it’s kind of a noop.

But the rest of the rule looks OK so it should be fine as written.

The actual energy rule might have a problem. The last line of the script action seems to have an unmatched closing }. But it’s not entirely clear that’s really the case and how that happened at all using Blockly in the first place.

I see that you got it to work so that } is a mystery.

Note that in OH 4 most of those blocks are now implemented by the base Blockly under the Dates and Times category. The blocks are implemented slightly differently but if you used those users of your templates wouldn’t need to install the separate library.

Thanks for your feedback.
I tried to get rid of that dependency, but could not find the correct block. Can you please point me to which one you are referring to ?

No, it updates the daily usage Item….


Found it, will change the template tonight….

You are right. It’s early here, need more coffee.

midnight today:

The temporal stuff is a little tricky. See

The now.minus days of the week is a little tricky in that you’ll have to make two “copies”.

Day of month and day or year follow the same pattern. Just change the property in that block as appropriate.

I’m just way behind. :wink: Gotta love these almost in time replies. :smiley:

1 Like

Thank’s Rich, but this one is not working.

2023-11-06 23:18:57.752 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: TypeError: Cannot read property "value" from undefined
	at <js>.:program(<eval>:4) ~[?:?]
	at org.graalvm.polyglot.Context.eval( ~[?:?]
	at ~[?:?]
	at ~[?:?]
	at javax.script.AbstractScriptEngine.eval( ~[java.scripting:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval( ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval( ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval( ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval( ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0( ~[?:?]
	at java.util.Optional.ifPresent( [?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute( [bundleFile:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions( [bundleFile:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runRule( [bundleFile:?]
	at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$ [bundleFile:?]
	at java.util.concurrent.Executors$ [?:?]
	at [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker( [?:?]
	at java.util.concurrent.ThreadPoolExecutor$ [?:?]
	at [?:?]
2023-11-06 23:18:57.757 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'energyUsageActual' failed: org.graalvm.polyglot.PolyglotException: TypeError: Cannot read property "value" from undefined

Start of Month and Year are working with the following

Edit : Got it working, it is easier than I thought :wink:

1 Like

@STSC @nikos7179 @jlikonen
I have updated the marketplace templace to remove the external dependency, please try the new version.

@rlkoshak Thanks Rich for your excellent help !!!

I updated the marketplace templates and both Edit script pages show the Blockly block. I have a small glitch, though. Some of the Items show “NULL” (see the screencopy) and this has been the case since I installed the widget.

Näyttökuva 2023-11-07 063025

I checked that e.g. the Item for Consumption Actual Month is “NULL”.

This is really nice widget and I’m going to adapt it to my other purposes.

Please note that the weekly/monthly/yearly Items get updated only at the first of a week/month/year, so you won’t see any values until then.
Your missing billing cycle value looks like a bug/issue. Anything in the logs? Did you set the DateTime Item correct?

This is a really simple but informative widget, I have modified it for my gas- and watermeter to have them shown with the same look and feel…

OK, thanks. I understand that to get the monthly/yearly values I need to wait until next month/year but Yesterday’s value is also null.

My Item for billing cycle start is 2023-01-01T00:00:00.000+0200. So I wonder whether this is wrong.

Do you have a meter value from that day and time ? If not, no delta can be calculated…

Yes, I have a energy meter reading for that date/time. There are no error/warning messages in the log. I think I need to look at your code and see how the billing cycle is handled.

I just checked, mine is showing 0kWh for the billing cycle since the update, so there indeed is a bug.
I will check later today.

1 Like

Take your time.

One more thing, I told earlier that we have the netting system in Finland and I have the hourly netted values (in kWh). In the Blockly you are calculating delta values but my netted values are already delta values so I guess I would need to calculate a sum in order to calculate daily/weekly/monthly values.

Found the bug within setting a wrong persistence service for the billing cycle value.

I think that does not matter.
The rules use the raw meter counter values and calculate the appropriate delta values. Nothing would need to be changed.

Edit: Might be that I doe not understand correct what you mean with netted values.
E.g. my meter delivers the usage value and, if I would have solar panels, production values at a different data point.
I am using the usage values from the meter.