Tuesday, October 13, 2015

Kamstrup meter software update

On September 29th around 1:45 at night, it seems that a software update has been performed on our Kamstrup smartmeter, triggered remotely.

The noticeable result is that the P1 telegrams have different (less) content. And since my monitoring script checks for an expected filesize, all telegrams were rejected and discarded that night. Fortunately only irrelevant/unused fields were removed, so after tweaking the expected filesize next morning everything worked just fine again.

Before:After:
/KMP5 KA6U001660000000

0-0:96.1.1(204B413655303031363630000000000000)
1-0:1.8.1(02576.028*kWh)
1-0:1.8.2(01115.025*kWh)
1-0:2.8.1(00453.811*kWh)
1-0:2.8.2(01129.910*kWh)
0-0:96.14.0(0001)
1-0:1.7.0(0000.19*kW)
1-0:2.7.0(0000.00*kW)
0-0:17.0.0(999*A)
0-0:96.3.10(1)
0-0:96.13.1()
0-0:96.13.0()
0-1:24.1.0(3)
0-1:96.1.0(3238303131303031333035000000000000)
0-1:24.3.0(150929010000)(08)(60)(1)(0-1:24.2.1)(m3)
(01850.034)
0-1:24.4.0(1)
!
/KMP5 KA6U001660000000

0-0:96.1.1(204B413655303031363630000000000000)
1-0:1.8.1(02577.074*kWh)
1-0:1.8.2(01115.202*kWh)
1-0:2.8.1(00453.811*kWh)
1-0:2.8.2(01137.015*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(0000.00*kW)
1-0:2.7.0(0002.68*kW)
0-0:96.13.1()
0-0:96.13.0()
0-1:96.1.0(3238303131303031333035000000000000)
0-1:24.3.0(150929110000)(08)(60)(1)(0-1:24.2.1)(m3)
(01850.050)
!

This filesize validation was mostly in place to detect a partial telegram due to some glitch in our serial communication. Now it's time to turn this validation into something more robust: we don't want to reject valid and complete telegrams when they ever decide to remove some other obscure field, or even add a new field. Especially since there's such a clean format requirement available: the first line must start with a '/' and the last line must end with a '!', and these symbols are unused in the main body of the telegram.

So instead of the filesize check in mbsolget_p1.sh we could use something like this:

...
  #Report unexpected filesize
  if [ $filesize = $FSIZE ]; then
    log "Received successfully"
  else
    log "Warning: Unexpected filesize!"
  fi

  telegram=$(cat $WORKDIR/p1_temp.log)

  if [[ $(expr match "$telegram" '/.*!') > 0 ]]; then

    #Process $WORKDIR/p1_temp.log to p1_daily.tmp
    MESSAGE=`echo "$telegram" | awk -f $AWKSCR > $STORE`
...

Edit:
On october 14th around 11:50 I started seeing 'Unexpected filesize' warnings in the logs - the P1 telegram format change mentioned above had been reverted for some reason. The script kept happily parsing and processing the telegrams, which means that the above tweak has already been useful and works as intended. At the same time I'm unsure what's really going on; why does the telegram content change so frequently all of a sudden? I'll keep an eye the logs to see if and when it changes again.

Edit 2:
On december 11th around 8:35 the above-mentioned fields have been removed from the P1 telegram again. Also, I noticed a small error in the validation check; it should have used double brackets in order to allow the greater-than comparison. I corrected this in the code above.