Previously I have been detecting if my TV is on by pinging it. It worked, well -almost. Sometimes the TV doesn't respond to pings for several minutes. Very frustrating indeed.
Recently I bought a couple of Fibaro wall switches FGWPF-102 ZW5. They can be used as power meters, hence I can determine if the TV is on by the value of the power usage reported. The power consumption when my TV is on is about 114W but it's varying. It never goes below 50W. But there is no power threshold to set on the FGWPF-102 ZW5. It can only be set to report power usage when triggered by a minimum percentage change in active power.
Working that way the FGWPF-102 ZW5 will send a quick series of power reports when i start or stop my TV. It's because the power usage raises and falls relatively slowly. My script needs to determine if the TV is "going down" or starting.
I have a virtual switch that indicates if the TV is on or off. It's named "LG 55UB820V"
The "TV Power Usage" device is electric usage device belonging to FGWPF-102 ZW5.
Below is my script. The problem is that several "devicechanged['TV Power Usage']" can occur quickly. The first time I may have issued "commandArray['LG 55UB820V'] = 'On'" but the device is still not "On" when the next "devicechanged['TV Power Usage']" happens. Now there is a gap between 10 and 90 watts where nothing will happen. It works but I feel my script is not optimal.
Code: Select all
if (devicechanged['TV Power Usage']) then
reading = tonumber(devicechanged['TV Power Usage'])
if ((reading <= 10) and (otherdevices['LG 55UB820V'] == 'On')) then
commandArray['LG 55UB820V'] = 'Off'
print('Turning off LG 55UB820V at Power Usage: ' .. reading)
elseif ((reading >= 90) and (otherdevices['LG 55UB820V'] == 'Off')) then
commandArray['LG 55UB820V'] = 'On'
print('Turning on LG 55UB820V at Power Usage: ' .. reading)
else
print('LG 55UB820V Power Usage: ' .. reading .. ' (Doing nothing)')
end
return commandArray
end