Counter rollover…

I like to automate things… but sometimes it just doesn’t work out as planned. Today my power usage counter reached it’s maximum value and started counting from 0 again.

I knew this would happen some day, i thought i had it all covered in my Home Automation system, but it didn’t work out as planned; i had to fix some power usage records in the database today. The charts looked like complete garbage!

In my system i keep detailed information about every piece of hardware i use and how it is being processed. The hardware that i use for counting the Wh pulses contains a counter of 3 bytes, ranging from 0..FFFFFF hex (0..16777215 decimal); that’s 16777 kWh.

So what did i do a long time ago to prevent a counter rollover from giving me work that i don’t want to do? I created a ‘counterbits’ field in my database where i can store the number of bits a countervalue is based on. With this it’s easy to calculate the maximum counter value to expect. So far so good.

Whenever a countervalue is received with a smaller value then the previous one, the ‘counter rollover protection’ mechanism starts working.. it calculates the (additional) offset to be used based on the ‘counterbits’ field, adds this to the current value of the  ‘counteroffset’ field in the database and this new counteroffset value is added to the value that was received. No need to do anything; complete automatic counter rollover recovery.

But don’t mess up your database resulting in a counterbits value being set to zero!!!

Then it won’t work… so despite all effort, today i had to browse the tables with the historical data and fix some values… and wait another 2-3 years to enjoy having this automated…  🙁

Tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *