SOLVED: Need some help with transferring values between rules and items

Hi,

I want to display the Gas Usage from a smart meter (DSMR binding). However: the gas usage only updates once an hour and remains constant in between (for example: now it’s 1.000 and in an hour it’s 1.500). The actual usage can only be calculated as a usage per hour.

Now I am struggling with this calculation. I want some rule, which holds the changed value (in this example 1.5 minus 1 = 0.5) and displays that until the next change or at a max of 1 hour).

The DSMR binding is working flawlessly. The issue is how to hold the 0.5 when every 10 seconds the value from the binding is updated. (simple calculation only show 0.5 for 10 seconds and then 0 the remaining hour).

My items:

String P1_Gas_Device_Type “Gasmeter Device Type [%s]” {dsmr=“gDeviceType”}
String P1_Gas_EquipmentId “Gasmeter Equipment ID [%s]” {dsmr=“gEquipmentId”}
String P1_Gas_Timestamp “Gasmeter last measurement timestamp [%s]” {dsmr=“gValueTS”}
Number P1_Gas_Actual_Usage “Gasverbruik meterstand [%s]” {dsmr=“gValue”}
Number P1_Gas_Actual_Usage_Tmin1 “Gasverbruik meterstand T-1 [%s]” {dsmr=“gValue2”}
Number P1_Gas_Used "Gasverbruik actueel [%.3f m3]"
Number P1_Gas_Used_Chart
Number P1_Gas_Last_Value "Laatste gasmeter stand [%.3f m3]"
Switch P1_Gas_Last_Value_Reset “Reset Last Gas Value”

Number P1_Gas_Counter_last_change “P1 Gas Counter Last Change”

The P1_Gas_Counter_last_change should be counting since the last change.

The rules:

rule “Reset Last Gas Value”
// resetting the Last Gas Value and the counter based on a switch in the sitemap. This to be used on startup or as a test.
when
Item P1_Gas_Last_Value_Reset changed
then
var Number CurrentGasValue
var int ResetGasCounter = 0
CurrentGasValue = P1_Gas_Actual_Usage.state
postUpdate (P1_Gas_Counter_last_change,ResetGasCounter)
postUpdate (P1_Gas_Last_Value,CurrentGasValue)

end

rule “Lengthen Gas Usage”
// when the actual usage changes, counter is being resetted as a base to lengthen the display time
when
Item P1_Gas_Actual_Usage changed
then
var Number P1GasActualUsage
var int ResetGasCounter = 0

P1GasActualUsage=P1_Gas_Actual_Usage.state
if (P1GasActualUsage > 0) {
	postUpdate(P1_Gas_Counter_last_change,ResetGasCounter)
	postUpdate(P1_Gas_Used,P1GasActualUsage)
							} 

logInfo("PV-GET",P1GasActualUsage)

end

rule "Maintain Gas Usage Counter"
when
// cron for every 5 minutes, reset counter after 14*5 minutes (70 minutes) max. Also clear the actual gas usage (if the usage would be 0 for a longer time, so there wouldn’t be any change noted.

Time cron “0 0/5 * * * ?”

then
var Number P1GasCounterLastChange
var Number ResetValue = 0
P1GasCounterLastChange=P1_Gas_Counter_last_change.state
P1GasCounterLastChange=P1GasCounterLastChange+1
postUpdate(P1_Gas_Counter_last_change, P1GasCounterLastChange)
if (P1GasCounterLastChange > 14) {
postUpdate(P1_Gas_Counter_last_change,ResetValue)
postUpdate(P1_Gas_Used,ResetValue)
}

end

Hope my aim is clear and somebody is able to help

So you want an item that shows the difference between the last meter reading and the meter reading before that?

Would using a changed trigger work? You could use the expire binding to reset the difference counter to 0 after 70 minutes of no changes:

Number P1_Gas_LastDifference "Last Gas Usage Difference [%.1f m3]" { expire="70m,0" }
rule "Update Gas Usage Counter"
when
  Item P1_Gas_Actual_Usage changed
then
  if (previousState instanceof DecimalType) {
    val difference = ((P1_Gas_Actual_Usage.state as DecimalType) - (previousState as DecimalType))
    postUpdate(P1_Gas_LastDifference, new DecimalType(difference))
  }
end

Hi John,

Thanks for your alternative, didn’t try it yet.

I changed a part of the setup, cleaned things up and… still didn’t work. However: a reboot made it work. Now it’s working fine (don’t know why, think both the changes and the reboot made it work).

Anyway: for the record, below is my working part.

in the items file:

String P1_Gas_Device_Type “Gasmeter Device Type [%s]” {dsmr=“gDeviceType”}
String P1_Gas_EquipmentId “Gasmeter Equipment ID [%s]” {dsmr=“gEquipmentId”}
String P1_Gas_Timestamp “Gasmeter last measurement timestamp [%s]” {dsmr=“gValueTS”}
Number P1_Gas_Actual_Usage “Gasverbruik meterstand [%s]” {dsmr=“gValue”}
Number P1_Gas_Actual_Usage_Tmin1 “Gasverbruik meterstand T-1 [%s]” {dsmr=“gValue2”}
Number P1_Gas_Used "Gasverbruik actueel [%.3f m3]"
Number P1_Gas_Used_Chart
Number P1_Gas_Last_Value "Laatste gasmeter stand [%.3f m3]"
Switch P1_Gas_Last_Value_Reset “Reset Last Gas Value”

Number P1_Gas_Counter_last_change "P1 Gas Counter Last Change [%s]"
Number P1_Gas_Used_Last_Hour “P1 Gas Used Last Hour [%.3f m3]”

and the rules file:
//
// kind of a reset when things are started for the very first time (afterwards: the persistence takes over
//
rule "Reset Last Gas Value"
when
Item P1_Gas_Last_Value_Reset changed
then
var Number CurrentGasValue
var int ResetGasCounter = 0
CurrentGasValue = P1_Gas_Actual_Usage.state

logInfo ("Reset Last Gas Value", "CurrentGasValue ={}", CurrentGasValue)

postUpdate (P1_Gas_Counter_last_change,ResetGasCounter)	
postUpdate (P1_Gas_Last_Value,CurrentGasValue)
postUpdate (P1_Gas_Used_Tmin1,CurrentGasValue)

end

rule “Lengthen Gas Usage”
//
// purpose is to calculate the usage of the last hour
// after a change of the actual usage
//
when
Item P1_Gas_Actual_Usage changed
then
var Number P1GasActualUsage
var int ResetGasCounter = 0
var Number P1GasUsedLastHour
var Number P1GasLastValue

P1GasActualUsage=P1_Gas_Actual_Usage.state
P1GasLastValue=P1_Gas_Last_Value.state

logInfo ("Lengthen Gas Usage", "P1GasActualUsage ={}", P1GasActualUsage)
logInfo ("Lengthen Gas Usage", "P1GasLastValue ={}", P1GasLastValue)

P1GasUsedLastHour=P1GasActualUsage-P1GasLastValue
P1GasLastValue=P1GasActualUsage

logInfo ("Lengthen Gas Usage", "P1GasUsedLastHour ={}", P1GasUsedLastHour)
logInfo ("Lengthen Gas Usage", "New P1GasLastValue ={}", P1GasLastValue)

postUpdate(P1_Gas_Counter_last_change, ResetGasCounter)
postUpdate(P1_Gas_Last_Value, P1GasLastValue)
postUpdate(P1_Gas_Used_Last_Hour, P1GasUsedLastHour)

end

rule "Maintain Gas Usage Counter"
when
// cron for every 5 minutes
Time cron “0 0/5 * * * ?”

then
var Number P1GasCounterLastChange
var Number ResetValue = 0
var Number P1GasLastValue
var Number P1GasActualUsage
var Number P1GasUsedLastHour

P1GasCounterLastChange=P1_Gas_Counter_last_change.state
logInfo ("Maintain Gas Usage Counter", "P1GasCounterLastChange ={}", P1GasCounterLastChange)
P1GasCounterLastChange=P1GasCounterLastChange+1	
logInfo ("Maintain Gas Usage Counter", "New P1GasCounterLastChange ={}", P1GasCounterLastChange)
P1GasActualUsage=P1_Gas_Actual_Usage.state
logInfo ("Maintain Gas Usage Counter", "P1GasActualUsage ={}", P1GasActualUsage)
P1GasLastValue=P1_Gas_Last_Value.state
logInfo ("Maintain Gas Usage Counter", "P1GasLastValue ={}", P1GasLastValue)
P1GasUsedLastHour=P1_Gas_Used_Last_Hour.state
logInfo ("Maintain Gas Usage Counter", "P1GasUsedLastHour ={}", P1GasUsedLastHour)

postUpdate(P1_Gas_Counter_last_change, P1GasCounterLastChange)
if (P1GasCounterLastChange > 14) {
	postUpdate(P1_Gas_Counter_last_change,ResetValue)
	P1GasUsedLastHour=ResetValue
										} 

postUpdate(P1_Gas_Used_Last_Hour,P1GasUsedLastHour)
logInfo ("Maintain Gas Usage Counter", "Finally: P1GasUsedLastHour ={}", P1GasUsedLastHour)

end

part of the persistence file:

P1_Gas_Used,P1_Gas_Used_Last_Hour,P1_Gas_Used_Tmin1, P1_Gas_Counter_last_change : strategy = everyMinute, restoreOnStartup 

Many thanks!

Best regards (and a happy Christmas and best wishes for 2017)