Rather a low tech solution, but the sx40 has a strange mode called 'Movie digest' where it records a low resolution (640 x 480) film for 2 to 4 seconds BEFORE the button is pressed to take a picture, so almost no matter how slow your motion detection script is you will have a film of that flash of lightning which triggered the picture to be taken! Sadly no matter what I have tried setting in CHDK I cannot change the resolution of the film taken.
BillBal