Actual 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 on every change of your energy meters counter value and updates items for today’s, this week’s/month’s/year’s/billing cycle’s energy consumption.

Language: JS Scripting

Changelog

Version 0.2.2

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

Version 0.2.1

  • fixed persistence bug for billing cycle value

Version 0.2

  • removed dependency from external blockly library
  • fixed more typos

Version 0.1.1

  • fixing some typos and incorrect placeholders

Version 0.1

  • initial release

Resources

uid: hmerk:actual_energy_consumption
label: Actual energy consumption
description: Updates Items holding information of actual 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: energyUsageToday
    type: TEXT
    context: item
    label: Energy consumption today
    description: Item that represents today's energy Usage
    required: true
  - name: energyUsageWeek
    type: TEXT
    context: item
    label: Energy consumption this week
    description: Item that represents this week's energy Usage
    required: true
  - name: energyUsageMonth
    type: TEXT
    context: item
    label: Energy consumption this month
    description: Item that represents this month's energy Usage
    required: true
  - name: energyUsageYear
    type: TEXT
    context: item
    label: Energy consumption this year
    description: Item that represents this year's energy Usage
    required: true
  - name: billingCycleStart
    type: TEXT
    context: item
    filterCriteria:
      - name: type
        value: DateTime
    label: Date of billing cycle start
    description: Item that holds the Date when the actual billing cycle started
    required: true
  - name: energyUsageBillingCycle
    type: TEXT
    context: item
    label: Energy billing cycle Item
    description: Item that represents the energy consumption for the actual billing cycle
    required: true
triggers:
  - id: "1"
    configuration:
      itemName: "{{energyCounter}}"
    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="-18" y="-712"><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="{{energyCounter}}" itemLabel="Energie
        Counter"></mutation><field
        name="itemName">{{energyCounter}}</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">{{persistenceService}}</field></block></value><value
        name="dayInfoSince"><block type="oh_zdt_amend"
        id="iD,W+1Cif+R+$PB{G9C4"><mutation items="3"></mutation><field
        name="operation">with</field><value name="baseZdt"><shadow type="oh_zdt"
        id=".}/;k[$7Zc)QZ#L7]?N^"><field
        name="day">2023-11-06</field></shadow><block type="oh_zdt_now"
        id="/1C:nps2x$!r8vhRP|j#"></block></value><value name="ADD0"><block
        type="oh_zdt_temporal_unit" id="8-|B:O|.DMk.L})K80LD"><mutation
        blockType="3"></mutation><field
        name="value">0</field></block></value><value name="ADD1"><block
        type="oh_zdt_temporal_unit" id="RKZtxH:+496e2$dQ!%8M"><mutation
        blockType="4"></mutation><field
        name="value">0</field></block></value><value name="ADD2"><block
        type="oh_zdt_temporal_unit" id="@5]mF~iAPG:(bp:p[G$W"><mutation
        blockType="5"></mutation><field
        name="value">0</field></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="{{energyUsageToday}}" itemLabel="Energieverbrauch
        Heute"></mutation><field
        name="itemName">{{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="{{energyCounter}}" itemLabel="Energie
        Counter"></mutation><field
        name="itemName">{{energyCounter}}</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">{{persistenceService}}</field></block></value><value
        name="dayInfoSince"><block type="oh_zdt_amend"
        id="2*j}a1;J+ahW).NAc-?_"><mutation items="3"></mutation><field
        name="operation">with</field><value name="baseZdt"><shadow type="oh_zdt"
        id=".}/;k[$7Zc)QZ#L7]?N^"><field
        name="day">2023-11-06</field></shadow><block type="oh_zdt_plusminus"
        id="(kYEPjaai#6;X6p|dxJ4"><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="(oeV|D]OsZCU.`m@)]1p"><field name="OP">MINUS</field><value
        name="A"><shadow type="math_number" id="Fx+Cu7o^MycxpT3jxx+."><field
        name="NUM">1</field></shadow><block type="oh_get_zdt_part"
        id="UZhn],`Suu4@iT2SSiQ!"><field
        name="temporalPart">getDayOfWeek</field><value name="zdt"><block
        type="oh_zdt_now"
        id="kC94|*Hu27vkg)had{QI"></block></value></block></value><value
        name="B"><shadow type="math_number" id=";|j$B7k0jnnHvH@b+~C?"><field
        name="NUM">1</field></shadow></value></block></value></block></value><value
        name="ADD0"><block type="oh_zdt_temporal_unit"
        id="7%H@giD-Uh9,|po3cj2A"><mutation blockType="3"></mutation><field
        name="value">0</field></block></value><value name="ADD1"><block
        type="oh_zdt_temporal_unit" id="dk:_xU=_y{Mk5ff;-[sF"><mutation
        blockType="4"></mutation><field
        name="value">0</field></block></value><value name="ADD2"><block
        type="oh_zdt_temporal_unit" id="}Py6_2W%?tn{lc||R#:D"><mutation
        blockType="5"></mutation><field
        name="value">0</field></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="{{energyUsageWeek}}" itemLabel="Energieverbrauch
        Woche"></mutation><field
        name="itemName">{{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="{{energyCounter}}" itemLabel="Energie
        Counter"></mutation><field
        name="itemName">{{energyCounter}}</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">{{persistenceService}}</field></block></value><value
        name="dayInfoSince"><block type="oh_zdt_amend"
        id="QkmniLh_:l9caOngKc%-"><mutation items="4"></mutation><field
        name="operation">with</field><value name="baseZdt"><shadow type="oh_zdt"
        id=".}/;k[$7Zc)QZ#L7]?N^"><field
        name="day">2023-11-06</field></shadow><block type="oh_zdt_now"
        id="Kb02Z)*92(+@lOWZOvh0"></block></value><value name="ADD0"><block
        type="oh_zdt_temporal_unit" id="H*pv,lD0=!UbXKZdp`[e"><mutation
        blockType="2"></mutation><field
        name="value">1</field></block></value><value name="ADD1"><block
        type="oh_zdt_temporal_unit" id="shEpFhjO1]LDVk{kWlv4"><mutation
        blockType="3"></mutation><field
        name="value">0</field></block></value><value name="ADD2"><block
        type="oh_zdt_temporal_unit" id="2Qm(5K)P]t|PFTNFDHtE"><mutation
        blockType="4"></mutation><field
        name="value">0</field></block></value><value name="ADD3"><block
        type="oh_zdt_temporal_unit" id="YPczNi4E!2m`Zm]0eEDm"><mutation
        blockType="5"></mutation><field
        name="value">0</field></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="{{energyUsageMonth}}" itemLabel="Energieverbrauch
        Monat"></mutation><field
        name="itemName">{{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="{{energyCounter}}" itemLabel="Energie
        Counter"></mutation><field
        name="itemName">{{energyCounter}}</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">{{persistenceService}}</field></block></value><value
        name="dayInfoSince"><block type="oh_zdt_amend"
        id="?[9oT3IE:*L+{@F~$(/i"><mutation items="5"></mutation><field
        name="operation">with</field><value name="baseZdt"><shadow type="oh_zdt"
        id=".}/;k[$7Zc)QZ#L7]?N^"><field
        name="day">2023-11-06</field></shadow><block type="oh_zdt_now"
        id="z|ebTzv5!aK$J=6?Qjk("></block></value><value name="ADD0"><block
        type="oh_zdt_temporal_unit" id="Em1+}@#N3RS)w1.2ZN!Z"><mutation
        blockType="1"></mutation><field
        name="value">1</field></block></value><value name="ADD1"><block
        type="oh_zdt_temporal_unit" id="(Lk8ACz!])CM0H[HZODa"><mutation
        blockType="2"></mutation><field
        name="value">1</field></block></value><value name="ADD2"><block
        type="oh_zdt_temporal_unit" id="Vp+el6m7t4$S#Z.m97XJ"><mutation
        blockType="3"></mutation><field
        name="value">0</field></block></value><value name="ADD3"><block
        type="oh_zdt_temporal_unit" id="Nmp.vDAE!1zJI-r!LW+."><mutation
        blockType="4"></mutation><field
        name="value">0</field></block></value><value name="ADD4"><block
        type="oh_zdt_temporal_unit" id="9,z(iAJd=|VL98uHq$_,"><mutation
        blockType="5"></mutation><field
        name="value">0</field></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="{{energyUsageYear}}" itemLabel="Energieverbrauch
        Jahr"></mutation><field
        name="itemName">{{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="{{energyCounter}}" itemLabel="Energie
        Counter"></mutation><field
        name="itemName">{{energyCounter}}</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">{{persistenceService}}</field></block></value><value
        name="dayInfoSince"><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="{{billingCycleStart}}" itemLabel="Start date ate of billing cycle"></mutation><field
        name="itemName">{{billingCycleStart}}</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="{{energyUsageBillingCycle}}"
        itemLabel="Energie usage billing cycle"></mutation><field
        name="itemName">{{energyUsageBillingCycle}}</field></block></value></block></next></block></next></block></next></block></next></block></xml>
      type: application/javascript
      script: >
        items.getItem('{{energyUsageToday}}').postUpdate((items.getItem('{{energyCounter}}').persistence.deltaSince((time.ZonedDateTime.now()).withHour(0).withMinute(0).withSecond(0),
        '{{persistenceService}}')));

        items.getItem('{{energyUsageWeek}}').postUpdate((items.getItem('{{energyCounter}}').persistence.deltaSince(time.ZonedDateTime.now().minusDays((((time.ZonedDateTime.now()).dayOfWeek().value()) - 1)).withHour(0).withMinute(0).withSecond(0), '{{persistenceService}}')));

        items.getItem('{{energyUsageMonth}}').postUpdate((items.getItem('{{energyCounter}}').persistence.deltaSince((time.ZonedDateTime.now()).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0), '{{persistenceService}}')));

        items.getItem('{{energyUsageYear}}').postUpdate((items.getItem('{{energyCounter}}').persistence.deltaSince((time.ZonedDateTime.now()).withMonth(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0), '{{persistenceService}}')));

        items.getItem('{{energyUsageBillingCycle}}').postUpdate((items.getItem('{{energyCounter}}').persistence.deltaSince(time.toZDT(items.getItem('{{energyUsageBillingCycle}}').state), '{{persistenceService}}')));
    type: script.ScriptAction
1 Like

I was curious how that rule would look like, so I have provided an image for everyone:

2 Likes