Page 1 of 1
attempt to index global 'devicechanged' (a nil value)
Posted: Thursday 27 December 2018 21:49
by gschmidt
Hi,
I have created LUA scripts for 2 Yeelight Bulbs RGBWW based on the "
https://www.domoticz.com/wiki/Yeelight" page.
I used the Adding Scenes script example.
Both Yeelight LUA scripts appear to work however when I look in the Domoticz log page i see a lot of "red" messages:
2018-12-27 20:57:00.169 Error: EventSystem: in Eethoek Lamp: [string "--..."]:30: attempt to index global 'devicechanged' (a nil value)
2018-12-27 20:57:00.173 Error: EventSystem: in Hoek Lamp: [string "--..."]:30: attempt to index global 'devicechanged' (a nil value)
This the code example of "Hoek Lamp" where line 30 is "if devicechanged[DomDevice]=='Off' then -- turn off"
The other "Eethoek Lamp" has the exact same script only other IP with one extra scene added.
Code: Select all
commandArray = {}
DomDevice = 'Hoek Lamp';
IP = '192.168.2.57';
PORT = '55443'
if devicechanged[DomDevice]=='Off' then -- turn off
runcommandoff = "sudo echo -ne '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.."";
os.execute(runcommandoff);
-- print(runcommandoff)
elseif devicechanged[DomDevice]=='Normaal' then -- turn to Normaal mode
runcommandnormaal = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\", 2900, 2]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.."";
os.execute(runcommandnormaal);
-- print(runcommandnormaal)
elseif devicechanged[DomDevice]=='Groot' then -- turn to Groot mode
runcommandgroot = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"ct\", 3000, 100]}\\r\\n' | nc -w1 " ..IP.." " ..PORT.."";
os.execute(runcommandgroot);
-- print(runcommandgroot)
end
return commandArray
What is wrong with the script? The switches of both lamps appear to work?
Greetzzz,
Gerben
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Thursday 27 December 2018 22:18
by SweetPants
Can you change
if devicechanged[DomDevice]=='Off' then
to
if (devicechanged[DomDevice]=='Off') then
and all subsequent elseif lines also?
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Thursday 27 December 2018 22:43
by jvdz
Change the script to a "Devices" Script instead of "All" or else the devicechanged{} array isn't there for the other events.
Jos
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Thursday 27 December 2018 23:57
by gschmidt
SweetPants wrote: ↑Thursday 27 December 2018 22:18
Can you change
if devicechanged[DomDevice]=='Off' then
to
if (devicechanged[DomDevice]=='Off') then
and all subsequent elseif lines also?
No difference
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Friday 28 December 2018 0:00
by gschmidt
jvdz wrote: ↑Thursday 27 December 2018 22:43
Change the script to a "Devices" Script instead of "All" or else the devicechanged{} array isn't there for the other events.
Jos
How should I approach that?
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Friday 28 December 2018 6:12
by waaren
gschmidt wrote: ↑Friday 28 December 2018 0:00
jvdz wrote: ↑Thursday 27 December 2018 22:43
Change the script to a "Devices" Script instead of "All" or else the devicechanged{} array isn't there for the other events.
Jos
How should I approach that?
You now have created the Lua script as a type All. This means that it will be triggered on every device change in the system and also on every minute.
This is standard behaviour for Lua scripts in domoticz.
Now the script reacts correctly when you trigger it by changing the 'Hoek Lamp' device. In that scenario a pointer to the table devicechanged is passed from domoticz to your script; giving it all information required to process.
But when the script is triggered because of a minute has passed, this table-pointer is not passed from domoticz to your script with the result you see in the log; "attempt to index global 'devicechanged' (a nil value)" .
Easiest way to change the script from an "All"type to a "Device" type:
goto Events tab in domoticz and select your script and click somewhere in the edit window
copy complete content of your script to your clipboard. [ctrl A][ctrl C]
switch this "old" Lua to "Off" and save it. You should see "disabled" in red in front of the name now.
create a new Lua script using the
+ button on the Events tab and choose Lua -> Device (see the attached picture)
Click somewhere in the edit window
paste / overwrite complete content of your clipboard into the edit window. [ctrl A][ctrl V]
Make sure the new Lua is set to "On" and save it to a different name.
Look at your log for at least a minute to check if it had the expected result. (no more complaints about devicechanged table being nil)

- Lua saved as type All and as Device
- Lua.png (100.81 KiB) Viewed 5947 times
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Friday 28 December 2018 9:32
by gschmidt
Correctomundo, no errors anymore...Thanx!
When I looked at your screen shot I thought...hey that is not what my UI is showing....???
I tried the machinon and default theme in a firefox browser, but both UI's are not showing the correct menu in the "Events" page so I was missing some menu items. When I installed Google Chrome it showed the same UI as your screenshot.
It seems to be difficult in domoticz to get a good working UI theme tested on all browsers and devices (phones, tables, PC, etc.) is it?
Anyway thanx for your help!
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Friday 28 December 2018 10:17
by waaren
gschmidt wrote: ↑Friday 28 December 2018 9:32
When I looked at your screen shot I thought...hey that is not what my UI is showing....???
I tried the machinon and default theme in a firefox browser, but both UI's are not showing the correct menu in the "Events" page so I was missing some menu items. When I installed Google Chrome it showed the same UI as your screenshot.
Your welcome. Can you please show the difference between the browsers ? And after taking the screenshots, please try again after deleting the browser- and appcaches.
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Friday 28 December 2018 10:54
by gschmidt
Here you go! The Machinon Theme
2 screenshots of what happens when I select the "Events" Page in "Settings" in Domoticz on a Raspberry Pi
Quite different if you ask me

Re: attempt to index global 'devicechanged' (a nil value)
Posted: Friday 28 December 2018 11:07
by waaren
gschmidt wrote: ↑Friday 28 December 2018 10:54
Here you go! The Machinon Theme
2 screenshots of what happens when I select the "Events" Page in "Settings" in Domoticz on a Raspberry Pi
Quite different if you ask me
Yes they are different but if I look at this page with firefox I see something more like the chrome one. I guess this is because of some cache left behind.
Have a look at
https://support.mozilla.org/en-US/kb/storage and delete cache before trying again.

- Lua.png (143.75 KiB) Viewed 5924 times
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Friday 28 December 2018 11:23
by gschmidt
It gets more strange though:
When I connect to my duckdns site (either Android Phone or Laptop Chrome) I get an error that my browser cache refresh failed
I swithed a few hours ago from theme back to default because not all menu items were shown in the machinon theme, when I tried to follow the instructions from Jos
But in the local HTTP site I have no problems in Chrome.
Strange
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Friday 28 December 2018 11:35
by waaren
gschmidt wrote: ↑Friday 28 December 2018 11:23
It gets more strange though:
When I connect to my duckdns site (either Android Phone or Laptop Chrome) I get an error that my browser cache refresh failed
I swithed a few hours ago from theme back to default because not all menu items were shown in the machinon theme, when I tried to follow the instructions from Jos
But in the local HTTP site I have no problems in Chrome.
Strange
Indeed not what I expected but I don't know anything about duckdns. Maybe better to open a new topic for this in "other questions..." or a better suited one ?
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Friday 28 December 2018 11:37
by gschmidt
I switched theme back to default and back to machinon and now everything seems to be normal again in Android and Chrome...looks like it was a hickup
Now only try to solve the Firefox cache
Re: attempt to index global 'devicechanged' (a nil value)
Posted: Friday 28 December 2018 13:57
by gschmidt
And also firefox showing the correct page after deleting cookies and data in firefox options
So this topic is solved!