Why LUA script execute multiple times per second

Moderator: leecollings

Post Reply
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Why LUA script execute multiple times per second

Post by rgroothuis »

I've developed multiple LUA scripts, I recently noticed that my doorbell script is executed multiple times. Or even worse on one doorbell push I get multiple notifications over Telegram. This means the script is executed multiple times. I only have one doorbell script in the LUA script directory. What is causing this problem?
2015-11-11 19:07:51.783 (NestHuiskamer) Thermostat (HuiskamerTemperatuurInstelling)
2015-11-11 19:07:51.841 LUA: Motion sensor script started......
2015-11-11 19:07:51.850 LUA: Door Bell script started......
2015-11-11 19:07:51.826 (NestHuiskamer) Temp + Humidity (HuiskamerTemperatuur)
2015-11-11 19:08:00.350 LUA: -- Rene is at home, check when the lights need to be enabled.....
2015-11-11 19:08:00.497 Hardware Monitor: Fetching data (System sensors)
2015-11-11 19:08:00.519 LUA: Motion sensor script started......
2015-11-11 19:08:00.532 LUA: Door Bell script started......
2015-11-11 19:08:00.579 LUA: Motion sensor script started......
2015-11-11 19:08:00.592 LUA: Door Bell script started......
2015-11-11 19:08:00.647 LUA: Motion sensor script started......
2015-11-11 19:08:00.656 LUA: Door Bell script started......
2015-11-11 19:08:00.689 LUA: Motion sensor script started......
2015-11-11 19:08:00.698 LUA: Door Bell script started......
2015-11-11 19:08:00.746 LUA: Motion sensor script started......
2015-11-11 19:08:00.755 LUA: Door Bell script started......
2015-11-11 19:08:22.822 LUA: Motion sensor script started......
2015-11-11 19:08:22.831 LUA: Door Bell script started......
2015-11-11 19:08:22.807 (NestHuiskamer) Thermostat (HuiskamerTemperatuurInstelling)
2015-11-11 19:08:22.865 LUA: Motion sensor script started.....
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Why LUA script execute multiple times per second

Post by jvdz »

The Device lua scripts are started each time any device is updated.
So also when you update a device in your lua script ALL device scripts will be fired again.
Key is to only set commandArray actions when you really need to change anything.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Why LUA script execute multiple times per second

Post by rgroothuis »

jvdz wrote:The Device lua scripts are started each time any device is updated.
So also when you update a device in your lua script ALL device scripts will be fired again.
Key is to only set commandArray actions when you really need to change anything.

Jos
Ok, clear. This means in that one (example) second I've 7 times a commandArray update. Otherwise I would not see that many script executions.

I need to find out which script is responsible for this. It is a bit strange because all my scripts have the following framework:

Code: Select all

commandArray = {}

print('Motion sensor script started......')

if (devicechanged['MailBoxMotionDetected'] == 'On') then
    print('-- Mailbox usage detected  at '.. os.date("%d/%m/%Y %X") .. '!')
    
    -- os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..message..'"  "https://api.telegram.org/bot'..token..'/sendMessage" ')
end

return commandArray
The commandArray variable will only be set within that if statement if a device is changed.
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Why LUA script execute multiple times per second

Post by jvdz »

Correct so it is either one of your defined devices receiving an update or one of your scripts setting a commandArray['device']='something'.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Why LUA script execute multiple times per second

Post by rgroothuis »

jvdz wrote:Correct so it is either one of your defined devices receiving an update or one of your scripts setting a commandArray['device']='something'.

Jos
Any suggestions on how to check/debug this? I just checked my LUA scripts and I cannot see a commandArray['device']='something' outside an if statement in which devicechanged is checked.

Is there a possibility in Domoticz so see which devices last have been changed?
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Why LUA script execute multiple times per second

Post by jvdz »

You could add a print() inside of the if when you actually set a command to be performed, then you know for sure, but actually you can already see it now because the log will contains a
record like this in case an action is performed:

Code: Select all

EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_xyz.lua
One other thing I understood is that the current production version will fire all device events for all known devices, not just the enabled devices.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Why LUA script execute multiple times per second

Post by rgroothuis »

Thanks Jos, your last comment is probably the reason. Because I don't see that:

Code: Select all

EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_xyz.lua
line in my log file every X seconds.

So it is not originating from a LUA script that sets the commandArray[] incorrectly. It has to come from one device changes inside Domoticz. In this situation it would be nice if I could see in a total overview which devices had a status change at which moment in time.
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Why LUA script execute multiple times per second

Post by jvdz »

Guess you could just look at the devices view (Setup/Devices) and sort on date Last Seen. Maybe that gives you an idea.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest