Historical energy consumption [4.0.0.0;4.9.9.9]

[:crayon: Add a primary screenshot or a logo here. The first image of the post will be promoted seen in the add-on list in the UI.]

This rule runs every midnight and updates items for daily, weekly, monthly and yearly energy consumption.

Language: JS Scripting

Changelog

Version 0.1.3

  • fixed persistence bug replaced history.deltaSince with persistence.deltaSince

Version 0.1.2

  • fixed more typos

Version 0.1.1

  • fix some typos and wrong placeholders

Version 0.1

  • initial release

Resources

uid: hmerk:historical_energy_consumption
label: Historical energy consumption
description: Updates Items holding information of historical energy consumption
configDescriptions:
  - name: persistenceService
    type: TEXT
    context: text
    label: Persistence Service
    description: Persistence service used to store information
    limitToOptions: true
    options:
      - label: default
        value: default
      - label: RRD4J
        value: rrd4j
      - label: MapDB
        value: mapdb
    required: true
  - name: energyCounter
    type: TEXT
    context: item
    label: Energy Counter Item
    description: Item that represents the energy counter value
    required: true
  - name: energyUsageDaily
    type: TEXT
    context: item
    label: Daily energy consumption Item
    description: Item that represents daily energy consumption
    required: true
  - name: energyUsageWeekly
    type: TEXT
    context: item
    label: Weekly energy consumption Item
    description: Item that represents weekly energy consumption
    required: true
  - name: energyUsageMonthly
    type: TEXT
    context: item
    label: Energy consumption this month
    description: Item that represents monthly energy consumption
    required: true
  - name: energyUsageYearly
    type: TEXT
    context: item
    label: Energy consumption this year
    description: Item that represents yearly energy consumption
    required: true
triggers:
  - id: "1"
    configuration:
      cronExpression: 1 0 0 * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
        type="oh_event" id="DcoZNq7swfEZoj)0Ua]X" x="-820" y="-416"><field
        name="eventType">postUpdate</field><value name="value"><shadow
        type="text" id="#(yQc8CV:OKPW@[kDmLq"><field
        name="TEXT">value</field></shadow><block type="oh_get_persistvalue"
        id="QcdG@s#+KDDo52O!ai.+"><field
        name="methodName">deltaSince</field><value name="itemName"><shadow
        type="oh_item" id="Py}?{~?.icb-QZU-}e|%"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="oh_item"
        id="{c|-MK@dbovJXw#OdoU%"><mutation
        itemName="{{energyCounter}}" itemLabel="Energie
        Counter"></mutation><field
        name="itemName">{{energyCounter}}</field></block></value><value
        name="persistenceName"><shadow type="oh_persistence_dropdown"
        id="a|^dX_MtavSf1=2cxeGW"><field
        name="persistence">default</field></shadow><block
        type="oh_persistence_dropdown" id="u.6YT}kCv9:!jJLX)Ha|"><field
        name="persistence">rrd4j</field></block></value><value
        name="dayInfoSince"><block type="oh_zdt_plusminus"
        id="[bDW~ua-t]1?8nutF9y!"><field name="plusminus">minus</field><field
        name="period">Days</field><value name="offset"><shadow
        type="math_number" id="{sVjuwBlf6dS!(gW*BYk"><field
        name="NUM">0</field></shadow><block type="math_number"
        id="WSiMw);T_W7KBh@4-u_L"><field
        name="NUM">1</field></block></value></block></value></block></value><value
        name="itemName"><shadow type="oh_item"
        id="NPb)7;I@d$=vPq^*MJ)`"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="oh_item"
        id="a8-G;5w)iG|(X+LUPFbs"><mutation
        itemName="{{energyUsageDaily}}" itemLabel="Daily energy 
        consumption"></mutation><field
        name="itemName">{{energyUsageDaily}}</field></block></value><next><block
        type="controls_if" id="0?4$NID6H0C9A#:}wuQ!"><value name="IF0"><block
        type="logic_compare" id="U6pYz7%x*I#a`^Y!pGY~"><field
        name="OP">EQ</field><value name="A"><block type="oh_get_zdt_part"
        id="RR$AD9u6D!yHL3_n!WXk"><field
        name="temporalPart">getDayOfWeek</field><value name="zdt"><block
        type="oh_zdt_now"
        id="nYo=K+nRS]V%g#,Pi;t/"></block></value></block></value><value
        name="B"><block type="math_number" id="9%MBAcAkS:LmTC52RXu*"><field
        name="NUM">1</field></block></value></block></value><statement
        name="DO0"><block type="oh_event" id="{8[5EibFv*n^xlsfU_9r"><field
        name="eventType">postUpdate</field><value name="value"><shadow
        type="text" id="#(yQc8CV:OKPW@[kDmLq"><field
        name="TEXT">value</field></shadow><block type="oh_get_persistvalue"
        id="%EWS?a0ed.^2W%u0r/4T"><field
        name="methodName">deltaSince</field><value name="itemName"><shadow
        type="oh_item" id="4~Z=Bm{5152mLS;7$5k}"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="oh_item"
        id="|D|At2K+fPXe#|m]z}MD"><mutation
        itemName="{{energyCounter}}" itemLabel="Energie
        Gesamt"></mutation><field
        name="itemName">{{energyCounter}}</field></block></value><value
        name="persistenceName"><shadow type="oh_persistence_dropdown"
        id="a|^dX_MtavSf1=2cxeGW"><field
        name="persistence">default</field></shadow><block
        type="oh_persistence_dropdown" id="O_c{pjiLi0O6(c+/5ug5"><field
        name="persistence">rrd4j</field></block></value><value
        name="dayInfoSince"><block type="oh_zdt_plusminus"
        id="y[1(nPz]4`Nk7Kq#pVMS"><field name="plusminus">minus</field><field
        name="period">Weeks</field><value name="offset"><shadow
        type="math_number" id="%ENY;4,LF9~rYdncN*J."><field
        name="NUM">0</field></shadow><block type="math_number"
        id="n};3a{3CvDCE)8$V]V9%"><field
        name="NUM">1</field></block></value></block></value></block></value><value
        name="itemName"><shadow type="oh_item"
        id="#fQEeLvX$:+Ok0s-_ndA"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="oh_item"
        id="4TyCQX|zx,QZlhsj[R4d"><mutation
        itemName="{{energyUsageWeekly}}" itemLabel="Weekly energy 
        consumption"></mutation><field
        name="itemName">{{energyUsageWeekly}}</field></block></value></block></statement><next><block
        type="controls_if" id="|NWnM*J!|AH9]Zfa[w-m"><value name="IF0"><block
        type="logic_compare" id="[E}0z@95rr_-(^9a##Iq"><field
        name="OP">EQ</field><value name="A"><block type="oh_get_zdt_part"
        id="(hY?hpqJyp,-~lbvVgB]"><field
        name="temporalPart">getDayOfMonth</field><value name="zdt"><block
        type="oh_zdt_now"
        id="(Dm;y)NZo`dU-VwwNI,q"></block></value></block></value><value
        name="B"><block type="math_number" id="i{%ezg~{d;[v`o^#g[L,"><field
        name="NUM">1</field></block></value></block></value><statement
        name="DO0"><block type="oh_event" id=",%4=aUR`Dp;lZk[vS=iI"><field
        name="eventType">postUpdate</field><value name="value"><shadow
        type="text" id="#(yQc8CV:OKPW@[kDmLq"><field
        name="TEXT">value</field></shadow><block type="oh_get_persistvalue"
        id="*}wUT?2YIi{%UWLFF)R["><field
        name="methodName">deltaSince</field><value name="itemName"><shadow
        type="oh_item" id="E16,H4)4DqzApG}4|Mrm"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="oh_item"
        id="1u;KZKQ;4ytz0hKJT;/f"><mutation
        itemName="{{energyCounter}}" itemLabel="Energie
        Gesamt"></mutation><field
        name="itemName">{{energyCounter}}</field></block></value><value
        name="persistenceName"><shadow type="oh_persistence_dropdown"
        id="a|^dX_MtavSf1=2cxeGW"><field
        name="persistence">default</field></shadow><block
        type="oh_persistence_dropdown" id="`w~opwkei^7CIB3FrO+."><field
        name="persistence">rrd4j</field></block></value><value
        name="dayInfoSince"><block type="oh_zdt_plusminus"
        id="Z?~MSD!-em3!8FSpdz8c"><field name="plusminus">minus</field><field
        name="period">Months</field><value name="offset"><shadow
        type="math_number" id="#+a!N7#(|H6,-X8@ROJU"><field
        name="NUM">0</field></shadow><block type="math_number"
        id=")^WuCM:FMVt1,K*X5STB"><field
        name="NUM">1</field></block></value></block></value></block></value><value
        name="itemName"><shadow type="oh_item"
        id="u9c,ktf{y}x6}w%8#jj~"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="oh_item"
        id="Sc9a}rvsYblGZ4/n@Rs]"><mutation
        itemName="{{energyUsageMonthly}}" itemLabel="Monthly energy 
        consumption"></mutation><field
        name="itemName">{{energyUsageMonthly}}</field></block></value></block></statement><next><block
        type="controls_if" id="Z|(jQNM~N;sjEs_9,OD,"><value name="IF0"><block
        type="logic_compare" id="~BC$sP`2wv49=P@wbIQl"><field
        name="OP">EQ</field><value name="A"><block type="oh_get_zdt_part"
        id="%X4Q/;r!xG,lPkC3i,~E"><field
        name="temporalPart">getDayOfYear</field><value name="zdt"><block
        type="oh_zdt_now"
        id="1/mF/jsRwX7L}-V/eu8#"></block></value></block></value><value
        name="B"><block type="math_number" id="C8-sV|{33~aWV%c8.]5|"><field
        name="NUM">1</field></block></value></block></value><statement
        name="DO0"><block type="oh_event" id="ThuO;#wF9bwNr`GCc#zC"><field
        name="eventType">postUpdate</field><value name="value"><shadow
        type="text" id="#(yQc8CV:OKPW@[kDmLq"><field
        name="TEXT">value</field></shadow><block type="oh_get_persistvalue"
        id="e!g~$Qww)$==?dC|Ph?."><field
        name="methodName">deltaSince</field><value name="itemName"><shadow
        type="oh_item" id="E16,H4)4DqzApG}4|Mrm"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="oh_item"
        id="#($cP7M_#{wMbxt.#k[b"><mutation
        itemName="{{energyCounter}}" itemLabel="Energie
        Gesamt"></mutation><field
        name="itemName">{{energyCounter}}</field></block></value><value
        name="persistenceName"><shadow type="oh_persistence_dropdown"
        id="a|^dX_MtavSf1=2cxeGW"><field
        name="persistence">default</field></shadow><block
        type="oh_persistence_dropdown" id="3A$FKU5,%6(lF$U]q[Sr"><field
        name="persistence">rrd4j</field></block></value><value
        name="dayInfoSince"><block type="oh_zdt_plusminus"
        id="Sy2=fJPU]:hwbCwD+B|Y"><field name="plusminus">minus</field><field
        name="period">Years</field><value name="offset"><shadow
        type="math_number" id="#+a!N7#(|H6,-X8@ROJU"><field
        name="NUM">0</field></shadow><block type="math_number"
        id="?n62/(a806h70QpkWW+2"><field
        name="NUM">1</field></block></value></block></value></block></value><value
        name="itemName"><shadow type="oh_item"
        id="u9c,ktf{y}x6}w%8#jj~"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="oh_item"
        id="?(##OvabvK76gTfk7(Q2"><mutation
        itemName="{{energyUsageYearly}}" itemLabel="Yearly energy 
        consumption"></mutation><field
        name="itemName">{{energyUsageYearly}}</field></block></value></block></statement></block></next></block></next></block></next></block></xml>
      type: application/javascript
      script: >
        items.getItem('{{energyUsageDaily}}').postUpdate((items.getItem('{{energyCounter}}').persistence.deltaSince(time.ZonedDateTime.now().minusDays(1),
        '{{persistenceService}}')));

        if (((time.ZonedDateTime.now()).dayOfWeek().value()) == 1) {
          items.getItem('{{energyUsageWeekly}}').postUpdate((items.getItem('{{energyCounter}}').persistence.deltaSince(time.ZonedDateTime.now().minusWeeks(1), '{{persistenceService}}')));
        }

        if (((time.ZonedDateTime.now()).dayOfMonth()) == 1) {
          items.getItem('{{energyUsageMonthly}}').postUpdate((items.getItem('{{energyCounter}}').persistence.deltaSince(time.ZonedDateTime.now().minusMonths(1), '{{persistenceService}}')));
        }

        if (((time.ZonedDateTime.now()).dayOfYear()) == 1) {
          items.getItem('{{energyUsageYearly}}').postUpdate((items.getItem('{{energyCounter}}').persistence.deltaSince(time.ZonedDateTime.now().minusYears(1), '{{persistenceService}}')));
        }
    type: script.ScriptAction
3 Likes

This is cool, thanks! I just set it up, now I have to wait till tomorrow to see if it works!

1 Like

I am new to the UI, I must be doing something wrong. I added the template, created a new rule using the template, added my persistence and items, but it never brings in the rest of the template.

configuration:
  energyCounter: Circuit_Datacenter_kWh
  energyUsageDaily: Datacenter_kWh_Daily
  energyUsageWeekly: Datacenter_kWh_Weekly
  persistenceService: rrd4j
  energyUsageMonthly: Datacenter_kWh_Monthly
triggers: []
conditions: []
actions: []

Ya, that is odd, I tried several times, but if I paste it in it stays, but I get:

2024-10-31 09:46:15.915 [WARN ] [rnal.defaultscope.ScriptBusEventImpl] - State '[object Object]' cannot be parsed for item 'Datacenter_kWh_Daily'.

Hm, will try to reproduce later…

That was easy, you did not setup ALL items:

 - name: energyUsageYearly
    type: TEXT
    context: item
    label: Energy consumption this year
    description: Item that represents yearly energy consumption
    required: true

This one is required but missing in your config. Unfortunately, there is no message thrown by the template paser, but it leads to an empty rule.

Yep, found that, still getting this error:

2024-10-31 11:35:40.391 [WARN ] [rnal.defaultscope.ScriptBusEventImpl] - State '[object Object]' cannot be parsed for item 'Datacenter_kWh_Daily'.

How is that item defined ?

Number    Today_Datacenter_kWh                "Datacenter kWh Today [%.1f kWh]"                       <energy>	      (jdbc_night, jdbc_change)                                                     
Number    Datacenter_kWh_Daily                "Datacenter Daily kWh [%.1f kWh]"                       <energy>                                                    
Number    Datacenter_kWh_Weekly               "Datacenter Weekly kWh [%.1f kWh]"                      <energy>
Number    Datacenter_kWh_Monthly              "Datacenter Monthly kWh [%.1f kWh]"                     <energy>                                                    
Number    Datacenter_kWh_Yearly               "Datacenter Yearly kWh [%.1f kWh]"                      <energy>

Ah, they need to be text? Why not number?

Nope, mine are definned as

Number:Energy

Got ya, I changed to Number:Energy and still getting the object Object error.

Can you please post a screenshot of the blockly part.
Mine is looking like this and working:

BTW, what openHAB version do you use ?

Running 4.2.2, the blockly is cool, never seen that before.

What is the value Today_Datacenter_kWh ? This rule is triggered at 1 second past midnight, so I would expect a zero value, which seems not to work correct.

The Rule template asks for " Item that represents the energy counter value", which will never be 0.

@rlkoshak Is my assumption correct here ?

rrd4j doesn’t like it when you query too far past the beginning of the data. You’ll get null from the persistence actions.

I’d assume this is a relatively new Item and doesn’t have data going back that far and the get historic data call is returning null and you cant’ postUpdate null.

It’s an easy test though. Just log out what each call is returning. If my hunch is right, your rule template might need to get a bit more complex and test for null before posting the update.

@sipvoip, if you:

  1. alt-shift-d to bring up the developer sidebar
  2. Click on the third tab
  3. “Open Scratchpad” (this will open or create then open a Script you can use for experiments with rules code, if it doesn’t already exist it will ask to choose a langauge, I’ll assume JS but can post Blockly if desired, Blockly is indeed pretty cool)
  4. Assuming you chose ECMAScript paste in the following
var i = items.Today_Datacenter_kWh;
console.info("Day:   " + i.persistence.persistedState(time.toZDT("P-1D"));
console.info("Week:  " + i.persistence.persistedState(time.toZDT("P-1W"));
console.info("Month: " + i.persistence.persistedState(time.toZDT("P-1M"));
console.info("Year:  " + i.persistence.persistedState(time.toZDT("P-1Y"));
  1. Click “save” or hit ctrl-s
  2. Click “run” or hit ctrl-r
  3. Look at openhab.log for th results. If you see null for any of these that’s going to be the problem. If not, something else is up.

You can get back to the scratchpad script from the developer sidebar or from Settings → Scripts → -Scratchpad-. To change the language used you can delete it and recreate it from the developer sidebar and choose a different language.

1 Like

It’s not 0, it is the up to the minute current kWh total reading, so right now its: 44.43023

Ok, then your item naming was irritating.
Is there anything else in the logs?

You could try to create a simpe blockly rule that does nothing else than getting the delta from that item since a specific date.

How do I reset them with a rule? I have tried:

OMWellPumpReset.postUpdate(ON)
postUpdate(OMWellPumpResetm,ON)

then back off after 2 seconds, does not reset, only if I click the reset in the UI.

Needed to use sendCommand, got it.