Page 1 of 1

Confused by variables - plz save a noob :/

Posted: Saturday 10 October 2020 18:20
by Sarcas
By trying to clean up my messy scripts, I messed them up even more I fear…

I think I am confused about local, global and persistent variables…

I thought it was practical to put the ids of the devices I often use in global_data, encouraging me to always use the same variable names etc.

So my global_data kinda looks like this:

Code: Select all

-- this scripts holds all the globally persistent variables and helper functions
-- see the documentation in the wiki
-- NOTE:
-- THERE CAN BE ONLY ONE global_data SCRIPT in your Domoticz install.

return {
	-- global persistent data
	data    = {
	    OWMForecast = {}
	    },

	helpers = {
-- SYSTEEM
-- systeem
		idxNachtstand                       = 153,

-- verwarming
		idxPompWoonkamer                    = 1328,
		idxPompKeuken                       = 255,
		idxRelaisVerwarming                 = 1333,  -- idxSchakelaarKeuken

-- weer
		idx_forecast_weather_6h             = 2287,  -- temperatuur over 6 uur

-- KAMERS
-- woonkamer
		idxBewegingWoonkamer                = 2500,
		idxLuxWoonkamer                     = 2502,
		idxTempWoonkamer                    = 1326,
		idxThermostaatWoonkamer             = 193,

		idxSchakelaarEettafel1x             = 2117,
		idxSchakelaarSalontafel1x           = 2164,

		idxLampEettafel                     = 2096,
		idxLampSalontafel                   = 2106,
		idxKleineStaandeLamp                = 2492,
		idxGroteStaandeLamp                 = 2493,
		
	-- global helper functions
		myStatusboard = function(domoticz, statusboard, statusboardCMD)
			-- code
			if statusboardCMD == 'CLR' then
				domoticz.devices(statusboard).updateText('')
			else
				domoticz.devices(statusboard).updateText(statusboardCMD)
			end
		end
	}
}
My scripts usually start with the declaration of the variables of the devices, like

Code: Select all

local idxLuxWoonkamer               = 2502
local idxBewegingWoonkamer          = 2500
local idxVentilatorSlaapkamer       = 212

local idxSchakelaarEettafel1x       = 2117
local idxSchakelaarSalontafel1x     = 2164

local idxLampEettafel               = 2096
local idxLampSalontafel             = 2106

return {
    on = {
        devices = {
            idxBewegingWoonkamer,
            idxSchakelaarEettafel1x,
            idxSchakelaarSalontafel1x
        }, -- end devices
        timer = {
            'every 12 minutes'
        } -- end timer
    }, -- end on
    
    et cetera
So I removed the often used local declarations and put them in global_data. And nothing worked anymore :D

Sooooo…. global and persistent variables aren’t the same thing I guess? Can someone please educate me, because I feel quite silly now.

Thx

Re: Confused by variables - plz save a noob :/

Posted: Saturday 10 October 2020 19:30
by waaren
Sarcas wrote: Saturday 10 October 2020 18:20 I think I am confused about local, global and persistent variables…
The variables that should be accessible and changeable by multiple scripts from global_data.lua must be defined in the data section like below.
Precede the name of the vars with domoticz.globalData. or dz.globalData.

Global accessible functions and Constants must be defined in the helpers section.
Precede the name of these with domoticz.helpers. or dz.helpers.

Also look at the example below and at the wiki chapter

Code: Select all

return 
{
    
        data = 
        {
                domoticzLocation                        = { initial = "Nederland" },
                openUVFilename                          = { initial = "scripts/dzVents/data/OpenUV.json" },
                delayedMessages                         = { initial = {}},

                hostname                                = { initial = "notset" },
                messageText,
        },

	helpers = 
    {
    
        hostname = function(dz)
            if dz.globalData.hostname ~= "notset" then return dz.globalData.hostname end
            local fileHandle     = assert(io.popen("hostname", 'r'))
            local commandOutput  = assert(fileHandle:read('*a'))
            fileHandle:close()
            dz.globalData.hostname = commandOutput
            return commandOutput
        end;
    }
}
 

Re: Confused by variables - plz save a noob :/

Posted: Friday 23 October 2020 18:20
by Sarcas
My global_data (see first post) is correct then, the constants are defined in the helpers section. Unfortunately my script seems to stop working although I do not get an error.

And this is the start of the script I struggle with:

Code: Select all

--  Dit script bestuurt het automatisch in- en uitschakelen van de verlichting in de groep woonkamer, afhankelijk van diverse variabelen
--  luxWoonkamer    - de hoeveelheid licht in de woonkamer
--  Nachtstand      - Zijn we naar bed of niet

-- local idxLuxWoonkamer           = 2502
-- local idxNachtstand             = 153

--  Pulseschakelaars
-- local lampEettafel              = 2096 -- #17
-- local schakelaarEettafel1x      = 2117
-- local schakelaarEettafel2x      = 2165

-- local lampSalontafel            = 2106 -- #18
-- local schakelaarSalontafel1x    = 2164
-- local schakelaarSalontafel2x    = 2124

return {
	on = {
		devices = {
			idxLichtenWoonkamer,
			idxBewegingWoonkamer,
			idxLuxWoonkamer,
			idxNachtstand,
			idxSchakelaarEettafel1x,
			idxSchakelaarEettafel2x,
			idxSchakelaarSalontafel1x,
			idxSchakelaarSalontafel2x
		}, -- end devices
		timer = {
			'every 5 minutes'
		} -- end timer
	}, -- end on

	data = {
		statusEettafel      = { initial = 'Auto' },
		statusSalontafel    = { initial = 'Auto' }
	}, -- end data

	logging = {
		level               = domoticz.LOG_DEBUG,
		marker              = "WOONKAMER"
	}, -- end logging



	execute = function(dz, device)
		-- variabelen uit 'global_data'
		local idxLuxWoonkamer               = dz.helpers.idxLuxWoonkamer
		local idxBewegingWoonkamer          = dz.helpers.idxBewegingWoonkamer
		local idxNachtstand                 = dz.helpers.idxNachtstand
		local idxLampEettafel               = dz.helpers.idxLampEettafel
		local idxSchakelaarEettafel1x       = dz.helpers.idxSchakelaarEettafel1x
		local idxSchakelaarEettafel2x       = dz.helpers.idxSchakelaarEettafel2x
		local idxLampSalontafel             = dz.helpers.idxLampSalontafel
		local idxSchakelaarSalontafel1x     = dz.helpers.idxSchakelaarSalontafel1x
		local idxSchakelaarSalontafel2x     = dz.helpers.idxSchakelaarSalontafel2x




		-- definieer de lokale variabelen
		local luxWoonkamer              = dz.devices(idxLuxWoonkamer).lux
		local nachtstandStand           = dz.devices(idxNachtstand).state
		local groupWoonkamerState       = dz.groups(idxLichtenWoonkamer).state
		local groupKeukenState          = dz.groups('Lichten keuken').state

		dz.log('••• idxLuxWoonkamer = ' .. idxLuxWoonkamer, dz.LOG_DEBUG )
		dz.log('••• luxWoonkamer = ' .. luxWoonkamer, dz.LOG_DEBUG )


		local luxThresholdBase          = 30
		local luxMinimal                = 5
		local hysteris                  = 24
		local minDimLevelPerc           = 50
		local maxDimLevelPerc           = 90
		local dimLevel
The problem is that none of my device triggers work. If I uncomment the first line (where they are defined locally) everything works fine. Do I have to ‘touch’ global_data first or something? global_data is already the top script btw, all other script names start with “z - “ like “z - verlichting” to make sure global_data is loaded first.

Re: Confused by variables - plz save a noob :/  [Solved]

Posted: Friday 23 October 2020 22:51
by waaren
Sarcas wrote: Friday 23 October 2020 18:20 My global_data (see first post) is correct then, the constants are defined in the helpers section. Unfortunately my script seems to stop working although I do not get an error.

The problem is that none of my device triggers work. If I uncomment the first line (where they are defined locally) everything works fine. Do I have to ‘touch’ global_data first or something? global_data is already the top script btw, all other script names start with “z - “ like “z - verlichting” to make sure global_data is loaded first.
You might want to look at this post It explains why this approach cannot work.

Re: Confused by variables - plz save a noob :/

Posted: Thursday 29 October 2020 15:56
by Sarcas
waaren wrote: Friday 23 October 2020 22:51 You might want to look at this post It explains why this approach cannot work.
Now that explains a lot. I wish the script would have give me an error or something.