Force LUA script to run only one instance at a time

Moderator: leecollings

Post Reply
Plantje
Posts: 451
Joined: Friday 16 October 2015 7:58
Target OS: Windows
Domoticz version:
Contact:

Force LUA script to run only one instance at a time

Post by Plantje »

I have created a LUA script to check if my coffee machine is still on. (My wife sometimes leaves it on after taking the last coffee).

I set the trigger to "all", but it goes completely bezerk. Even if I switch off the smart plug I get several messages that it has been switched off.
I think this can be solved by forcing the script to only run one instance at a time. Currently, I have the idea that as soon as it meets the requirements to start it keeps on starting over and over again.

Code: Select all

commandArray = {}

strKoffiezetapparaat = otherdevices_svalues['Koffiezetapparaat Power Meter'] 
strKoffiezetapparaatPwrLoc = string.find(strKoffiezetapparaat,";")
strKoffiezetapparaatPwr = string.sub(strKoffiezetapparaat,0,strKoffiezetapparaatPwrLoc)
intKoffiezetapparaatPwr = tonumber(strKoffiezetapparaatPwr)
intCounter = 0
blnExitLoop = 0

if (intKoffiezetapparaatPwr < 45 and intKoffiezetapparaatPwr > 35) then
    print ("Vastgesteld dat power hoger dan 35 en lager dan 45 is");

    t1 = os.date()
    t3 = os.time()
    t2 = os.date()
    t4 = os.time()
    print ("T1 buiten loop '"..t1.."'");
    print ("T2 buiten loop '"..t2.."'");
    print ("T3 buiten loop '"..t3.."'");
    print ("T4 buiten loop '"..t4.."'");
    difference = (os.difftime (t4, t3))
    print ("Verschil buiten loop '"..tostring(difference).."'");
    repeat
        intCounter = intCounter + 1
        t2 = os.date()
        t4 = os.time()
        difference = (os.difftime (t4, t3))
        if (intCounter == 60) then
            print ("T1 binnen loop '"..t1.."'");
            print ("T2 binnen loop '"..t2.."'");
            print ("T3 binnen loop '"..t3.."'");
            print ("T4 binnen loop '"..t4.."'");
            print ("Verschil binnen loop '"..tostring(difference).."'");
            intCounter = 0
        end
        
        strKoffiezetapparaat = otherdevices_svalues['Koffiezetapparaat Power Meter'] 
        strKoffiezetapparaatPwrLoc = string.find(strKoffiezetapparaat,";")
        strKoffiezetapparaatPwr = string.sub(strKoffiezetapparaat,0,strKoffiezetapparaatPwrLoc)
        intKoffiezetapparaatPwr = tonumber(strKoffiezetapparaatPwr)
    
        if (otherdevices['Koffiezetapparaat Switch'] == 'On' and difference > 3600 and intKoffiezetapparaatPwr < 45 and intKoffiezetapparaatPwr > 35) then
            commandArray['SendNotification']='Koffiezetapparaat#Het LUA script heeft vastgesteld dat het Koffiezetapparaat al een uur aan staat en gaat nu uit'
            commandArray['Koffiezetapparaat Switch'] = "Off"
            Print ('Koffiezetapparaat#Het LUA script heeft vastgesteld dat het Koffiezetapparaat al een uur aan staat en gaat nu uit');
            blnExitLoop = 1
        elseif otherdevices['Koffiezetapparaat Switch'] == 'Off' then
            commandArray['SendNotification']='De stekker van het koffiezetapparaat is al uitgezet'
            Print ('De stekker van het koffiezetapparaat is al uitgezet');
            blnExitLoop = 1
        elseif intKoffiezetapparaatPwr < 35 then
            commandArray['SendNotification']='Het koffiezetapparaat zelf is al uitgezet'
            Print ('Het koffiezetapparaat zelf is al uitgezet');
            blnExitLoop = 1
        end 
    until(difference > 3610 or blnExitLoop == 1)
end 
return commandArray
And at some point I get:
2019-08-23 22:48:29.185 Error: EventSystem: in Koffiezetapparaat LUA: Lua script execution exceeds maximum number of lines

Just playing around with this, but it is not really important.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Force LUA script to run only one instance at a time

Post by waaren »

Plantje wrote: Friday 23 August 2019 22:50 I have created a LUA script to check if my coffee machine is still on. (My wife sometimes leaves it on after taking the last coffee).
The domoticz event system is not designed to deal with these kind of loops. Every script does need to end within 10 seconds, mainly because the event system is single threaded, so one script can block the complete event system if it takes too long. The max number of lines message originates from the Lua interpreter within domoticz that does count every line it interprets; so not max. the lines in a script but if a line is in a loop that is executed 1000 times the Lua interpreter does count it as 1000 lines.

Back to the original solution to prevent the coffeeMachine staying on for too long...
in dzVents you could do something like

Code: Select all

coffeeMachine.switchOff.afterSec(3600) 
when the script noticed the coffeeMachine was switched on. It gets a bit more complicated (but still possible) when you want to deal with a situation where the coffeeMachine was switched On and someone decided more coffee was needed within that 1 hour. A decision would then have to be made on what the script should do; still switchOff at the initial scheduled time or does the new startTime need to reset the switchOff counter to 0 before counting up to 3600 .
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Plantje
Posts: 451
Joined: Friday 16 October 2015 7:58
Target OS: Windows
Domoticz version:
Contact:

Re: Force LUA script to run only one instance at a time

Post by Plantje »

waaren wrote: Saturday 24 August 2019 0:37
Plantje wrote: Friday 23 August 2019 22:50 I have created a LUA script to check if my coffee machine is still on. (My wife sometimes leaves it on after taking the last coffee).
The domoticz event system is not designed to deal with these kind of loops. Every script does need to end within 10 seconds, mainly because the event system is single threaded, so one script can block the complete event system if it takes too long. The max number of lines message originates from the Lua interpreter within domoticz that does count every line it interprets; so not max. the lines in a script but if a line is in a loop that is executed 1000 times the Lua interpreter does count it as 1000 lines.
Ok, that's clear then. I am going about this the wrong way... And it makes sense not having scripts constantly running for hours and hours.
waaren wrote: Saturday 24 August 2019 0:37 Back to the original solution to prevent the coffeeMachine staying on for too long...
in dzVents you could do something like

Code: Select all

coffeeMachine.switchOff.afterSec(3600) 
when the script noticed the coffeeMachine was switched on.
Yes, that was my first attempt too. I created that in Blockly and it was fairly ok. The downside is: in that case I cannot send a message at the very moment the smart plug is switched off.
waaren wrote: Saturday 24 August 2019 0:37 It gets a bit more complicated (but still possible) when you want to deal with a situation where the coffeeMachine was switched On and someone decided more coffee was needed within that 1 hour. A decision would then have to be made on what the script should do; still switchOff at the initial scheduled time or does the new startTime need to reset the switchOff counter to 0 before counting up to 3600 .
Perhaps I can solve it like this: create a dummy switch (would be the first time for me). Then, as soon as the coffeemachine uses between 35 and 45 Watt tell the dummy switch to switch off after an hour. Then write another script that checks if the dummy switch is set to off. If so, send the message, switch off the coffee machine and set the dummy switch back to on again.
However, the first script will be fired on all events and set the dummy switch to switch off after an hour, after an hour and 1 second, after an hour and 3 seconds and so on.... Would it then have some sort off schedule for as long as the hour is running for every event that triggered it?

The real solution would be to extend the Domoticz events functionality and not only allow for a switch off "after x seconds", but also a "run script after x seconds". Or is that functionality available and do I just miss it?

Thank you for your responses. I am spending way more time on this then I should, because it is just fun :)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Force LUA script to run only one instance at a time

Post by waaren »

Plantje wrote: Saturday 24 August 2019 9:43 I am spending way more time on this then I should, because it is just fun :)
Played a little bit with the idea. This dzVents script could be a working solution. Please let me know your findings or any other feedback.

When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minute reading). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents disabled' is not checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."

In this script I use a delayed openUrl as method to retrigger the script after a given time. Until someone is clever enough to natively implement triggering a script from a script this is a work around that does the job without virtual switches or domoticz uservariables.

Code: Select all

-- CoffeeMachine.lua

local scriptVar = "coffeeMachineProtection"
 
return 
{
    on      = 
    { 
        devices        = { "CoffeeSwitch", "CoffeePower" },
        httpResponses  = { scriptVar },
    },
                
    data = 
    { 
        usage = 
        { 
            history = true, 
            maxHours = 2 
        },
        state = { initial = '' },
    },
    
    logging = 
    { 
        level = domoticz.LOG_DEBUG, 
        marker  = scriptVar, 
    },

    execute = function(dz, item)
        local coffeeSwitch = dz.devices('CoffeeSwitch')
        local coffeePower = dz.devices('CoffeePower').WhActual
        local coffeeMachineOnStandBy = coffeePower > 35 and coffeePower < 45
        local coffeeMachineOff = coffeePower < 10
        local coffeeMachineActive = coffeePower > 45
        
        local function retriggerScript(delay)
            local  url = dz.settings['Domoticz url'] .. '/json.htm?type=command&param=addlogmessage&message=' .. scriptVar .. '%20retriggered'
            dz.openURL  ({ url = url, callback = scriptVar }).afterMin(delay)   -- to come back after delay minutes
        end

        if coffeeMachineActive and dz.data.state ~= 'Active' then
            dz.data.state = 'Active'
        elseif item.isDevice and coffeeMachineOnStandBy and dz.data.state ~= 'StandBy' then
            dz.data.state = 'StandBy'
            dz.log('Coffeemachine on standBy; scheduled check in 61 minutes',dz.LOG_DEBUG)
            retriggerScript(61)
        elseif item.isDevice and coffeeMachineOff and dz.data.state ~= 'Off' then
            dz.data.state = 'Off'
            dz.log('Coffeemachine switched off manually',dz.LOG_DEBUG)
            dz.notify(scriptVar,'Coffeemachine has been switched off manually')
        elseif item.isHTTPResponse then      
            local maxUsage = dz.data.usage.maxSince('01:00:00') or 0
            dz.log('maxUsage in last hour:' .. maxUsage,dz.LOG_DEBUG)
            if maxUsage > 45 then 
                dz.log('Coffeemachine was used again in the last hour. No action needed',dz.LOG_DEBUG)
            elseif maxUsage > 35 then
                dz.log('Coffeemachine still on standBy. Switching Off now',dz.LOG_DEBUG)
                dz.notify(scriptVar,'Coffeemachine still on standBy. Switching Off now')
                coffeeSwitch.switchOff().silent()
                dz.data.state = 'Off'
            end
        end
        dz.data.usage.add(coffeePower)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Plantje
Posts: 451
Joined: Friday 16 October 2015 7:58
Target OS: Windows
Domoticz version:
Contact:

Re: Force LUA script to run only one instance at a time

Post by Plantje »

Thanks for thinking along! Really appreciated!

I will first read through the getting started, then go through the script you proposed and come back with my findings. However, it may take a while as I will go back to work after three weeks of vacation :)
Plantje
Posts: 451
Joined: Friday 16 October 2015 7:58
Target OS: Windows
Domoticz version:
Contact:

Re: Force LUA script to run only one instance at a time

Post by Plantje »

waaren wrote: Sunday 25 August 2019 0:16
Plantje wrote: Saturday 24 August 2019 9:43 I am spending way more time on this then I should, because it is just fun :)
Played a little bit with the idea. This dzVents script could be a working solution. Please let me know your findings or any other feedback.
Will still look into this if I have a little bit more time. However, in the mean time I was wondering: would it be possible with a script written in Python as well? In general, I notice Python is more a hot skill. So if I want to invest in learning a new programming/scripting language, perhaps I can better invest in Ptyhon.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Force LUA script to run only one instance at a time

Post by waaren »

Plantje wrote: Saturday 02 May 2020 9:53 Will still look into this if I have a little bit more time. However, in the mean time I was wondering: would it be possible with a script written in Python as well? In general, I notice Python is more a hot skill. So if I want to invest in learning a new programming/scripting language, perhaps I can better invest in Python.
It all depends on what you want to achieve.
If you want a paid job as programmer then don't invest any time in learning dzVents.
If you want to quickly get some home automation tasks done with domoticz then dzVents is my preferred choice but I am sure that other forum members will have other favorites (classic Lua, PHP, Python, etc..)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Plantje
Posts: 451
Joined: Friday 16 October 2015 7:58
Target OS: Windows
Domoticz version:
Contact:

Re: Force LUA script to run only one instance at a time

Post by Plantje »

I have a paid job as a BI consultant and I would like to extend my horizon. Which category is that? 😀
But I get your point.

With blockly I cannot do what I want to achieve here. With Lua perhaps. And with dzVents there is a possibility that you can help me
azonneveld
Posts: 160
Joined: Wednesday 02 October 2019 7:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands
Contact:

Re: Force LUA script to run only one instance at a time

Post by azonneveld »

Plantje wrote: Saturday 02 May 2020 10:17 I have a paid job as a BI consultant and I would like to extend my horizon. Which category is that? 😀
But I get your point.

With blockly I cannot do what I want to achieve here. With Lua perhaps. And with dzVents there is a possibility that you can help me
Maybe you can flash the smartplug using ESPEASY. Using espeasy you can integrate the timer in the smartplug, using the rules.

By the way, don't use the 'all' trigger, but use the 'time' trigger, this will make your script to run every minute.
rpi4 - zigbee2mqtt - roborock - espeasy - rfxcom - homewizard p1 - otgw - homebridge - surveillance station - egardia - goodwe - open weather map - wol - BBQ detection - rsync backup
Plantje
Posts: 451
Joined: Friday 16 October 2015 7:58
Target OS: Windows
Domoticz version:
Contact:

Re: Force LUA script to run only one instance at a time

Post by Plantje »

I would think there should be an easier way to just have a smart plug switch off after it has been on for 1,5 hours.

I quickly looked up ESPEASY, but for now I don't really see any added value.
User avatar
jvdz
Posts: 2332
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Force LUA script to run only one instance at a time

Post by jvdz »

Couldn't you simply change the original script a bit to make it work?
Just remove the loop and make it a time script that sets a uservariable when the power consumption changes and test for the lastupdate date?
Something like this (untested):

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

strKoffiezetapparaat = otherdevices_svalues['Koffiezetapparaat Power Meter']
strKoffiezetapparaatPwrLoc = string.find(strKoffiezetapparaat,";")
strKoffiezetapparaatPwr = string.sub(strKoffiezetapparaat,0,strKoffiezetapparaatPwrLoc)
intKoffiezetapparaatPwr = tonumber(strKoffiezetapparaatPwr)
intKoffiezetapparaatPowerNiveau = tonumber(uservariables['KoffiezetapparaatPowerNiveau']) or 0

if (intKoffiezetapparaatPwr > 45) then
	-- Zet Uservarible op 1 als het verbruik boven 45 watt is en nog niet op 1 staat
	if intKoffiezetapparaatPowerNiveau ~= 2 then
		print ("Vastgesteld dat power voor het eerst hoger dan 45 is");
		commandArray['Variable:KoffiezetapparaatPowerNiveau'] = '2'
		intKoffiezetapparaatPowerNiveau = 2
	end
