attempt to index global 'devicechanged' (a nil value) Topic is solved

Moderator: leecollings

Post Reply
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

attempt to index global 'devicechanged' (a nil value)

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

Re: attempt to index global 'devicechanged' (a nil value)

Post by SweetPants »

Can you change
if devicechanged[DomDevice]=='Off' then

to

if (devicechanged[DomDevice]=='Off') then

and all subsequent elseif lines also?
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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 saved as type All and as Device
Lua.png (100.81 KiB) Viewed 5945 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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 ;-)
Attachments
Domoticz Machinon-Firefox.JPG
Domoticz Machinon-Firefox.JPG (60.55 KiB) Viewed 5923 times
Domoticz Machinon-Chrome.JPG
Domoticz Machinon-Chrome.JPG (152.24 KiB) Viewed 5923 times
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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
Lua.png (143.75 KiB) Viewed 5922 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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
Attachments
Chrome and Android.jpg
Chrome and Android.jpg (103.88 KiB) Viewed 5918 times
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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 ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post 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
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: attempt to index global 'devicechanged' (a nil value)

Post by gschmidt »

And also firefox showing the correct page after deleting cookies and data in firefox options

So this topic is solved!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest