global_data creating global constants

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

Moderator: leecollings

Post Reply
User avatar
Ragdag
Posts: 125
Joined: Friday 30 March 2018 13:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

global_data creating global constants

Post by Ragdag »

I'm trying to improve my pushover global helper that I have in global_data.

I have this in my global_data,

Code: Select all

-- Global Data and Helper Functions Configuration
return {
    -- Global persistent data
    data = {
        lastStateUserGPS = {initial = ''}, 
    },

    -- Global constants and configurations
    constants = {
        PUSHOVER = {
            -- User IDs
            USERS = {
                FAMILIE = '1',
            },
            -- Application IDs
            APPS = {
                HUIS = '1',
                TEST = '1',
                WAS = '1',
                DOMOTICZ = '1',
                ALARM = '1',
                SERVER = '1',
            },
            -- Priority levels
            PRIORITY = {
                LOWEST = -2, 
                LOW = -1, 
                NORMAL = 0, 
                HIGH = 1, 
                EMERGENCY = 2
            },
            -- All available Pushover sounds
            SOUNDS = {
                -- Default sound
                PUSHOVER = 'pushover', -- Default Pushover tone

                -- Standard sounds
                BIKE = 'bike',
                BUGLE = 'bugle',
                CASHREGISTER = 'cashregister',
                CLASSICAL = 'classical',
                COSMIC = 'cosmic',
                FALLING = 'falling',
                GAMELAN = 'gamelan',
                INCOMING = 'incoming',
                INTERMISSION = 'intermission',
                MAGIC = 'magic',
                MECHANICAL = 'mechanical',
                PIANOBAR = 'pianobar',
                SIREN = 'siren',
                SPACEALARM = 'spacealarm',
                TUGBOAT = 'tugboat',

                -- Long sounds
                ALIEN = 'alien', -- Alien Alarm (long)
                CLIMB = 'climb', -- Climb (long)
                PERSISTENT = 'persistent', -- Persistent (long)
                ECHO = 'echo', -- Pushover Echo (long)
                UPDOWN = 'updown', -- Up Down (long)

                -- Special
                VIBRATE = 'vibrate', -- Vibrate Only
                NONE = 'none', -- Silent

                -- Custom sounds
                CLEARANNOUNCETONES = 'clear-announce-tones',
                LIGHTSWITCH = 'light-switch',
            },
            -- Default values
            DEFAULTS = {
                PRIORITY = 0,
                SOUND = 'pushover', -- Changed default to standard Pushover sound
                TITLE = 'Domoticz Notification',
                TTL = nil,
            },
            -- Common TTL presets (in seconds)
            TTL = {
                ONE_HOUR = 3600,
                FOUR_HOURS = 14400,
                EIGHT_HOURS = 28800,
                ONE_DAY = 86400,
                THREE_DAYS = 259200,
                ONE_WEEK = 604800,
            },
        },
    },

    -- Global helper functions
    helpers = {
        -- Enhanced pushover function with TTL support
        pushover = function(domoticz, scriptVar, PushoverUser, PushoverApp, title, message, priority, sound, ttl)
            -- Input validation
            assert(PushoverUser, 'PushoverUser is required')
            assert(PushoverApp, 'PushoverApp is required')
            assert(message, 'Message is required')

            -- TTL validation when provided
            if ttl then
                assert(type(ttl) == 'number' and ttl > 0, 'TTL must be a positive number of seconds')
                -- TTL is ignored for emergency priority
                if priority == domoticz.constants.PUSHOVER.PRIORITY.EMERGENCY then
                    domoticz.log('TTL is ignored for emergency priority messages', domoticz.LOG_DEBUG)
                    ttl = nil
                end
            end

            -- Logging
            domoticz.log(
                    string.format(
                            'Sending Pushover notification - User: %s, App: %s, Title: %s, Sound: %s, TTL: %s',
                            PushoverUser, PushoverApp, title or 'No title',
                            sound or domoticz.constants.PUSHOVER.DEFAULTS.SOUND, ttl or 'No TTL'
                    ), domoticz.LOG_DEBUG
            )

            -- Prepare post data
            local postData = {
                token = PushoverApp,
                user = PushoverUser,
                message = message,
                title = title or domoticz.constants.PUSHOVER.DEFAULTS.TITLE,
                priority = priority or domoticz.constants.PUSHOVER.DEFAULTS.PRIORITY,
                sound = sound or domoticz.constants.PUSHOVER.DEFAULTS.SOUND,
            }

            -- Add TTL if specified
            if ttl then
                postData.ttl = ttl
            end

            -- Send notification
            domoticz.openURL(
                    {
                        url = 'https://api.pushover.net/1/messages.json',
                        method = 'POST',
                        callback = scriptVar,
                        postData = postData,
                    }
            )
        end,
    },
}
but I'm not able to call these in a script
If I do:

Code: Select all

domoticz.log('Test: ' .. domoticz.globalData.constants.PUSHOVER.USERS.FAMILIE, domoticz.LOG_DEBUG)
I get:

Code: Select all

2025-01-15 12:39:44.023 Error: dzVents: Test_Pushover: ...ticz/scripts/dzVents/generated_scripts/Test_Pushover.lua:20: attempt to index a nil value (field 'constants')
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: global_data creating global constants

Post by boum »

the globalData table is the table set as `data` in your lua file. Move your constants table there (with `lastStateUserGPS`)
or put your PUSHOVER table inside helpers and access it with `domoticz.helpers.PUSHOVER.USERS.FAMILIE`
User avatar
Ragdag
Posts: 125
Joined: Friday 30 March 2018 13:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: global_data creating global constants

Post by Ragdag »

Thanks.
So like this

Code: Select all

-- Global Data and Helper Functions Configuration
return {
    -- Global persistent data
    data = {
        lastStateuserGPS = {initial = ''}
    },

    -- Global helper functions
    helpers = {
        PUSHOVER = {
            -- User IDs
            USERS = {
                FAMILIE = '1',
            },
            -- Application IDs
            APPS = {
                HUIS = '1',
                TEST = '1',
                WAS = '1',
                DOMOTICZ = '1',
                ALARM = '1',
                SERVER = '1',
            },
            -- Priority levels
            PRIORITY = {
                LOWEST = -2, 
                LOW = -1, 
                NORMAL = 0, 
                HIGH = 1, 
                EMERGENCY = 2
            },
            -- All available Pushover sounds
            SOUNDS = {
                -- Default sound
                PUSHOVER = 'pushover', -- Default Pushover tone

                -- Standard sounds
                BIKE = 'bike',
                BUGLE = 'bugle',
                CASHREGISTER = 'cashregister',
                CLASSICAL = 'classical',
                COSMIC = 'cosmic',
                FALLING = 'falling',
                GAMELAN = 'gamelan',
                INCOMING = 'incoming',
                INTERMISSION = 'intermission',
                MAGIC = 'magic',
                MECHANICAL = 'mechanical',
                PIANOBAR = 'pianobar',
                SIREN = 'siren',
                SPACEALARM = 'spacealarm',
                TUGBOAT = 'tugboat',

                -- Long sounds
                ALIEN = 'alien', -- Alien Alarm (long)
                CLIMB = 'climb', -- Climb (long)
                PERSISTENT = 'persistent', -- Persistent (long)
                ECHO = 'echo', -- Pushover Echo (long)
                UPDOWN = 'updown', -- Up Down (long)

                -- Special
                VIBRATE = 'vibrate', -- Vibrate Only
                NONE = 'none', -- Silent

                -- Custom sounds
                CLEARANNOUNCETONES = 'clear-announce-tones',
                LIGHTSWITCH = 'light-switch',
            },
            -- Default values
            DEFAULTS = {
                PRIORITY = 0,
                SOUND = 'pushover', -- Changed default to standard Pushover sound
                TITLE = 'Domoticz Notification',
                TTL = nil,
            },
            -- Common TTL presets (in seconds)
            TTL = {
                TEN_SECONDS = 10,
                ONE_MINUTE = 60,
                ONE_HOUR = 3600,
                FOUR_HOURS = 14400,
                EIGHT_HOURS = 28800,
                ONE_DAY = 86400,
                THREE_DAYS = 259200,
                ONE_WEEK = 604800,
            },
        },

        -- Enhanced pushover function with TTL support
        pushover = function(domoticz, scriptVar, PushoverUser, PushoverApp, title, message, priority, sound, ttl)
            -- Input validation
            assert(PushoverUser, 'PushoverUser is required')
            assert(PushoverApp, 'PushoverApp is required')
            assert(message, 'Message is required')

            -- Use defaults if not provided
            title = title or domoticz.helpers.PUSHOVER.DEFAULTS.TITLE
            priority = priority or domoticz.helpers.PUSHOVER.DEFAULTS.PRIORITY
            sound = sound or domoticz.helpers.PUSHOVER.DEFAULTS.SOUND

            -- TTL validation when provided
            if ttl then
                assert(type(ttl) == 'number' and ttl > 0, 'TTL must be a positive number of seconds')
                -- TTL is ignored for emergency priority
                if priority == domoticz.helpers.PUSHOVER.PRIORITY.EMERGENCY then
                    domoticz.log('TTL is ignored for emergency priority messages', domoticz.LOG_DEBUG)
                    ttl = nil
                end
            end

            -- Logging
            domoticz.log(
                    string.format(
                            'Sending Pushover notification - User: %s, App: %s, Title: %s, Sound: %s, TTL: %s',
                            PushoverUser, PushoverApp, title or 'No title',
                            sound or domoticz.helpers.PUSHOVER.DEFAULTS.SOUND, ttl or 'No TTL'
                    ), domoticz.LOG_DEBUG
            )

            -- Prepare post data
            local postData = {
                token = PushoverApp,
                user = PushoverUser,
                message = message,
                title = title or domoticz.helpers.PUSHOVER.DEFAULTS.TITLE,
                priority = priority or domoticz.helpers.PUSHOVER.DEFAULTS.PRIORITY,
                sound = sound or domoticz.helpers.PUSHOVER.DEFAULTS.SOUND,
            }

            -- Add TTL if specified
            if ttl then
                postData.ttl = ttl
            end

            -- Send notification
            domoticz.openURL(
                    {
                        url = 'https://api.pushover.net/1/messages.json',
                        method = 'POST',
                        postData = postData,
                        callback = function(response)
                            if response.isError then
                                domoticz.log(
                                    'Failed to send Pushover notification: ' .. response.data, domoticz.LOG_ERROR
                                )
                            else
                                domoticz.log('Pushover notification sent successfully!', domoticz.LOG_INFO)
                            end
                        end,
                    }
            )
        end,
    },
}

Post Reply

Who is online

Users browsing this forum: paul402 and 0 guests