Added some logging and I think this helps me in trouble shooting it... Not sure about the solution yet
In more occasions I write the time difference to the log and that shows the following:
Code: Select all
2020-12-23 14:29:24.684 Status: EventSystem: reset all events...
2020-12-23 14:29:28.319 Status: LUA: 46641.0
2020-12-23 14:29:34.386 Status: LUA: 46647.0
2020-12-23 14:29:39.215 Status: LUA: 46652.0
2020-12-23 14:29:44.495 Status: LUA: 46657.0
2020-12-23 14:29:46.813 Status: User: Admin initiated a switch command (1142/TV Slaapkamer Switch/On)
2020-12-23 14:29:46.969 Status: LUA: 46659.0
2020-12-23 14:29:47.424 Status: LUA: 46660.0
2020-12-23 14:29:47.659 Status: LUA: 46660.0
2020-12-23 14:29:48.020 Status: LUA: 46661.0
2020-12-23 14:29:53.087 Status: LUA: 46666.0
2020-12-23 14:29:53.856 Status: LUA: 46666.0
2020-12-23 14:29:53.965 Status: LUA: 46666.0
2020-12-23 14:29:54.576 Status: LUA: 46667.0
2020-12-23 14:29:55.001 Status: LUA: 46668.0
2020-12-23 14:29:55.126 Status: LUA: 46668.0
2020-12-23 14:29:55.773 Status: LUA: 46668.0
2020-12-23 14:29:58.183 Status: LUA: 46671.0
2020-12-23 14:29:58.292 Status: LUA: 46671.0
2020-12-23 14:29:58.734 Status: LUA: 46671.0
2020-12-23 14:29:59.173 Status: LUA: 46672.0
2020-12-23 14:29:59.298 Status: LUA: 46672.0
2020-12-23 14:29:59.550 Status: LUA: 46672.0
2020-12-23 14:30:00.413 Status: LUA: 46673.0
2020-12-23 14:30:00.853 Status: LUA: 46673.0
2020-12-23 14:30:02.067 Status: LUA: 46675.0
2020-12-23 14:30:02.600 Status: LUA: 46675.0
2020-12-23 14:30:02.758 Status: LUA: 46675.0
2020-12-23 14:30:02.883 Status: LUA: 46675.0
2020-12-23 14:30:03.010 Status: LUA: 46676.0
2020-12-23 14:30:03.120 Status: LUA: 46676.0
2020-12-23 14:30:03.260 Status: LUA: 46676.0
2020-12-23 14:30:03.370 Status: LUA: 46676.0
2020-12-23 14:30:03.720 Status: LUA: 46676.0
2020-12-23 14:30:03.829 Status: LUA: 46676.0
2020-12-23 14:30:03.985 Status: LUA: 46676.0
2020-12-23 14:30:04.300 Status: LUA: 46677.0
2020-12-23 14:30:04.645 Status: LUA: 46677.0
2020-12-23 14:30:04.960 Status: LUA: 46677.0
2020-12-23 14:30:04.960 Status: LUA: Vastgesteld dat power voor het eerst hoger dan 75 is
2020-12-23 14:30:04.960 Status: LUA: 46677.0
2020-12-23 14:30:04.960 Status: LUA: 46677.0
2020-12-23 14:30:04.960 Status: LUA: 46677.0
2020-12-23 14:30:04.960 Status: LUA: Het LUA script heeft vastgesteld dat de TV in de slaapkamer al een uur aan staat en gaat nu uit
2020-12-23 14:30:05.006 Status: Notification: Het LUA script heeft vastgesteld dat de TV in de slaapkamer al een uur aan staat en gaat nu uit
2020-12-23 14:30:05.164 Status: LUA: 1.0
2020-12-23 14:30:05.164 Status: LUA: Het TVSlaapKamer is uitgezet
2020-12-23 14:30:05.164 Status: LUA: 1.0
2020-12-23 14:30:05.164 Status: LUA: 1.0
2020-12-23 14:30:05.321 Status: LUA: 0.0
2020-12-23 14:30:05.321 Status: LUA: Vastgesteld dat power voor het eerst hoger dan 75 is
2020-12-23 14:30:05.321 Status: LUA: 0.0
2020-12-23 14:30:05.321 Status: LUA: 0.0
2020-12-23 14:30:05.461 Status: LUA: 0.0
2020-12-23 14:30:05.461 Status: LUA: Het TVSlaapKamer is uitgezet
2020-12-23 14:30:05.461 Status: LUA: 0.0
2020-12-23 14:30:05.461 Status: LUA: 0.0
2020-12-23 14:30:05.586 Status: LUA: 0.0
2020-12-23 14:30:05.698 Status: LUA: 0.0
2020-12-23 14:30:05.809 Status: LUA: 0.0
2020-12-23 14:30:05.934 Status: LUA: 0.0
2020-12-23 14:30:06.044 Status: LUA: 1.0
2020-12-23 14:30:07.016 Status: LUA: 2.0
2020-12-23 14:30:07.141 Status: LUA: 2.0
So, it seems after determining the power is 75 or higher for the first time, the variable is not immediately updated and it is still working with the olf last changed date and shutting down the TV.
I think I fixed it by adding a constraint to check against the user variable as well rather than only the local variable. And only after the user variable has been set to 1, it will check for how long it has had the value of 1.
Wondering if there is any logic in not doing that for the coffee maker as well? Why both the user variable and the local variable?
Anyway.... here is the updated script:
Code: Select all
commandArray = {}
-- Calculate the time difference in seconds between Now and supplied time. in YYYY/MM/DD HH:MM:SS
function datetimedifferencenow(s)
s = s .. ":00" -- add seconds in case only hh:mm is supplied
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year = year, month = month, day = day, hour = hour, min = minutes, sec = seconds}
difference = os.difftime(t1, t2)
return difference
end
strTVSlaapKamer = otherdevices_svalues['TV Slaapkamer Watt']
strTVSlaapKamerPwrLoc = string.find(strTVSlaapKamer,";")
strTVSlaapKamerPwr = string.sub(strTVSlaapKamer,0,strTVSlaapKamerPwrLoc)
intTVSlaapKamerPwr = tonumber(strTVSlaapKamerPwr)
intTVSlaapKamerPowerNiveau = tonumber(uservariables['TVSlaapKamerPowerNiveau']) or 0
--print (datetimedifferencenow(uservariables_lastupdate['TVSlaapKamerPowerNiveau']))
if (intTVSlaapKamerPwr > 75) then
-- Zet Uservarible op 1 als het verbruik boven 75 watt is en nog niet op 1 staat
if intTVSlaapKamerPowerNiveau ~= 1 then
print ("Vastgesteld dat power voor het eerst hoger dan 75 is");
commandArray['Variable:TVSlaapKamerPowerNiveau'] = '1'
--print (datetimedifferencenow(uservariables_lastupdate['TVSlaapKamerPowerNiveau']))
intTVSlaapKamerPowerNiveau = 1
--print (datetimedifferencenow(uservariables_lastupdate['TVSlaapKamerPowerNiveau']))
end
else
-- Zet Uservarible op 0 als het verbruik lager is dan 35 watt is en nog niet op 0 staat
if intTVSlaapKamerPowerNiveau ~= 0 then
print ('De TV in de slaapkamer is uitgezet en de user variabele mag weer naar 0');
commandArray['Variable:TVSlaapKamerPowerNiveau'] = '0'
--print (datetimedifferencenow(uservariables_lastupdate['TVSlaapKamerPowerNiveau']))
intTVSlaapKamerPowerNiveau = 0
--print (datetimedifferencenow(uservariables_lastupdate['TVSlaapKamerPowerNiveau']))
end
end
--print (intTVSlaapKamerPowerNiveau)
--print (otherdevices['TV Slaapkamer Switch'])
--print (datetimedifferencenow(uservariables_lastupdate['TVSlaapKamerPowerNiveau']))
-- Zet het koffiezet apparaat uit als hij langer dan 1 uur aan staat
if (otherdevices['TV Slaapkamer Switch'] == 'On'
and intTVSlaapKamerPowerNiveau == 1
and uservariables['TVSlaapKamerPowerNiveau'] == '1'
and datetimedifferencenow(uservariables_lastupdate['TVSlaapKamerPowerNiveau']) > 3600 ) then
--print (datetimedifferencenow(uservariables_lastupdate['TVSlaapKamerPowerNiveau']))
commandArray['SendNotification']='Het LUA script heeft vastgesteld dat de TV in de slaapkamer al een uur aan staat en gaat nu uit'
commandArray['TV Slaapkamer Switch'] = "Off"
print ('Het LUA script heeft vastgesteld dat de TV in de slaapkamer al een uur aan staat en gaat nu uit');
end
return commandArray