Calculate consumption rainwater

Watch out for a slow leak type situation. It’s not clear but for 2, you need to run if the difference is bigger than 0.05 from the last processed reading, not just the last reading. For example, you receive 50 readings that go down only by 0.04 between each, your tank will be empty and your rule will never have run.

I recently published a rule template to the Marketplace that might make a good starting point for something like this (templates are only available in the UI though). To do so you’d install the template and instantiate a new rule from it. Trigger the rule using Waterput1a. You’ll need another Item to store the totals for the second property when installing the rule, we’ll call that Waterput1aSum.

But this rule assumes it’s always counting up and treats new readings that are lower than the last as the sensor having been reset so we need to customize it some.

  1. Add a new Item to store the last processed reading, we’ll call that Waterput1aLastProcessed

  2. Add a new condition (Rules DSL) with code along these lines:

val curr = newState as Number
val lastState = previousState as Number
val lastProcessed = Waterput1aLastProcessed.state as Number
val delta = lastProcessed - curr

curr < lastState && delta >= 0.05

That will only allow the rule to run if the current reading is less than the last reading and the current reading is more than 0.05 less than the last processed reading (which avoids the slow leak problem).

  1. The calculation in the rule’s action assumes the reading is always counting up but in this case we are always counting down. So we need to make one minor change to the code. Change the following line:
var delta = (reading < lastReading) ? reading : (reading - lastReading);

To

var delta = Math.abs(reading - lastReading);

That will make the total increase as the readings decrease. We don’t have to care about seeing is reading is greater than the last reading because that’s now handled in the condition.

  1. We need to record the Item as having been processed. Add a new Script Action (if you want to stick to Rules DSL) with the following line.
Waterput1aLastProcessed.postUpdate(newState)

That should do it. If you want you could instead add the following line of ECMAScript to the existing action.

items.getItem('Waterput1aLastProcessed').postUpdate(newState);

Even if you don’t use the rule template, this should give you enough of an example to work from.

1 Like