[Solved] Variable declaration Rules file question

I’m loving this stuff. Can anyone see any issue with the below? I tried declaring the variable and setting it in the first rule (to grab the KWH at the start of the new day - I’m crazy about knowing the amount of energy I’m using). Although It doesn’t seem to be working - if I declare it in the rule the other rule doesn’t see it - but I recall reading the variables in the rules files are global between the rules in the file right?

here is what I have. In the morning though - It goes all “NULL” on me (or at least seemed to when I check the log). So I set it up top and just look at my zwave log and set it manually but that isn’t ideal going forward.

Thanks for any pointers!


var Number kwh = zwave_device_dcac0cb6_node2_meter_kwh.state
var Number total
var Number dailyC
var Number dailyKWH
var Number initialKWH
var Number price = 0.111

rule "Calculate Daily Charge"
when
	Time cron "0 0 0 * * ?"
then
	initialKWH = kwh
end

rule "Calculate Monthly Charge"
when
    Item zwave_device_dcac0cb6_node2_meter_kwh received update
then 
    total = kwh * price
    dailyKWH = kwh - initialKWH
    dailyC = dailyKWH * price
    totalCost.postUpdate(total)
    dailyCost.postUpdate(dailyC)
    logInfo("RULE", "initialKWH: "+initialKWH)
    logInfo("RULE", "dailyKWH: "+dailyKWH)

end

Also - I realize I need to rename my rules - probably something like “get current KWH” and “Calculated costs”

“var Number kwh = zwave_device_dcac0cb6_node2_meter_kwh.state”

looks a bit fishy to me.

Maybe you could try to put it in a rule that is triggered by Startup, e.g.

var Number kph

rule "startup "
when
System started
then
kwh = zwave_device_dcac0cb6_node2_meter_kwh.state as DecimalType
end

var Number kwh = zwave_device_dcac0cb6_node2_meter_kwh.state

Outside a rule is definitely not working.
Even more, maybe a simple startup rule won’t work reliable, because there is no guarantee that the item is initialized yet. But there is no need for this var, so, instead of initializing this var (after item has changed from uninitialized), use the state itself in the rules:

var Number total
var Number dailyC
var Number dailyKWH
var Number initialKWH
var Number price = 0.111

rule "Calculate Daily Charge"
when
    Time cron "0 0 0 * * ?"
then
    initialKWH = zwave_device_dcac0cb6_node2_meter_kwh.state as DecimalType
end

rule "Calculate Monthly Charge"
when
    Item zwave_device_dcac0cb6_node2_meter_kwh received update
then 
    total = (zwave_device_dcac0cb6_node2_meter_kwh.state as DecimalType) * price
    dailyKWH = (zwave_device_dcac0cb6_node2_meter_kwh.state as DecimalType) - initialKWH
    dailyC = dailyKWH * price
    totalCost.postUpdate(total)
    dailyCost.postUpdate(dailyC)
    logInfo("RULE", "initialKWH: " + initialKWH.toString)
    logInfo("RULE", "dailyKWH: " + dailyKWH.toString)
end

You could even get one log line using this statement:

logInfo("RULE", "initialKWH: {}; dailyKWH: {}",initialKWH, dailyKWH)
1 Like

I actually wrote a lengthy post before realizing I only “glanced” over your actual suggestion. Foolish me!

I’m going to try you’re approach - and I’m going to reset my cron timing to happen sooner than having to wait until midnight (I’m so not that patient!)

Appreciate the help with the rule - I’ll report back!

@Udo_Hartmann

Brilliant!! Works a treat - I can’t believe I didn’t catch on earlier. Just instantiate them above, then set them below.
Probably using the wrong words - I’m just starting with development but all is working well.

Thanks again!