DZvents script gives error 'attempt to index field 'devices' (a function value)'

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Dirk
Posts: 35
Joined: Wednesday 04 November 2015 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

DZvents script gives error 'attempt to index field 'devices' (a function value)'

Post by Dirk »

I tried to write a very simple script in dzEvents, using the web interface of Domoticz.
My scripts writes a message to the log ánd turns on a switch (the same switch that triggered the action, but that is irrelevant as far as I can tell).

Code: Select all

return {
    active = true,
    on = {
        devices = {
            85 -- this is a switch for a lamp
        }
    },
    execute = function(domoticz, roomSwitch)
        if (roomSwitch.state == 'On') then
            domoticz.log('Switch 85 has been turned on') -- write a message to the log
            domoticz.devices[85].switchOn() -- switch a device on (the same device, actually)
        end
    end
}
Switching on switch 85 results in this log:

Code: Select all

2017-09-30 20:44:42.881 dzVents: Info: Handling events for: "Woonkamer schemerlamp (K1)", value: "On"
2017-09-30 20:44:42.881 dzVents: Info: ------ Start internal script: dztest: Device: "Woonkamer schemerlamp (K1) (RFXCOM zender)", Index: 85
2017-09-30 20:44:42.881 dzVents: Info: Switch 85 has been turned on
2017-09-30 20:44:42.881 Error: dzVents: Error: An error occured when calling event handler dztest
2017-09-30 20:44:42.881 Error: dzVents: Error: ...pi/domoticz/scripts/dzVents/generated_scripts/dztest.lua:11: attempt to index field 'devices' (a function value)
2017-09-30 20:44:42.881 dzVents: Info: ------ Finished dztest
Since I copy-pasted the exact code from the manual, I have no clue why it gives an error. I tried replacing the number with the exact name between quotes, but this results in the same error.
If I try switching another devices too, or another call like 'domoticz.variables['anotherVar'].set(15)'.

Probably I'm overlooking something very obvious. Help would be greatly appreciated.

(Sidenote: What I actually intend to do is have every 'on-command' for a switch trigger a burst of 5 on-commands (with a small interval), so I can be quite sure that my switches actually receive the signal. Currently I do that my placing the switch in a Group, and adding the same switch to that group with delay 0, 1, 2, 3, 4.)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: DZvents script gives error 'attempt to index field 'devices' (a function value)'

Post by dannybloe »

Make sure you use the current version of the documentation in the wiki. You have to access a device like this: domoticz.devices(85)
Notice de (). Since 2.0 the devices collection is a function.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Dirk
Posts: 35
Joined: Wednesday 04 November 2015 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DZvents script gives error 'attempt to index field 'devices' (a function value)'

Post by Dirk »

I thought I was using the newest manual. In a forumpost I found I also saw a 'newer version' mentioned, but when I searched I found the manual I was already using... Or maybe I just didn't spot the differences

For others: this seems to be the most recent for DZvents: http://www.domoticz.com/wiki/DzVents:_n ... _scripting
This one is outdated: https://github.com/dannybloe/dzVents. I now see that it has a message at the beginning of the readme: 'dzVents in this repository is no longer maintained! The documentation below is for 1.1.x.' My bad...

Thanks Dannybloe for helping me back on track! (I haven't tried the solution yet but I'm confident having the correct instructions will help me a lot ;) . UPDATE: it works, thanks! Note that te autosuggest in the editor will still use the old syntax, like after_sec instead of the new syntax afterSec.)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest