global_data question

Easy to use, 100% Lua-based event scripting framework.
Post Reply
poudenes
Posts: 513
Joined: Wednesday 08 March 2017 10:42
Target OS: Linux
Domoticz version: 3.8993
Location: Amsterdam
Contact:

global_data question

Post by poudenes » Monday 09 April 2018 9:08

Hi All,

I have a friend who is gonna use Domoticz as well. We have global_data for some things.

I can use this code on my RPi without errors:

Code: Select all

on = {
	    devices = {[SceneGoodNight] = { 'at nighttime' }},
	},
On his RPi i get a error:

Table is NIL

My RPI runs with latest Beta Domoticz on Rasbian. His RPi runs same Beta Domoticz on OSMC OS.

global_data

Code: Select all

local Version       = '18.04.08'

SceneEvening        = 11
SceneGoodNight      = 12
SceneMovie          = 15
SceneDayTime        = 13
LivingRoomStatus    = 45
iPhone        = 10
KodiStatus          = 50
                        
return {
    helpers = {
        
        TurnOnScene = function(domoticz,idx)
            domoticz.devices(idx).switchOn().checkFirst()
        end,

        TurnOffScene = function(domoticz,idx)
            domoticz.devices(idx).switchOff().checkFirst() 
        end,
        
        On = function(domoticz,idx,dim)
            domoticz.devices(idx).switchOn().checkFirst()
            domoticz.devices(idx).dimTo(dim)
        end,
        
        Off = function(domoticz,idx)
            domoticz.devices(idx).switchOff().checkFirst()
        end
  
    }
}
Script:

Code: Select all

local Version = '18.04.08'
local results

return {
    active = true,
    on = {
        devices = {SceneGoodNight},
    },
    logging = {marker = 'SCENE GOODNIGHT ' ..Version..'........'},
    execute = function(domoticz, device)

     if (domoticz.devices(SceneGoodNight).active) then

        domoticz.helpers.TurnOffScene(domoticz,SceneEvening)
        domoticz.helpers.TurnOffScene(domoticz,SceneDaytime)
        domoticz.helpers.TurnOffScene(domoticz,SceneMovie)
        domoticz.helpers.Off(domoticz,LivingRoomStatus)

        domoticz.log('--------------------==<[ Goodnight is activated ]>==-----------------', domoticz.LOG_FORCE)
        os.execute('/home/osmc/domoticz/scripts/Peter/speech.sh "The scene goodnight is activated"&')
        
    end
end
}
RPi3 with OSMC - RPi3 with Domoticz - Dashticz - MiLight - Nanolead Aurora - KaKu - Logitech Hamony - Nest - RFLink

User avatar
waaren
Posts: 807
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: global_data question

Post by waaren » Monday 09 April 2018 14:45

Can you please both switch on debugging for dzVents for a moment while executing these scripts ? Maybe that will give just enough information to see what the problem is.
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

dannybloe
Posts: 1461
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: global_data question

Post by dannybloe » Monday 09 April 2018 15:38

Yes and post the piece of the log with the errors. Which table is nil? And do you both use the same script sources (filesystem or GUI editor)?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

dannybloe
Posts: 1461
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: global_data question

Post by dannybloe » Tuesday 10 April 2018 7:55

Looking at the dzVents code I don't believe it is currently guaranteed that global_data.lua is loaded before all other modules. So perhaps in your system it is by coincidence but on your friend's it isn't. In that case the event-script is loaded earlier and there is no SceneGoodNight global available yet.
I never anticipated this kind of usage of global_data but it is a good idea to try to load global_data at the beginning if it exists. I'll add it to my todo list.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

poudenes
Posts: 513
Joined: Wednesday 08 March 2017 10:42
Target OS: Linux
Domoticz version: 3.8993
Location: Amsterdam
Contact:

Re: global_data question

Post by poudenes » Tuesday 10 April 2018 10:40

Hi ALl,

hmm, ok. Sounds logic. It will be nice when global_data is loaded first. When you use lot of devices you can put them in global_data and not have to assign in every script you use. Thats why i did it this way.

The error code we get is:

2018-04-10 10:36:33.469 dzVents: Error (2.4.4): ...oticz/scripts/dzVents/generated_scripts/SceneGoodNight.lua:7: table index is nil

When i remove the = {'at nighttime'} part the script is working. And it has to read the device name also right?

I will revert back to old way and put all devices in every script.
RPi3 with OSMC - RPi3 with Domoticz - Dashticz - MiLight - Nanolead Aurora - KaKu - Logitech Hamony - Nest - RFLink

dannybloe
Posts: 1461
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: global_data question

Post by dannybloe » Tuesday 10 April 2018 10:50

You can also create a Lua module with the device-ids in there and require that at the top of you script.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

poudenes
Posts: 513
Joined: Wednesday 08 March 2017 10:42
Target OS: Linux
Domoticz version: 3.8993
Location: Amsterdam
Contact:

Re: global_data question

Post by poudenes » Wednesday 11 April 2018 22:21

Thanks. That seems to work for the other RPi. But what is the difference. Modules will always load first and helpers will called when needed?
Thats how i see it now...
RPi3 with OSMC - RPi3 with Domoticz - Dashticz - MiLight - Nanolead Aurora - KaKu - Logitech Hamony - Nest - RFLink

dannybloe
Posts: 1461
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: global_data question

Post by dannybloe » Monday 23 April 2018 7:31

Well, the helpers are part of the global_data. At some point during the bootstrapping of dzVents all the scripts are loaded and the order is just not defined (i.e. explicitly programmed) so on some systems global_data.lua may be loaded after some of the other scripts. So if global_data defines some global variables they will only be available to other scripts after global_data.lua has been loaded. So, I just gotta make sure that global_data.lua is loaded before all other modules.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests