My first Virtual Device for Fibaro. Somehow useless, but it shows how a Virtual Device works.
The device checks the status, calculates the watt usage and it’s possible to turn on or off all lights.
It’s required to set the power consumption in Watt and make sure each light is set as a lighting device:
Copy the code below and save it as MyLights_0.5.vfib or download the file: [sdm_download id=”561″ fancy=”0″ color=”black”]
Afterwards import it as a new virtual device and replace the icons under the Update Button and main loop in the code with your icons (search for 1062 and 1062).
{"name":"MyLights 0.5","type":"virtual_device","properties":{"deviceIcon":1063,"currentIcon":"1062","log":"0 Watt","logTemp":"TxtGreen","mainLoop":"-- V0.1 basic funtionaility / 19.10.2015 / Andi -- V0.2 fixed status calculation / 19.10.2015 / Andi -- V0.3 added lights sum / 20.10.2015 / Andi -- V0.4 removed off devices from Label -- V0.5 Updated islight local vDeviceID = fibaro:getSelfId() --virtual device gets an ID local output = \"\"; --set output value local wattsum = \"0\"; --set wattsum value local statussum = \"0\"; --set statussum value local status = \"0\"; --set statussum value local sum = \"0\"; --set statussum value fibaro:debug('<span style=\"color:green;\">Main Loop:</span>') for i = 1, 500 do -- go trough all devices which are configured as lightning device if fibaro:getValue(i, \"isLight\") == \"1\" then \t\tlocal licht = fibaro:getName(i) --get name of the Device \tfibaro:debug(licht) \tsum = sum + 1; \tlocal status = fibaro:getValue(i,'value') --get status of the Device \tlocal watt = math.ceil(tonumber(fibaro:getValue(i, 'power'))) --get watt usage of the Device \tif(status=='1')then \t\twattsum = wattsum + watt --calculate watt usage \t\tstatustext = \"ON\"; \t\tstatussum = tonumber(statussum) + tonumber(status) --calculate status \toutput = licht .. \" (\" .. statustext .. \") \" .. watt .. \"W, \" .. output --set label text \t\t if(tonumber(statussum)>=1)then \t\tfibaro:call(vDeviceID, \"setProperty\", \"currentIcon\", 1063) \t\telse \t\tfibaro:call(vDeviceID, \"setProperty\", \"currentIcon\", 1062) \t\tend \t \telse \t\tstatustext = \"OFF\"; \tend -- \toutput = licht .. \" (\" .. statustext .. \") \" .. watt .. \"W<br/>\" .. output --set label text end end if(tonumber(statussum)<1)then \toutput = \"All Lights are OFF\" --set label text \tfibaro:debug(output) fibaro:call(vDeviceID, \"setProperty\", \"currentIcon\", 1062) end fibaro:debug(output) --set labels fibaro:call(vDeviceID, \"setProperty\", \"ui.Label1.value\",statussum .. \"/\" .. sum .. \" using \" ..wattsum .. \" Watt(s)\") fibaro:call(vDeviceID, \"setProperty\", \"ui.Label2.value\",output) fibaro:log(wattsum .. \" Watt\") fibaro:sleep(7000) -- Wait for 7 seconds","ui.Label1.value":"0/20 using 0 Watt(s)","ui.Label2.value":"All Lights are OFF","visible":"true","rows":[{"type":"button","elements":[{"id":1,"lua":true,"waitForResponse":false,"caption":"All ON","name":"Button11","empty":false,"msg":"-- go trough all devices which are configured as lightning device fibaro:debug('<span style=\"color:green;\">All Light devices:</span>') for i = 40, 1000 do if fibaro:getValue(i, \"isLight\") == \"1\" then \tfibaro:debug(fibaro:getName(i)) \tfibaro:call(i, \"turnOn\"); end end ","buttonIcon":1063,"favourite":false,"main":false},{"id":2,"lua":true,"waitForResponse":false,"caption":"All OFF","name":"Button12","empty":false,"msg":"--[[ %% properties %% globals --]] -- go trough all devices which are configured as lightning device fibaro:debug('<span style=\"color:green;\">All Light devices:</span>') for i = 40, 1000 do if fibaro:getValue(i, \"isLight\") == \"1\" then \tfibaro:debug(fibaro:getName(i)) \tfibaro:call(i, \"turnOff\"); end end ","buttonIcon":1062,"favourite":false,"main":false}]},{"type":"label","elements":[{"id":3,"lua":false,"waitForResponse":false,"caption":"Lights","name":"Label1","favourite":true,"main":true}]},{"type":"label","elements":[{"id":4,"lua":false,"waitForResponse":false,"caption":"","name":"Label2","favourite":false,"main":false}]},{"type":"button","elements":[{"id":5,"lua":true,"waitForResponse":false,"caption":"Update","name":"Button21","empty":false,"msg":"local vDeviceID = fibaro:getSelfId(); --virtual device gets an ID local output = \"\"; --set output value local wattsum = \"0\"; --set wattsum value local statussum = \"0\"; --set statussum value local status = \"0\"; --set statussum value local sum = \"0\"; --set statussum value fibaro:debug('<span style=\"color:green;\">Main Loop:</span>') for i = 1, 500 do -- go trough all devices which are configured as lightning device if fibaro:getValue(i, \"isLight\") == \"1\" then \t\tlocal licht = fibaro:getName(i) --get name of the Device \tfibaro:debug(licht) \tsum = sum + 1; \tlocal status = fibaro:getValue(i,'value') --get status of the Device \tlocal watt = math.ceil(tonumber(fibaro:getValue(i, 'power'))) --get watt usage of the Device \tif(status=='1')then \t\twattsum = wattsum + watt --calculate watt usage \t\tstatustext = \"ON\"; \t\tstatussum = tonumber(statussum) + tonumber(status) --calculate status \toutput = licht .. \" (\" .. statustext .. \") \" .. watt .. \"W, \" .. output --set label text \t\t if(tonumber(statussum)>=1)then \t\tfibaro:call(vDeviceID, \"setProperty\", \"currentIcon\", 1063) \t\telse \t\tfibaro:call(vDeviceID, \"setProperty\", \"currentIcon\", 1062) \t\tend \t \telse \t\tstatustext = \"OFF\"; \tend -- \toutput = licht .. \" (\" .. statustext .. \") \" .. watt .. \"W<br/>\" .. output --set label text end end if(tonumber(statussum)<1)then \toutput = \"All Lights are OFF\" --set label text \tfibaro:debug(output) fibaro:call(vDeviceID, \"setProperty\", \"currentIcon\", 1062) end fibaro:debug(output) --set labels fibaro:call(vDeviceID, \"setProperty\", \"ui.Label1.value\",statussum .. \"/\" .. sum .. \" using \" ..wattsum .. \" Watt(s)\") fibaro:call(vDeviceID, \"setProperty\", \"ui.Label2.value\",output) fibaro:log(wattsum .. \" Watt\")","buttonIcon":1063,"favourite":false,"main":true}]}]},"actions":{"pressButton":1,"setSlider":2,"setProperty":2}}