Energy Card

This is the Rule I’m using:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: ABB_A43_imp_energy
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >-
        configuration: {}

        triggers:
          - id: "1"
            configuration:
              itemName: ABB_A43_imp_energy
            type: core.ItemStateChangeTrigger
        conditions: []

        actions:
          - inputs: {}
            id: "2"
            configuration:
              blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block type="oh_event" id="wR2@!=5jfQG3|Tnh{+~x" x="-14" y="-145"><field name="eventType">postUpdate</field><value name="value"><shadow type="text" id="jWgD4t7.O;)]3OP#c3_R"><field name="TEXT">value</field></shadow><block type="oh_get_persistvalue" id="o{T16a(Kz8%H}OiFX!92"><field name="methodName">deltaSince</field><value name="itemName"><shadow type="oh_item" id=";^(0GHJN+qNYn[-TH#fs"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="Tzh_y}fz^X{LSYcL)1:A"><mutation itemName="ABB_A43_imp_energy" itemLabel="Energie Gesamt"></mutation><field name="itemName">ABB_A43_imp_energy</field></block></value><value name="persistenceName"><shadow type="oh_persistence_dropdown" id="4Ip6?cO^P}fVH,;U(Ig1"><field name="persistence">default</field></shadow><block type="oh_persistence_dropdown" id="6jc}NQ;mmWl:(zG!(jGl"><field name="persistence">rrd4j</field></block></value><value name="dayInfo"><block type="deibich:blockly:dateTime_zdt_set_time_with_fields" id="BXkQhKmnZ}Ye]N,[/KuW"><field name="HOURS">0</field><field name="MINUTES">0</field><field name="SECONDS">0</field><value name="DATE"><shadow type="oh_zdt" id="-YwB#{75n6x@v:LZ_wRf"><field name="day">2023-08-20</field></shadow><block type="oh_zdt_now" id="?M%+fG)$eQATRt1NyDX{"></block></value></block></value></block></value><value name="itemName"><shadow type="oh_item" id="Pt$^PEJnHsZ}1wO!(iS%"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="+h(C{ZBBbQbWF0}5O-T("><mutation itemName="ElectricityMeter_energyUsageToday" itemLabel="Energieverbrauch Heute"></mutation><field name="itemName">ElectricityMeter_energyUsageToday</field></block></value><next><block type="oh_event" id="i.q{)XrqTs1%hj:3tW1h"><field name="eventType">postUpdate</field><value name="value"><shadow type="text" id="jWgD4t7.O;)]3OP#c3_R"><field name="TEXT">value</field></shadow><block type="oh_get_persistvalue" id="~Fs,0-d,f:r)/)Po0fzF"><field name="methodName">deltaSince</field><value name="itemName"><shadow type="oh_item" id="eV4}S-t^%::[S(zE:Xlf"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="SOrpMoT6[8q]knugKx_q"><mutation itemName="ABB_A43_imp_energy" itemLabel="Energie Gesamt"></mutation><field name="itemName">ABB_A43_imp_energy</field></block></value><value name="persistenceName"><shadow type="oh_persistence_dropdown" id="4Ip6?cO^P}fVH,;U(Ig1"><field name="persistence">default</field></shadow><block type="oh_persistence_dropdown" id="FDP+td7ya7ZJiW/APx7q"><field name="persistence">rrd4j</field></block></value><value name="dayInfo"><block type="deibich:blockly:dateTime_zdt_set_time_with_fields" id="H1?zJ%xWP(djG9331v=o"><field name="HOURS">0</field><field name="MINUTES">0</field><field name="SECONDS">0</field><value name="DATE"><shadow type="oh_zdt" id="-YwB#{75n6x@v:LZ_wRf"><field name="day">2023-08-20</field></shadow><block type="oh_zdt_plusminus" id="3CAJ%?j`%-`S(cs646d^"><field name="plusminus">minus</field><field name="period">Days</field><value name="offset"><shadow type="math_number" id="-yJ-wrzk}kF?MBl$=Q.v"><field name="NUM">0</field></shadow><block type="math_arithmetic" id="-hadISd2nVwTrE0S+dqb"><field name="OP">MINUS</field><value name="A"><shadow type="math_number" id="}uT^gKPy.r__jTDW@tye"><field name="NUM">1</field></shadow><block type="oh_get_zdt_part" id="6D`|~dy77.lp8moxom7@"><field name="temporalPart">getDayOfWeek</field><value name="zdt"><block type="oh_zdt_now" id="?/[,lh[c8pq@]{#vNLIq"></block></value></block></value><value name="B"><shadow type="math_number" id="8-H0GA(SID)LfG)nFQ)x"><field name="NUM">1</field></shadow></value></block></value></block></value></block></value></block></value><value name="itemName"><shadow type="oh_item" id="Pt$^PEJnHsZ}1wO!(iS%"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="U^2/Ta?7FbQCrVTT$/6:"><mutation itemName="ElectricityMeter_energyUsageWeek" itemLabel="Energieverbrauch Woche"></mutation><field name="itemName">ElectricityMeter_energyUsageWeek</field></block></value><next><block type="oh_event" id="ekS[%nx~(};zhL!aj3cq"><field name="eventType">postUpdate</field><value name="value"><shadow type="text" id="jWgD4t7.O;)]3OP#c3_R"><field name="TEXT">value</field></shadow><block type="oh_get_persistvalue" id="iLkwL4E;@rmKIiZ6qll*"><field name="methodName">deltaSince</field><value name="itemName"><shadow type="oh_item" id="eV4}S-t^%::[S(zE:Xlf"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="X2hEF34^7YJOSj0$`K@r"><mutation itemName="ABB_A43_imp_energy" itemLabel="Energie Gesamt"></mutation><field name="itemName">ABB_A43_imp_energy</field></block></value><value name="persistenceName"><shadow type="oh_persistence_dropdown" id="4Ip6?cO^P}fVH,;U(Ig1"><field name="persistence">default</field></shadow><block type="oh_persistence_dropdown" id="MP#fhHAZvlfF6G`(xA,U"><field name="persistence">rrd4j</field></block></value><value name="dayInfo"><block type="deibich:blockly:dateTime_zdt_set_time_with_fields" id="Eu`Ge?Xs@@UdL=#r~vIp"><field name="HOURS">0</field><field name="MINUTES">0</field><field name="SECONDS">0</field><value name="DATE"><shadow type="oh_zdt" id="-YwB#{75n6x@v:LZ_wRf"><field name="day">2023-08-20</field></shadow><block type="oh_zdt_plusminus" id=".wyGf7(:ck4^fV]mPZ~I"><field name="plusminus">minus</field><field name="period">Days</field><value name="offset"><shadow type="math_number" id="-yJ-wrzk}kF?MBl$=Q.v"><field name="NUM">0</field></shadow><block type="math_arithmetic" id="#Bag6+_vL=*lvOBmxxHm"><field name="OP">MINUS</field><value name="A"><shadow type="math_number" id="}uT^gKPy.r__jTDW@tye"><field name="NUM">1</field></shadow><block type="oh_get_zdt_part" id="MJ=s3o9zRWUwl3L478-%"><field name="temporalPart">getDayOfMonth</field><value name="zdt"><block type="oh_zdt_now" id="]gJ{KIp%t+llJE:Neh_z"></block></value></block></value><value name="B"><shadow type="math_number" id="9uwi/uaZ5-rxJEwn(Zg="><field name="NUM">1</field></shadow></value></block></value></block></value></block></value></block></value><value name="itemName"><shadow type="oh_item" id="Pt$^PEJnHsZ}1wO!(iS%"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="Q7zR(dyrQ!pqzAIc8v?j"><mutation itemName="ElectricityMeter_energyUsageMonth" itemLabel="Energieverbrauch Monat"></mutation><field name="itemName">ElectricityMeter_energyUsageMonth</field></block></value><next><block type="oh_event" id=";{L95656B0OHC6ZIf9Qn"><field name="eventType">postUpdate</field><value name="value"><shadow type="text" id="jWgD4t7.O;)]3OP#c3_R"><field name="TEXT">value</field></shadow><block type="oh_get_persistvalue" id="KW|1F}!ZJpL#aZem8%[s"><field name="methodName">deltaSince</field><value name="itemName"><shadow type="oh_item" id="eV4}S-t^%::[S(zE:Xlf"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="d[6-:!?sti()whBdPIm6"><mutation itemName="ABB_A43_imp_energy" itemLabel="Energie Gesamt"></mutation><field name="itemName">ABB_A43_imp_energy</field></block></value><value name="persistenceName"><shadow type="oh_persistence_dropdown" id="4Ip6?cO^P}fVH,;U(Ig1"><field name="persistence">default</field></shadow><block type="oh_persistence_dropdown" id="ZD^J(z8(zg7JMpLh4Lx2"><field name="persistence">rrd4j</field></block></value><value name="dayInfo"><block type="deibich:blockly:dateTime_zdt_set_time_with_fields" id="E4[}=fSeiaFbher(C?/e"><field name="HOURS">0</field><field name="MINUTES">0</field><field name="SECONDS">0</field><value name="DATE"><shadow type="oh_zdt" id="-YwB#{75n6x@v:LZ_wRf"><field name="day">2023-08-20</field></shadow><block type="oh_zdt_plusminus" id="#WP9ziro@OOi[)3xvs,|"><field name="plusminus">minus</field><field name="period">Days</field><value name="offset"><shadow type="math_number" id="-yJ-wrzk}kF?MBl$=Q.v"><field name="NUM">0</field></shadow><block type="math_arithmetic" id="8i5+AXmgX#^-F89,Lu]X"><field name="OP">MINUS</field><value name="A"><shadow type="math_number" id="}uT^gKPy.r__jTDW@tye"><field name="NUM">1</field></shadow><block type="oh_get_zdt_part" id=".7VS+[H|NoEI$`bO;E=y"><field name="temporalPart">getDayOfYear</field><value name="zdt"><block type="oh_zdt_now" id="dJW)ytlA,R%lc9(0T+jy"></block></value></block></value><value name="B"><shadow type="math_number" id="Bi)-*(@7#0:8^dFK%PRR"><field name="NUM">1</field></shadow></value></block></value></block></value></block></value></block></value><value name="itemName"><shadow type="oh_item" id="Pt$^PEJnHsZ}1wO!(iS%"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="n0#(pT9Uj$D.G9qpYSf@"><mutation itemName="ElectricityMeter_energyUsageYear" itemLabel="Energieverbrauch Jahr"></mutation><field name="itemName">ElectricityMeter_energyUsageYear</field></block></value><next><block type="oh_event" id="c)`^`GpeCp)rQf3{pKLq"><field name="eventType">postUpdate</field><value name="value"><shadow type="text" id="jWgD4t7.O;)]3OP#c3_R"><field name="TEXT">value</field></shadow><block type="oh_get_persistvalue" id="~/A/N.nVj]EIak,;a#LL"><field name="methodName">deltaSince</field><value name="itemName"><shadow type="oh_item" id="eV4}S-t^%::[S(zE:Xlf"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="MeG%yL]#$Poq~}yMXxi;"><mutation itemName="ABB_A43_imp_energy" itemLabel="Energie Gesamt"></mutation><field name="itemName">ABB_A43_imp_energy</field></block></value><value name="persistenceName"><shadow type="oh_persistence_dropdown" id="4Ip6?cO^P}fVH,;U(Ig1"><field name="persistence">default</field></shadow><block type="oh_persistence_dropdown" id="2hPNgWokiMplnWPc@fDy"><field name="persistence">rrd4j</field></block></value><value name="dayInfo"><block type="oh_zdt_fromText" id="Yi{o9`Uv!L[sTXWKzH%s"><value name="day"><shadow type="text" id="%HAh{g{;*ad]1^jZVpn("><field name="TEXT">2023-08-19</field></shadow><block type="oh_getitem_state" id="#SNHcwEDkwgxVvQEncs+"><value name="itemName"><shadow type="oh_item" id="`)F90,]i,zF0!^MTlPjh"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="Qa|FV[|k3D.`.c}b7|CX"><mutation itemName="ElectricityMeter_lastReading" itemLabel="Energieverbrauch Ablesedatum"></mutation><field name="itemName">ElectricityMeter_lastReading</field></block></value></block></value></block></value></block></value><value name="itemName"><shadow type="oh_item" id="Pt$^PEJnHsZ}1wO!(iS%"><mutation itemName="MyItem" itemLabel="MyItem"></mutation><field name="itemName">MyItem</field></shadow><block type="oh_item" id="QCMA}5ELYGMsf6F4c4yu"><mutation itemName="ElectricityMeter_energyUsagePeriod" itemLabel="Energieverbrauch Periode"></mutation><field name="itemName">ElectricityMeter_energyUsagePeriod</field></block></value><next><block type="controls_if" id="/j*}b.(ZeI=_tC-otP[S"><value name="IF0"><block type="logic_compare" id="#;6wvoGsWi%+Q)QzCVL|"><field name="OP">EQ</field><value name="A"><block type="oh_zdt_toText" id="L)?pberGBJ,Dsg9V)gl("><field name="withtime">asOHTime</field><value name="date"><shadow type="oh_zdt" id=")eCC#^LvsN;S?Y8D~V+?"><field name="day">2023-09-03</field></shadow><block type="oh_zdt_now" id="._G]KdO|Xj,mb+;2@g#:"></block></value></block></value><value name="B"><block type="text" id="*(ZRhuuD{OCD3HTX=Xua"><field name="TEXT">00:00:00</field></block></value></block></value></block></next></block></next></block></next></block></next></block></next></block></xml>
              type: application/javascript
              script: >
                items.getItem('ABB_A43_energy_consumption_today').postUpdate((items.getItem('ABB_A43_imp_energy').history.deltaSince(time.ZonedDateTime.now().withHour(0).withMinute(0).withSecond(0), 'rrd4j')));

                items.getItem('ABB_A43_energy_consumption_actual_week').postUpdate((items.getItem('ABB_A43_imp_energy').history.deltaSince(time.ZonedDateTime.now().minusDays((((time.ZonedDateTime.now()).dayOfWeek().value()) - 1)).withHour(0).withMinute(0).withSecond(0), 'rrd4j')));

                items.getItem('ABB_A43_energy_consumption_actual_month').postUpdate((items.getItem('ABB_A43_imp_energy').history.deltaSince(time.ZonedDateTime.now().minusDays((((time.ZonedDateTime.now()).dayOfMonth()) - 1)).withHour(0).withMinute(0).withSecond(0), 'rrd4j')));

                items.getItem('ABB_A43_energy_consumption_actual_year').postUpdate((items.getItem('ABB_A43_imp_energy').history.deltaSince(time.ZonedDateTime.now().minusDays((((time.ZonedDateTime.now()).dayOfYear()) - 1)).withHour(0).withMinute(0).withSecond(0), 'rrd4j')));

                items.getItem('ABB_A43_energyUsagePeriod').postUpdate((items.getItem('ABB_A43_imp_energy').history.deltaSince(time.toZDT(items.getItem('ABB_A43_lastReading').state), 'rrd4j')));

                if (((time.ZonedDateTime.now()).format(time.DateTimeFormatter.ofPattern('yyyy-MM-dd\'T\'HH:mm:ss.SSSZ'))) == '00:00:00') {

                }
            type: script.ScriptAction
    type: script.ScriptAction

I’m running OH4.0.3 and using JavaScript Scripting.

Looks like you copied the complete example into your script editor, which is wrong.
Please go to the code tab of your rule and override it completely with my code posted above.

Yes, what a stupid mistake. I’ll have to leave now but made a very quick test. Some of the consumption items are now populated which is a good sign. I’ll do more testing tomorrow. Many thanks for your help and great widget.

1 Like

Are you sure? I thought Tasmota can only send the energy readings every 10s over MQTT?

Nope, you can set the update frequency via a console command. My reader came preflashed with a short period already set.

I have now created the two rule templates that can be installed from the addon marketplace and configured through the UI

Actual energy consumption [4.0.0.0;4.9.9.9] - Add-on Marketplace / Rule Templates - openHAB Community

Historical energy consumption [4.0.0.0;4.9.9.9] - Add-on Marketplace / Rule Templates - openHAB Community

It seems that I still have some problems with the first rule. When looking at the codes for both rules above it looks to me that the first one is possibly missing some code. When I paste the first code in the code page and look at the script in the Edit script page it seems to be empty whereas the 2nd rule shows the Blockly code. Also when I try to save the script of the first rule I get the following error message.


So, I’m wondering what I’m missing. I also tried to paste the code in Add-on marketplace but same result.

Do not copy and paste marketplace rule templates, they need to be installed!!!
Within openHAB goto Addon-Store, select automation at the bottom, scroll to rule templates and press add on the two rules
Then go to your rules and press the plus symbol. You now have the new option to create a new rule from a template. Selecting this will show you the install templates.
After selecting one, you will see the config options where you set the persistence service and your items.

