I made some improvements on this sketch. Please review them here: https://github.com/mysensors/MySensors/pull/1540
with these changes you will have:
1 - factory reset
2 - automatic home assistant entities creation
3 - counter correction from home assistant using service notify.mysensors
4 - fixed counter automatically incremented by 1 at each device restart (due to arduino library interrupt bug)
5- other tiny improvements