[ 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