Data not persistent?  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Data not persistent?

Post by FearNaBoinne »

I am trying to write a script that toggles between different scenes, and wanted to use a persistent variable, but it doesn't seem to work...

Code: Select all

return {
    active = {
        true  -- either true or false, or you can specify a function
    },
    
    on = {
        devices = {
            'Eettafel switch'
        }
    },
    data = {
        Scene_number = { initial = 0 }
    },	
    
    execute = function(domoticz, triggeredItem, info)
        local Device  = domoticz.devices('Group Eettafel')
        local Device1 = domoticz.devices('Eettafel voor')
        local Device2 = domoticz.devices('Eettafel mid voor')
        local Device3 = domoticz.devices('Eettafel mid achter')
        local Device4 = domoticz.devices('Eettafel achter')
        
        -- domoticz.log('Current Scene is '..Scene_number, domoticz.LOG_INFO)
        
        if (triggeredItem.state == 'Off') then
            domoticz.log('I am seeing this Off command...', domoticz.LOG_INFO)
            Device.switchOff()
            Scene_number = 0
        elseif (triggeredItem.state == 'On') then
            domoticz.log('I am seeing this On command...', domoticz.LOG_INFO)
            if (Scene_number == 0) then -- starting from Off, going Scene_Normal
                domoticz.log('Off to Normal', domoticz.LOG_INFO)
                Device.dimTo(50)
                Device.setKelvin(68)
                Scene_number = 1
            elseif (Scene_number == 1) then -- starting from Scene_Normal, going Scene_Daglicht
                domoticz.log('Normal to Daglicht', domoticz.LOG_INFO)
                Device.dimTo(100)
                Device.setKelvin(8)
                Scene_number = 2
            elseif (Scene_number == 2) then -- starting from Scene_Daglicht, going Scene_Avondlichtje
                domoticz.log('Daglicht to Avond', domoticz.LOG_INFO)
                Device4.dimTo(10)
                Device4.setKelvin(75)
                Device3.dimTo(10)
                Device3.setKelvin(75)
                Device2.switchOff()
                Device1.switchOff()
                Scene_number = 3
            elseif (Scene_number == 3) then -- starting from Scene_Avondlichtje, going Scene_Eetlichtje
                domoticz.log('Avond to Eet', domoticz.LOG_INFO)
                Device.dimTo(5)
                Device.setKelvin(90)
                Scene_number = 4
            elseif (Scene_number == 4) then -- starting from Scene_Eetlichtje, going Scene_Normal
                domoticz.log('Eet to Normal', domoticz.LOG_INFO)
                Device.dimTo(50)
                Device.setKelvin(68)
                Scene_number = 1
            end
        end
        
        domoticz.log('The light ' .. Device.name .. ' is switched to Scene number '..Scene_number, domoticz.LOG_INFO)
    end
}
If I run this from a switch 'On', I get an error:
dzVents: Error (2.4.19): .../domoticz/scripts/dzVents/generated_scripts/Eettafel.lua:62: attempt to concatenate global 'Scene_number' (a nil value)

If I uncomment the first log command, the error changed slightly: The number 62 changed to 22 (the line number of the first log command)

It also doesn't hit any of the Scene_number based [else]if statements.

All this shows me that Scene_number is not initialized nor persistent... What am I missing?..
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Data not persistent?

Post by waaren »

FearNaBoinne wrote: Monday 01 July 2019 0:29 I am trying to write a script that toggles between different scenes, and wanted to use a persistent variable, but it doesn't seem to work...
If I run this from a switch 'On', I get an error:
All this shows me that Scene_number is not initialized nor persistent... What am I missing?..
dzVents persistent variables are keys in the table domoticz.data
You have to address them as such, so in this case use

Code: Select all

domoticz.data.Scene_number
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: Data not persistent?

Post by FearNaBoinne »

waaren wrote: Monday 01 July 2019 0:44 dzVents persistent variables are keys in the table domoticz.data
You have to address them as such, so in this case use

Code: Select all

domoticz.data.Scene_number
Thanks! I totally missed that bit! :shock:

I will modify and test!
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: Data not persistent?  [Solved]

Post by FearNaBoinne »

Totally works! I looked at that segment in the wiki several times and missed that bit every time... (Unfortunately I have no clue how to make it even more clear for the "selectively blind'... :oops: )
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest