BUG - LUA runs before the "last update" which seems wrong
Moderator: leecollings
-
- Posts: 36
- Joined: Tuesday 06 October 2015 10:22
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Latest
- Contact:
BUG - LUA runs before the "last update" which seems wrong
Raspberry PI 3 updated
Zwave-me key
Domoticz last stable update v3.8153
Hello,
I just face an issue which I am pretty sure wasn't here some months ago.
When I do lua script, I put a time difference that when something is "on" for more than 5 minutes, it turns off.
The issue is that the script runs before the last update is updated and therefore the wrong value is taken.
Example: if I switch on the light, the LUA even kicks in and does the script. As the script says turn "off" when more than 300 secondes, the light automatically switch off at the same moment because he doesn't have yet the "last update" updated with when I switch it on...
I can confirm this issue because if I "track" the seconds of last update from now, when I click on the button, the script gives me the seconds BEFORE I clicked the button which is wrong. It should give AFTER the update (means 0 seconds as I just clicked)...
You can see here below that scripts runs before the OpenZwave light means before the action was done which I believe is wrong.
2017-10-22 01:31:10.479 OpenZWave: Domoticz has send a Switch command! NodeID: 59 (0x3b)
2017-10-22 01:31:10.619 LUA: 349
2017-10-22 01:31:10.631 (OpenZWave) Light/Switch (LumEntree)
2017-10-22 01:31:14.277 LUA: 4
2017-10-22 01:31:14.282 (RfxCom) Temp + Humidity (Temp_Outside_Home)
Thanks for your POV or fix.
Regards
Sam
Zwave-me key
Domoticz last stable update v3.8153
Hello,
I just face an issue which I am pretty sure wasn't here some months ago.
When I do lua script, I put a time difference that when something is "on" for more than 5 minutes, it turns off.
The issue is that the script runs before the last update is updated and therefore the wrong value is taken.
Example: if I switch on the light, the LUA even kicks in and does the script. As the script says turn "off" when more than 300 secondes, the light automatically switch off at the same moment because he doesn't have yet the "last update" updated with when I switch it on...
I can confirm this issue because if I "track" the seconds of last update from now, when I click on the button, the script gives me the seconds BEFORE I clicked the button which is wrong. It should give AFTER the update (means 0 seconds as I just clicked)...
You can see here below that scripts runs before the OpenZwave light means before the action was done which I believe is wrong.
2017-10-22 01:31:10.479 OpenZWave: Domoticz has send a Switch command! NodeID: 59 (0x3b)
2017-10-22 01:31:10.619 LUA: 349
2017-10-22 01:31:10.631 (OpenZWave) Light/Switch (LumEntree)
2017-10-22 01:31:14.277 LUA: 4
2017-10-22 01:31:14.282 (RfxCom) Temp + Humidity (Temp_Outside_Home)
Thanks for your POV or fix.
Regards
Sam
-
- Posts: 36
- Joined: Tuesday 06 October 2015 10:22
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Latest
- Contact:
Re: BUG - LUA runs before the "last update" which seems wrong
I checked once more and beleive there is really a bug
.
When I push the "lumierentree", the system give me the last update BEFORE the activation. You can see it on the blue lines.
See below:
2017-10-22 09:05:53.735 dzVents: 2017-10-22 01:31:10
2017-10-22 09:05:53.749 (OpenZWave) Light/Switch (SensorRez)
2017-10-22 09:05:57.541 dzVents: 2017-10-22 01:31:10
2017-10-22 09:05:57.554 (OpenZWave)Light/Switch (LumEntree)
A quick fix would be very appreciated as all my script which are taking the last update are not working anymore...

When I push the "lumierentree", the system give me the last update BEFORE the activation. You can see it on the blue lines.
See below:
2017-10-22 09:05:53.735 dzVents: 2017-10-22 01:31:10
2017-10-22 09:05:53.749 (OpenZWave) Light/Switch (SensorRez)
2017-10-22 09:05:57.541 dzVents: 2017-10-22 01:31:10
2017-10-22 09:05:57.554 (OpenZWave)Light/Switch (LumEntree)
A quick fix would be very appreciated as all my script which are taking the last update are not working anymore...
-
- Posts: 1355
- Joined: Friday 29 August 2014 11:26
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Ermelo
- Contact:
Re: BUG - LUA runs before the "last update" which seems wrong
Well, the thing is.. earlier, the lastUpdate value was wrongly implemented.. well.. at least, not quite useful meaning-wise in some instances.
You have the situation where a device is updated (somebody presses a switch for example) and that triggers a script for that device. Now, earlier, lastUpdate was updated BEFORE the script was triggered causing the value of lastUpdate during script execution to be equal to the current time (perhaps a few milliseconds older). In this case there is no way to figure out the time between the current event and the previous event. And that is a big problem if you want to do some clever stuff depending of the period between the two events (current and previous). That's why it was changed to make more sense.
So, this is only the case for the device that triggered the event-system! All other devices will have a lastUpdate that matches exactly the moment of the last change. So if you have a timer script and you query all your devices for the lastUpdate value, this value is always the time of the last event (as the event was not triggered due to a device update).
I have many scripts that do stuff depending on the lastUpdate (of course with dzVents) and they all make sense and work as expected.
You have the situation where a device is updated (somebody presses a switch for example) and that triggers a script for that device. Now, earlier, lastUpdate was updated BEFORE the script was triggered causing the value of lastUpdate during script execution to be equal to the current time (perhaps a few milliseconds older). In this case there is no way to figure out the time between the current event and the previous event. And that is a big problem if you want to do some clever stuff depending of the period between the two events (current and previous). That's why it was changed to make more sense.
So, this is only the case for the device that triggered the event-system! All other devices will have a lastUpdate that matches exactly the moment of the last change. So if you have a timer script and you query all your devices for the lastUpdate value, this value is always the time of the last event (as the event was not triggered due to a device update).
I have many scripts that do stuff depending on the lastUpdate (of course with dzVents) and they all make sense and work as expected.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
-
- Posts: 36
- Joined: Tuesday 06 October 2015 10:22
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Latest
- Contact:
Re: BUG - LUA runs before the "last update" which seems wrong
Hi Danny,
thanks for your reply !
Let me post an example of my script and would be grateful if you could explain me on how to avoid such misleading output.
As you can see, I am simply checking if the last update was more than 30 secondes or 300 seconds to do an action. Issue is that it turns the actions because he believe the value is above 300 seconds when the actual value is zero !
Thanks in advance for your feedback !
Sam
thanks for your reply !
Let me post an example of my script and would be grateful if you could explain me on how to avoid such misleading output.
Code: Select all
commandArray = {}
function timedifference (s)
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
stopothers = 0 --to avoid other scripts to run at the same time
-- 1 Lumiere interieur entree--
if (otherdevices['LumEntree'] == 'On')
and (timedifference(otherdevices_lastupdate['SensorRez']) > 300)
and stopothers == 0 then
stopothers = 1
commandArray['LumEntree']='Off'
print('1 - Eteint lumiere entree dedans')
end
-- 2 Lumiere interieur entree--
if otherdevices['LumEntree'] == 'Off' and
otherdevices['Porte Entree'] == 'Open' and
timeofday['Nighttime'] == true and
(timedifference(otherdevices_lastupdate['LumEntree']) > 30)
and stopothers == 0 then
stopothers = 1
commandArray['LumEntree']='On'
print('2 - allume lumiere entree dedans')
end
Thanks in advance for your feedback !
Sam
Last edited by dannybloe on Monday 23 October 2017 7:43, edited 1 time in total.
Reason: Added code tags around code.
Reason: Added code tags around code.
-
- Posts: 1355
- Joined: Friday 29 August 2014 11:26
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Ermelo
- Contact:
Re: BUG - LUA runs before the "last update" which seems wrong
How is this script triggered? Is it a timer script that runs every minute? And can you elaborate a bit about what it exactly does?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
-
- Posts: 36
- Joined: Tuesday 06 October 2015 10:22
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Latest
- Contact:
Re: BUG - LUA runs before the "last update" which seems wrong
Thanks for asking.
It runs all the time (all in Domoticz).
When door open, light still off, light off for more than x minutes, it switch light on. -> This works well.
When light on for more than 5 minutes, no "indoor" presence, it switch off the light. -> My issue !
When I come home, open the door, it switch on the light and then automatically switch it off. Reason is that he knows light is on but as the last update hasn't been updated, he beleives that light is on for more than 5 minutes and therefore switch it off.
On the switch off on, yesterday evening, I have added a devicechangedXXXX == nil and it works. But it doesn't feel clean, would you have another idea ?
It runs all the time (all in Domoticz).
When door open, light still off, light off for more than x minutes, it switch light on. -> This works well.
When light on for more than 5 minutes, no "indoor" presence, it switch off the light. -> My issue !
When I come home, open the door, it switch on the light and then automatically switch it off. Reason is that he knows light is on but as the last update hasn't been updated, he beleives that light is on for more than 5 minutes and therefore switch it off.
On the switch off on, yesterday evening, I have added a devicechangedXXXX == nil and it works. But it doesn't feel clean, would you have another idea ?
-
- Posts: 1355
- Joined: Friday 29 August 2014 11:26
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Ermelo
- Contact:
Re: BUG - LUA runs before the "last update" which seems wrong
Mm.. I'd move to dzVents asap as your approach (using the traditional way of how events are triggered in Domoticz) is way to uncontrollable.
In dzVents you create triggers and act upon then. Much better and that way you have a predictable lastUpdate. Your scripts are executed for every event that takes place in your system, even if it is totally unrelated to what the script does (like a temp sensor that was updated). That's exactly the reason why you get these unpredictable lastUpdates because you have no idea what caused the events to be triggered.
dzVents makes your life a whole lot easier, less code, full control over what will happen when, better maintainability (and much better documentation).
I was about to write a solution for you but I have promised myself never to write traditional Lua event-code anymore because to me that's a waste of time and a dead-end if you ask me
So, take some time to look at dzVents and I'm glad to help you out if you have questions (or better use the dzVents forum). You'll be rewarded with much less code and much more readable as well.
Good luck.
Danny
In dzVents you create triggers and act upon then. Much better and that way you have a predictable lastUpdate. Your scripts are executed for every event that takes place in your system, even if it is totally unrelated to what the script does (like a temp sensor that was updated). That's exactly the reason why you get these unpredictable lastUpdates because you have no idea what caused the events to be triggered.
dzVents makes your life a whole lot easier, less code, full control over what will happen when, better maintainability (and much better documentation).
I was about to write a solution for you but I have promised myself never to write traditional Lua event-code anymore because to me that's a waste of time and a dead-end if you ask me

So, take some time to look at dzVents and I'm glad to help you out if you have questions (or better use the dzVents forum). You'll be rewarded with much less code and much more readable as well.
Good luck.
Danny
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
-
- Posts: 71
- Joined: Thursday 12 January 2017 13:38
- Target OS: Raspberry Pi / ODroid
- Domoticz version: all beta
- Location: NL - Pijnacker
- Contact:
Re: BUG - LUA runs before the "last update" which seems wrong
I found the same issue as svenker. However, the explanation of dannyblue also makes some sense. Although it would have been much better to call it PreviousUpdate.
If you have a Domoticz master/slave installation, then you will see the same device twice. In LUA this exactly the same device will show two different LastUpdate values, depending on the one that triggered the script. That is of course nonsense.
I don't use dsVents, it made my systems instable. Perhaps, this is solved now, but that is not important.
A real programming language like LUA (well, allmost) gives much more flexibility.
Now, the issue of svenker. If the script is an event-based script and you know the device that triggered it, then you also know the REAL LastModified of that specific device, namely, the actual time of the system.
So, you should change your LUA script that it checks the current time instead of the LastModified time of the device.
If you have a Domoticz master/slave installation, then you will see the same device twice. In LUA this exactly the same device will show two different LastUpdate values, depending on the one that triggered the script. That is of course nonsense.
I don't use dsVents, it made my systems instable. Perhaps, this is solved now, but that is not important.
A real programming language like LUA (well, allmost) gives much more flexibility.
Now, the issue of svenker. If the script is an event-based script and you know the device that triggered it, then you also know the REAL LastModified of that specific device, namely, the actual time of the system.
So, you should change your LUA script that it checks the current time instead of the LastModified time of the device.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
-
- Posts: 1355
- Joined: Friday 29 August 2014 11:26
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Ermelo
- Contact:
Re: BUG - LUA runs before the "last update" which seems wrong
dzVents is 100% Lua. It's just a library on top of the commandArray and the various scattered Lua tables provided by Domoticz with a clear and sane object model. So I really don't know where your claim about flexibility comes from. Perhaps you should read about dzVents some more.
And it's pretty stable actually. In fact.. it has around 370 unit tests and around 140 integration tests (against a running Domoticz instance). I dare to say that currently dzVents is the best tested piece of code that currently ships with Domoticz.
And it's pretty stable actually. In fact.. it has around 370 unit tests and around 140 integration tests (against a running Domoticz instance). I dare to say that currently dzVents is the best tested piece of code that currently ships with Domoticz.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
-
- Posts: 36
- Joined: Tuesday 06 October 2015 10:22
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Latest
- Contact:
Re: BUG - LUA runs before the "last update" which seems wrong
Thanks to all your feedbacks ! I have to read more about dzvents. My standard scripts are now working well again so move to dzvents is unecessary work even if I am getting curious.
For people who face the same issue, in order to avoid to take the previous lastupdate script (top right in Domoticz, you need to select "device" and not "all") and in your IF conditions, simply add one "devicechanged['LumGarageInt'] == nil". The 'LumGarageInt' has to be NIL to avoid to run this IF with the previous lastupdate. You can also do timedifference script on a "TIME" LUA which runs every minute and one DEVICE LUA on each command fired with a device changed.
BUT A TIME LUA will not accept a "devicechanged" item as the table is not available at that time...
Feel free to PM me if unclear. I am not a master but just getting used to it
.
Bonne nuit from Switzerland.
Sam
For people who face the same issue, in order to avoid to take the previous lastupdate script (top right in Domoticz, you need to select "device" and not "all") and in your IF conditions, simply add one "devicechanged['LumGarageInt'] == nil". The 'LumGarageInt' has to be NIL to avoid to run this IF with the previous lastupdate. You can also do timedifference script on a "TIME" LUA which runs every minute and one DEVICE LUA on each command fired with a device changed.
BUT A TIME LUA will not accept a "devicechanged" item as the table is not available at that time...
Feel free to PM me if unclear. I am not a master but just getting used to it

Bonne nuit from Switzerland.
Sam
Who is online
Users browsing this forum: No registered users and 0 guests