Waterflow to a item

I want to measure waterflow. I have a reading of the watermeter every 15 sec. I want to make the difference of these to readings into a items. How do I do that.

Please post your Items and your first attempt and writing a Rule to do this.

It’s not that we don’t want to help, but we are also not here to do it for you. We are here to teach you how to do it and help you with problems. But the bulk of the work needs to come from you.

If you have specific questions about an approach we will be happy to help answer specific questions.

At a high level, you will want a Rule that triggers when the watermeter Item changes. In that Rule you will do your calculations and update your other Items.

3 Likes

My item is water_volume and it updates every 15 sec. I would like to make a new item (waterflow) where the difference between water_volume and the updated water_volume is.

If I knew how to write the rules I would send the first attempt

ā€œI want to fly to the moonā€
ā€œOkay, first take your rocket ā€¦ā€
ā€œWhat’s a rocket?ā€

At the moment, it is difficult to help. If we wrote a rule, would you know what to do with it? i.e. are you able to use one of the ways to include rules in your system - using an editor and saving in a file.

You say ā€œI would like to make a new itemā€ - okay, can you do that or do you need help with that part?

ā€œMy item is water_volumeā€ - okay, what type of Item is it? Can we see your definition? This makes a difference to how to use it in a rule, Rick didn’t ask for more detail just to be awkward.

Maybe you could sketch out how you think your rule should work. What would start it off? What it would it do, or calculate? What would it do with the result?

Can you roughly fit those ideas into this structure

1 Like

Thank you for your answer. I hope you know that it can be very difficult to explain yourself when english is not your best language.

At the moment, it is difficult to help. If we wrote a rule, would you know what to do with it? i.e. are you able to use one of the ways to include rules in your system - using an editor and saving in a file.

You say ā€œI would like to make a new itemā€ - okay, can you do that or do you need help with that part?

I know how to use the rule and I know how to make an item.

ā€œMy item is water_volumeā€ - okay, what type of Item is it? Can we see your definition? This makes a difference to how to use it in a rule, Rick didn’t ask for more detail just to be awkward.

Water_volume is a number like this 312.938 m3

The water_volume updates every 15. sec and I would like the rule to calculate the waterflow so I can see how many liters of water I have been using in a minut

I have tried this rule but it gives me this error : Rule ā€˜Meter_update’: Could not cast NULL

rule "Meter_update"
 when
 	Item Water_volume received update
 then
 	val Number newMeter = Water_volume.state as DecimalType
 	switch(updmeter.state as DecimalType) {
 		case 1: {
 			val oldMeter = wtrMeter.state as DecimalType
			val oldDate = new DateTime((wtrMeterDate.state as DateTimeType).calendar.timeInMillis)
			val newDate = new DateTime(now())
			val double daymsec= 86400000
			var double daydiff = (newDate.millis - oldDate.millis)/daymsec
			// if(daydiff==0) {daydiff=1}
			wtrUsedI.postUpdate((newMeter-oldMeter)/daydiff)
			wtrUsedD.postUpdate((newMeter-oldMeter)/daydiff)
			wtrMeter.postUpdate(newMeter)
			wtrMeterD.postUpdate(newMeter)
			wtrMeterDate.postUpdate(new DateTimeType(newDate.toCalendar(null)))
 			logInfo("Meters","Updated water meter")
 		}	
 	}
end

There’s still secrets about your Item type. Is this a ā€œplainā€ Number type? Or is it dimensioned, a Number:Volume type? It makes a difference how you handle it in rules.

Okay, let’s play.
The message is telling you that something in your rule can’t deal with a value NULL.
NULL (not the same as null) is what Item states get set to when Items are created at bootup. So we’re talking about an Item that hasn’t yet been updated by binding or rule.

We can guess this is to do with the Item(s) your new rule populates - if it’s never run before, they’ll be NULL.
You can’t extract a sensible numeric value from NULL, so that will be the part that fails.
So you’d need to change the rule to test for state NULL before extracting the numeric value, and do something else if it is NULL.

Just as a general comment on numeric values, it’s usually best to use ā€œas Numberā€ in rules (rather than DecimalType or double) as it’s the closest thing to a ā€œnativeā€ format. There are exceptions.

var Number oldMeter = 0
if (wtrMeter.state != NULL {
     oldMeter = wtrMeter.state as Number
}

You’ll need to deal with the ā€œold readingā€ timestamp being NULL as well - perhaps skip the calculations and just store the latest reading. You can’t calculate flow rate without the start reading/timestamp.

I’m not sure what the switch/case part is about, that doesn’t seem to be needed for what you asked. Maybe get it working first before adding complications.