How to count compressor cycles of my heating pump?

just yesterday I discovered, that my onewire counter for my gas meter nulled for some reason.
Then I remembered to have set the real counter for some reason two months ago (at the start of heating Season - as I’m lucky to only need gas from mid-November till Mid-February). But whats more important here:

  1. I have an old gas meter (not smart), which I count via an S0-reader
    (see here my original post: Gas consumption, adding the offset from the meter)
  1. it works kind of - for some reason the S0counter gets nulled, and therefore my Approach to tell the Offset in a static variable was lost
  2. So I changed the Approach - and as I use persistence, this one comes in Handy

I guess @Halloween, @Timo_Horn , you could adapt this one to your needs as well. This rule just counts the offset between the last two cycles and adds that one to the overall consumption counter.

in my script I use those items:

  • Sensoren_Leist_Gas => Actual Count of the S0-Reader (would be a compressor cycle)
  • Sensoren_Status_Gas => Overall consumption Counter (that one on my BK4 - would be your Counter)
    as my BK4 Counts 1/100 of a m3 in one count, I have to calculate this.

I also had some troubles casting the right types for calculating and item updates. What I found out was quite confusing: Sensoren_Leist_Gas.sendCommand(value) didn’t update the value, only postUpdate did. As this one is an item without a binding, I am confused. Second one was Sensoren_Status_Gas.postUpdate(Sensoren_Status_Gas.previousState().state) didn’t work, I had to use a variable for the update-command for the argument. Same for the overall consumption, I had to use a variable for this…

So here’s my code, looks a bit ugly and rough.

rule "Gaszähler"
	when
		Item Sensoren_Leist_Gas changed
	then
		if (Sensoren_Leist_Gas != UNDEF) {
			// calculate the Delta since the last update
			var Number newCounter = (Sensoren_Leist_Gas.state as Number - previousState as Number)
			
			// NULL abfragen
			if (newCounter < 0) { 
				// This was my concern, if the S0 was nulled, use just the counter (it would be 1)
				newCounter = Sensoren_Leist_Gas.state 
			}
					
			if (Sensoren_Status_Gas.state == NULL) {
				//  I experienced even with .restoreOnStartup the state wasn't restored - so do it manually
				var Number oldConsumption = Sensoren_Status_Gas.previousState().state
				Sensoren_Status_Gas.postUpdate(oldConsumption)
				Thread::sleep(10) // not sure, if needed, but somehow I suspected the script to be too fast sometimes
			}

			// now calculate the consumption and assign it to the counter
			var Number newConsumption = newCounter/100 
			newConsumption += Sensoren_Status_Gas.state
			Sensoren_Status_Gas.postUpdate(newConsumption)
		}
end

this one works for now! :wink: just wanted to share with you guys.

1 Like