Example: Read Plugwise Smile P1 electricity/gas meter data with HTTP requests from Logic Machine 2

About Plugwise Smile P1

smile_p1

The Plugwise Smile P1 gives you realtime insight into your energy for both electricity and gas via smartphone or tablet.

Resident or Scheduled script

Execute the following program once in some specific time on Logic Machine 2 to read gas and electricity values and peak values.

Source code    
  1. require('socket.http')
  2.  
  3. socket.http.TIMEOUT = 5
  4. smiledata = socket.http.request('http://smile:hjzfhmdf@192.168.178.21/core/modules')
  5.  
  6. if not smiledata then
  7. alert('Smile: cannot fetch data')
  8. end
  9.  
  10. result = {}
  11.  
  12. function starttag(parser, tag, args)
  13. if tag == 'measurement' then
  14. curkey = args.directionality
  15. if args.tariff_indicator then
  16. curkey = curkey .. '_' .. args.tariff_indicator
  17. end
  18.  
  19. savevalue = true
  20. elseif tag:find('_meter') then
  21. curid = args.id
  22. result[ curid ] = { type = tag }
  23. end
  24. end
  25.  
  26. function chardata(parser, text)
  27. if savevalue then
  28. result[ curid ][ curkey ] = tonumber(text)
  29. savevalue = false
  30. end
  31. end
  32.  
  33. -- parse xml
  34. require('lxp').new({
  35. StartElement = starttag,
  36. CharacterData = chardata,
  37. }):parse(smiledata)
  38.  
  39. -- log full result table
  40. log(result)
  41. -- log single consumption item
  42. log(result['ad2ea5e2498e4b658ee54ed149f5f0e8'].consumed)