OK, thanks, now I understand. I still have few questions. Is ElectricityMeter_lastReading e.g. 1.1.2023? If so how do you change that next year? What is ElectricityMeter_energyUsagePeriod, is it 1 year?

I had to send my meater counter value to my energy provider before April 20th 2023, dod so on April 12th 2023. So lastReading is set to 2023-04-12. This is then the start of the new billing cycle (period)
Actually, there is no option in the widget to set this date, but hiho, there is always room for improvement.
I will add a button to set this Item to the actual date, so when one does the reading and sends the counter value to the provider, just press that button and ne cycle starts…

It is from the lastReading (billing) date until today.

From the rules Its missing the part that calculate the yesterday value?

edit :Ok my mistake i found it.

1 Like

It might be a bit irritating, as it is just the last daily value. Same for last week/month/year….

1 Like

i get also this errors
Failed to execute script:

org.graalvm.polyglot.PolyglotException: SyntaxError: :6:0 Expected eof but found }

i have install the rules from the marketplace

For what rule or both of them ?
Need to check when I am back home this evening…

The rule that calculate the actual values

i solve this problem by manual changed the items on the blockly. its seems that even if i setup (all the items) the rule from the template its not working

OK, thanks. Now I understand the meaning of ElectricityMeter_lastReading and ElectricityMeter_energyUsagePeriod. I checked that ElectricityMeter_energyUsagePeriod is populated.

I also get the same error code with the Actual energy consumption rule:

2023-11-05 13:22:48.475 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'e79485a8a1' failed: org.graalvm.polyglot.PolyglotException: SyntaxError: <eval>:6:0 Expected eof but found }
}
^

but I also get an error message with the Historic rule:

2023-11-05 13:23:26.585 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '9991cc3525' failed: org.openhab.core.items.ItemNotFoundException: Item '${energyUsageDaily}' could not be found in the item registry

I have checked that I do have energyUsageDaily Item and it has a value. Config of the Historic rule is

configuration:
  persistenceService: rrd4j
  energyUsageMonthly: ABB_A43_energyUsageMonthly
  energyCounter: ABB_A43_imp_energy
  energyUsageYearly: ABB_A43_energy_consumption_actual_year
  energyUsageDaily: ABB_A43_energyUsageDaily
  energyUsageWeekly: ABB_A43_energyUsageWeekly

One thing which I am still somewhat wondering. The Historic rule shows the Blockly block in the Edit script page but the Actual energy consumption rule doesn’t.

It would be nice to save e.g. the weekly, monthly an year values so that you can compare values for different periods. I guess I could do this by persisting the data to InfluxDB.

Somewhat another topic related to electricity consumption. Here in Finland we have so-called netting system (explained e.g. here). I can calculate the netted electricity hourly so these numbers are already deltavalues and you can’t calculate a deltavalue from these numbers. I haven’t looked at your code but I guess this netting could be included.

Anyway, your widget is really great.

I might have found the issue.
When creating the templates from my rules, I looked at an old example, which included wrong placeholders for the config params. I will check and correct that later on.
The second rule might have a missing dependency, will check that too and try to replace that or leave a link to what is missing.

I have updated both rule template in the marketplace after fixing the wrong placeholders.
After installation from the marketplace I could configure both rules through the UI and they are showing correct filled blockly blocks.
Please give it a try.

That’s exactly what the historical rule does. It runs every night at 00:00:01 and stores the following values:

  • If it is the first day of a year, it updates the yearly value
  • If it is the first day of a month, it updates the monthly value
  • If it is the first day of a week, it updates the weekly value
  • Daily value is updated every night and the last stored value represents yesterdays value.

You just need to persist those Items in your InfluxDB or whatever persistence you like.