How would you write a rule that sends me a estimated power bill in pounds every month on the 20th
I already have an item that records home power usage in KWH
Item "powerusage" changed from 116.400 to 116.401
rule "Bill Estimate Notification"
when
Time cron "0 0 12 20 1/1 ? *" //date 20th every month
then
powerusage.deltaBetween(NOW, 20thlastmonth)
* 0.32 = #powerusagecost
sendNotification("emailaddress", "estimated power usage is #"Item Delta" KWH this has a cost of #"powerusagecost")
end
Calculates the current bill estimate as we go (UI Rules using JS Scripting). I like to keep a running total rather than just get alerted at the end of the month. So I can see at any time how much power we’ve used.
configuration: {}
triggers:
- id: "1"
configuration:
itemName: HomeEnergyMeter_ElectricmeterkWh
type: core.ItemStateChangeTrigger
- id: "2"
configuration:
itemName: HomeEnergyMeter_Access
type: core.ItemStateChangeTrigger
- id: "3"
configuration:
itemName: HomeEnergyMeter_Rate
type: core.ItemStateChangeTrigger
conditions: []
actions:
- inputs: {}
id: "4"
label: Calculate the power bill and update the Item
configuration:
type: application/javascript;version=ECMAScript-2021
script: >
var logger = log('Power Bill Estimate');
var curr = items.getItem('HomeEnergyMeter_ElectricmeterkWh').rawState;
var rate = items.getItem('HomeEnergyMeter_Rate').rawState;
var access = items.getItem('HomeEnergyMeter_Access').rawState;
var estimate = (curr * rate) + access;
items.getItem('HomeEnergyMeter_CurrentBillEstimate').postUpdate(estimate.toString());
logger.debug('Calculated bill = ${}', estimate);
type: script.ScriptAction
Save the estimate for last month and reset the calculation for the new month (here is where you’d send your notification)
configuration: {}
triggers:
- id: "1"
configuration:
cronExpression: 0 0 0 9 * ? *
type: timer.GenericCronTrigger
conditions: []
actions:
- inputs: {}
id: "3"
configuration:
type: application/javascript;version=ECMAScript-2021
script: >
var logger = log('Reset Power Estimate');
logger.info("It's the first day of the electricity billing period, time to reset the running total.");
items.getItem('HomeEnergyMeter_LastMonthBillEstimate').postUpdate(items.getItem('HomeEnergyMeter_CurrentBillEstimate').state);
items.getItem('HomeEnergyMeter_ResetMeter').sendCommand('ON');
items.getItem('HomeEnergyMeter_CurrentBillEstimate').postUpdate(items.getItem('HomeEnergyMeter_Access').state);
type: script.ScriptAction
Hi again @rlkoshak thanks for the reply it was helpful I spent a lot of time trying too get what you sent me working but had issues it will have been something I was doing wrong but anyway I managed too get something else working posting for the community
rule "Electric Bill Notification & Calculations & Estimates"
when
Time cron "0 0 8 20 1/1 ? *" // 20th every month @ 8AM
then
HomeEnergyMeter_CurrentBillEstimate.postUpdate((HouseMQTTPowerMonitor_powermeterTotalEnergy.state as DecimalType) - PowerUsageLastMonth.state as DecimalType) // calculate this months usage in KWH
Thread::sleep(1500)
ElectricBillPrice.postUpdate(((HomeEnergyMeter_CurrentBillEstimate.state as Number) * 0.32)) // multiply the above value by the price per unit
Thread::sleep(1500)
sendNotification("email@email", "Bill Estimate Your Electric Usage for this month is " + HomeEnergyMeter_CurrentBillEstimate.state + "KWH this has cost £" + ElectricBillPrice.state ) //send notification containing these two above value
Thread::sleep(1500)
PowerUsageLastMonth.postUpdate((HouseMQTTPowerMonitor_powermeterTotalEnergy.state)) // set this months reading
end