elseif (intKoffiezetapparaatPwr < 45 and intKoffiezetapparaatPwr > 35) then
	-- Zet Uservarible op 2 als het verbruik tussen 35-45 watt is en nog niet op 2 staat
	if intKoffiezetapparaatPowerNiveau ~= 1 then
		print ("Vastgesteld dat power voor het eerst hoger dan 35 en lager dan 45 is");
		commandArray['Variable:KoffiezetapparaatPowerNiveau'] = '1'
		intKoffiezetapparaatPowerNiveau = 1
	end
else
	-- Zet Uservarible op 0 als het verbruik lager is dan 35 watt is en nog niet op 0 staat
	if intKoffiezetapparaatPowerNiveau ~= 0 then
		print ('Het koffiezetapparaat is uitgezet');
		commandArray['Variable:KoffiezetapparaatPowerNiveau'] = '0'
		intKoffiezetapparaatPowerNiveau = 0
	end
end

-- zet het koffiezet apparaat uit als hij langer dan 1 uur aan staat
if (otherdevices['Koffiezetapparaat Switch'] == 'On'
and intKoffiezetapparaatPowerNiveau == 1
and datetimedifferencenow(uservariables_lastupdate['KoffiezetapparaatPowerNiveau']) > 3600 )  then
	commandArray['SendNotification']='Koffiezetapparaat#Het LUA script heeft vastgesteld dat het Koffiezetapparaat al een uur aan staat en gaat nu uit'
	commandArray['Koffiezetapparaat Switch'] = "Off"
	print ('Koffiezetapparaat#Het LUA script heeft vastgesteld dat het Koffiezetapparaat al een uur aan staat en gaat nu uit');
end
return commandArray
EDIT: Made a few changes in the script.
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Plantje
Posts: 451
Joined: Friday 16 October 2015 7:58
Target OS: Windows
Domoticz version:
Contact:

Re: Force LUA script to run only one instance at a time

Post by Plantje »

Thanks, Jos! That's it!

I wasn't aware that you can check for the lastupdate property of a user variable! I will implement it as soon as possible!

At the moment, I don't have that smart plug in my network as it was throwing some errors and I think that required a daily reboot of my Domoticz server, but anyway.... looks good!
Plantje
Posts: 451
Joined: Friday 16 October 2015 7:58
Target OS: Windows
Domoticz version:
Contact:

Re: Force LUA script to run only one instance at a time

Post by Plantje »

Just to confirm: this script is doing exactly what I wanted. Thanks a lot! Now I don't have to worry if I switched off the coffee machine as it happens automatically!
Plantje
Posts: 451
Joined: Friday 16 October 2015 7:58
Target OS: Windows
Domoticz version:
Contact:

Re: Force LUA script to run only one instance at a time

Post by Plantje »

@jvdz, thanks again for this script.

I have made some changes and applied it to my TV too. However, it does something strange in some cases. In a scene I have added the TV to switch on and this is the 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

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'
		intTVSlaapKamerPowerNiveau = 1
	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 ('Het TVSlaapKamer is uitgezet');
		commandArray['Variable:TVSlaapKamerPowerNiveau'] = '0'
		intTVSlaapKamerPowerNiveau = 0
	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 datetimedifferencenow(uservariables_lastupdate['TVSlaapKamerPowerNiveau']) > 3600 )  then
	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
If I start the scene I see the following in the log (and the TV switches on and immediately off again):

Code: Select all

2020-12-23 00:24:39.225 Status: LUA: Vastgesteld dat power voor het eerst hoger dan 75 is
2020-12-23 00:24:39.225 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 00:24:39.272 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 00:24:39.555 Status: LUA: Het TVSlaapKamer is uitgezet
If I, however, switch on the TV by the TV switch in Domoticz (so apart from the rest of the scene) everything works fine. And after an hour the TV is switched off. What is going wrong here?
Plantje
Posts: 451
Joined: Friday 16 October 2015 7:58
Target OS: Windows
Domoticz version:
Contact:

Re: Force LUA script to run only one instance at a time

Post by Plantje »

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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest