attempt to index local 'variable' (a number value)  [Solved]

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

Moderator: leecollings

Post Reply
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

attempt to index local 'variable' (a number value)

Post by imdos »

Code: Select all

--[[
wii charger if last seen more than 5 days ago, then turn on for 1 hour.
If wii is turned off; then turn on for 2 hours.
wii heet Wii en heeft IDX: 62
wii charger heet Wii-charger en heeft IDX: 63
--]]
return {
   on = {
      timer = { 'at 00:58' },
      -- wii heet Wii en heeft IDX: 62
      devices = { 'Wii' }
   },
             logging =   {
                        level   =   domoticz.LOG_ERROR,    -- change to LOG_ERROR when script executes OK
                        marker  =   wii_charger,
                    },
   execute = function(domoticz, item)
        local charger = domoticz.devices('Wii-charger')
        local wii = domoticz.devices('Wii')
        -- local wii = 62
      if (item.isTimer) then
			-- denkfout! De Wii-charger gaat elke nacht uit ivm scheduled task
			-- if (charger.lastUpdate.daysAgo > 4) then
			-- dit zou moeten werken
			if (wii.lastUpdate.daysAgo > 4) then
					charger.switchOn()
					-- force an update otherways it will trigger each following night.
					wii.switchOff().silent()
			end
      elseif (item.isDevice and not item.active) then
         -- do charge if (wii.state == 'Off') 
                charger.switchOn().forHour(2)
         -- end
      end
   end
}
This is the output from the IDX. It should start charging. But instead I get this error.

/var/log/domoticz/domoticz.log.11:2019-06-11 00:58:00.357 Status: dzVents: Error (2.4.22): An error occured when calling event handler wii
/var/log/domoticz/domoticz.log.11:2019-06-11 00:58:00.357 Status: dzVents: Error (2.4.22): /domoticz/scripts/dzVents/scripts/wii.lua:25: attempt to index local 'wii' (a number value)

line 25 is equal to: "if (wii.lastUpdate.daysAgo > 4) then"
However I don't see a difference with the second example underneath the link to the wiki.

Copied it here for a reference.

Code: Select all

         if (domoticz.devices('My PIR').lastUpdate.minutesAgo > 5) then
                domoticz.devices('Bathroom lights').switchOff()
            end

Code: Select all

curl "http://127.0.0.1:8080/json.htm?type=devices&rid=62"
{
   "ActTime" : 1560448733,
   "AstrTwilightEnd" : "00:00",
   "AstrTwilightStart" : "00:00",
   "CivTwilightEnd" : "22:49",
   "CivTwilightStart" : "04:31",
   "DayLength" : "16:41",
   "NautTwilightEnd" : "00:06",
   "NautTwilightStart" : "03:14",
   "ServerTime" : "2019-06-13 19:58:53",
   "SunAtSouth" : "13:40",
   "Sunrise" : "05:19",
   "Sunset" : "22:00",
   "app_version" : "4.10856",
   "result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 255,
         "CustomImage" : 0,
         "Data" : "Off",
         "Description" : "",
         "DimmerType" : "none",
         "Favorite" : 1,
         "HardwareID" : 4,
         "HardwareName" : "Dummy",
         "HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
         "HardwareTypeVal" : 15,
         "HaveDimmer" : true,
         "HaveGroupCmd" : true,
         "HaveTimeout" : false,
         "ID" : "00014129",
         "Image" : "Light",
         "IsSubDevice" : false,
         "LastUpdate" : "2019-05-24 17:21:07",
         "Level" : 0,
         "LevelInt" : 0,
         "MaxDimLevel" : 100,
         "Name" : "Wii",
         "Notifications" : "false",
         "PlanID" : "0",
         "PlanIDs" : [ 0 ],
         "Protected" : false,
         "ShowNotifications" : true,
         "SignalLevel" : "-",
         "Status" : "Off",
         "StrParam1" : "",
         "StrParam2" : "",
         "SubType" : "Switch",
         "SwitchType" : "On/Off",
         "SwitchTypeVal" : 0,
         "Timers" : "false",
         "Type" : "Light/Switch",
         "TypeImg" : "lightbulb",
         "Unit" : 1,
         "Used" : 1,
         "UsedByCamera" : false,
         "XOffset" : "0",
         "YOffset" : "0",
         "idx" : "62"
      }
   ],
   "status" : "OK",
   "title" : "Devices"
}
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: attempt to index local 'variable' (a number value)

Post by waaren »

imdos wrote: Thursday 13 June 2019 20:30 This is the output from the IDX. It should start charging. But instead I get this error.

/var/log/domoticz/domoticz.log.11:2019-06-11 00:58:00.357 Status: dzVents: Error (2.4.22): An error occured when calling event handler wii
/var/log/domoticz/domoticz.log.11:2019-06-11 00:58:00.357 Status: dzVents: Error (2.4.22): /domoticz/scripts/dzVents/scripts/wii.lua:25: attempt to index local 'wii' (a number value)
I don't see anything fundamentally wrong in your script. Only real issue is that you did not use quotes around the marker string.
Can you check this ?

Code: Select all

return 
{
    on = 
    {
        timer = { 'at 00:32' },
        devices = { 'Wii' }  
    },
   
    logging =   
    {
        level   =   domoticz.LOG_DEBUG,    -- change to LOG_ERROR when script executes OK
        marker  =   'wii charger',
    },
   
    execute = function(domoticz, item)
        local charger = domoticz.devices('Wii-charger')
        local wii = domoticz.devices('Wii')  -- local wii = 62
       
        if item.isTimer and wii.lastUpdate.daysAgo > 4  then
            charger.switchOn().checkFirst().forHour(2)
            wii.switchOff().silent()        -- force an update otherwise it will trigger each following night.
        elseif item.isDevice
            charger.switchOn().checkFirst().forHour(2)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: attempt to index local 'variable' (a number value)

Post by imdos »

Code: Select all

2019-06-14 10:20:00.554  Status: dzVents: Info:  wii charger: ------ Start external script: wii.lua:, trigger: at 10:20
2019-06-14 10:20:00.579  Status: dzVents: Debug: wii charger: Processing device-adapter for Wii-charger: Switch device adapter
2019-06-14 10:20:00.581  Status: dzVents: Debug: wii charger: Processing device-adapter for Wii: Switch device adapter
2019-06-14 10:20:00.581  Status: dzVents: Info:  wii charger: ------ Finished wii.lua
Hi waaren,

It worked out. Strange thing is that I must have changed something in between since I didn't get an error message, last night.
I first thought there is something wrong with your addition of the script since the wii is updated but it doesn't start charging.

But while inspecting the logs from yesterday:

Code: Select all

2019-06-14 00:58:00.343  (Dummy) Light/Switch (Wii-charger)
2019-06-14 00:58:00.354  (Dummy) Light/Switch (Wii)
2019-06-14 02:00:01.584  Status: Schedule item started! Name: Wii-charger, Type: On Time, DevID: 63, Time: 2019-06-14 02:00:01
2019-06-14 02:00:01.594  (Dummy) Light/Switch (Wii-charger)
And the IDX

Code: Select all

         "LastUpdate" : "2019-06-14 00:58:00",
         "Name" : "Wii",
"LastUpdate" : "2019-06-14 02:00:01",
"Name" : "Wii-charger",
It worked out perfectly.
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: attempt to index local 'variable' (a number value)

Post by imdos »

Have an update question about this code. I have too many false charges due to the sonoff which sends it's status. Therefore I modified my script to check whether a boolean has been set

Code: Select all

--[[
wii charger if last seen more than 5 days ago, then turn on for 1 hour.
If wii is turned off; then turn on for 2 hours.
wii heet Wii en heeft IDX: 62
wii charger heet Wii-charger en heeft IDX: 63
https://www.domoticz.com/forum/viewtopic.php?f=71&p=217143

https://www.domoticz.com/forum/viewtopic.php?f=59&t=28730&p=219525&hilit=Nintendo#p219525
--]]
return 
{
    on = 
    {
        timer = { 'at 00:58' },
        devices = { 62 } -- 'Wii' }  
    },
   
    logging =   
    {
        level   =   domoticz.LOG_DEBUG, 
--        level   =   domoticz.LOG_ERROR,    -- change to LOG_ERROR when script executes OK
        marker  =   'wii charger',
    },
   
    execute = function(domoticz, item)
        local charger = domoticz.devices(63) -- 'Wii-charger')
        -- local wii = domoticz.devices(62) -- 'Wii')  -- local wii = 62
        -- variabele om constant opladen te voorkomen.
        local wiion = false
        if (item.isTimer and wii.lastUpdate.daysAgo > 4 )  then
            charger.switchOn().checkFirst()
			    domoticz.log('Charging the Wii controllers periodically',domoticz.LOG_FORCE)
            wii.switchOff().silent()			-- force an update otherwise it will trigger each following night.
        elseif (item.isDevice and item.active) then
             wiion = true
        elseif (item.isDevice and wiion and not item.active) then
             wiion = false
            charger.switchOn().checkFirst().forHour(2)
			     domoticz.log('Charging the Wii controllers after usage',domoticz.LOG_FORCE)
	  end
    end
}
And the error

Code: Select all

2019-12-10 20:01:30.256  Status: dzVents: Debug: wii charger: Processing device-adapter for Wii-charger: Switch device adapter
2019-12-10 20:01:30.257  Error: dzVents: Error: (2.4.29) wii charger: An error occurred when calling event handler wii
2019-12-10 20:01:30.257  Error: dzVents: Error: (2.4.29) wii charger: /domoticz/scripts/dzVents/scripts/wii.lua:29: attempt to concatenate global 'wiion' (a nil value)
2019-12-10 20:01:30.257  Status: dzVents: Info: wii charger: ------ Finished wii.lua                                                                                
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: attempt to index local 'variable' (a number value)

Post by waaren »

imdos wrote: Tuesday 10 December 2019 20:20 Have an update question about this code. I have too many false charges due to the sonoff which sends it's status. Therefore I modified my script to check whether a boolean has been set
And the error

Code: Select all

2019-12-10 20:01:30.257  Error: dzVents: Error: (2.4.29) wii charger: /domoticz/scripts/dzVents/scripts/wii.lua:29: attempt to concatenate global 'wiion' (a nil value)
Are you sure you show us the same script that produced this error? Line 29 of this script cannot have this error because line 29 does not contain code that concatenates anything. (local wiion = false -- is not a concatenation)
You also commented the declaration of the wii as domoticz.devices(62)in line 27, but still try switching it off in line 33. That will not work

Please understand that the value of the wiion variable is not persistent over multiple executions of this script. No script variables are kept after the script finished. If you want to keep variable content over different runs of a script have a look at the persistent data section of the dzVents wiki
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: attempt to index local 'variable' (a number value)  [Solved]

Post by imdos »

waaren wrote: Tuesday 10 December 2019 20:49 Are you sure you show us the same script that produced this error? Line 29 of this script cannot have this error because line 29 does not contain code that concatenates anything. (local wiion = false -- is not a concatenation)
You also commented the declaration of the wii as domoticz.devices(62)in line 27, but still try switching it off in line 33. That will not work
You're absolutely correct. I had adjusted the script already a bit. Have fixed both items and did a data section; but I get no charging event started.
waaren wrote: Tuesday 10 December 2019 20:49 Please understand that the value of the wiion variable is not persistent over multiple executions of this script. No script variables are kept after the script finished. If you want to keep variable content over different runs of a script have a look at the persistent data section of the dzVents wiki
This is the current version, with the persistent data added.

Code: Select all

return
{
    on =
    {
        timer = { 'at 00:58' },
        devices = { 62 } -- 'Wii' }
    },
    data = { wiion = { initial = false } },
    logging =
    {
    --   level   =   domoticz.LOG_DEBUG,
       level   =   domoticz.LOG_ERROR,    -- change to LOG_ERROR when script executes OK
        marker  =   'wii charger',
    },

    execute = function(domoticz, item)
        local charger = domoticz.devices(63) -- 'Wii-charger')
        local wii = domoticz.devices(62) -- 'Wii')  -- local wii = 62
        -- variabele om constant opladen te voorkomen.
        local wiion = false
        if (item.isTimer and wii.lastUpdate.daysAgo > 4 )  then
            charger.switchOn().checkFirst()
            domoticz.log('Charging the Wii controllers periodically',domoticz.LOG_FORCE)
            wii.switchOff().silent()                    -- force an update otherwise it will trigger each following night.
        elseif (item.isDevice and item.active) then
             wiion = true
        elseif (item.isDevice and wiion and not item.active) then
             wiion = false
             charger.switchOn().checkFirst().forHour(2)
             domoticz.log('Charging the Wii controllers after usage',domoticz.LOG_FORCE)
        end
    end
}
But I found out that I had another issue, which is why I had to make the system foolproof. I get sudden reboots (and thus power restore settings) due to what appears a failed flash update of a Sonoff Basic.

But I guess I have found the error myself. I declare the value at the top (line 17), and on each run on line 29 I overwrite the setting with the same value. Thereby overwriting what would be set on line 34/35 which is the correct value.
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: attempt to index local 'variable' (a number value)

Post by imdos »

Damn; I thought I had solved my problem. But it still doesn't work exactly. I must be doing something wrong with the persistent data. Could someone have a look.

Code: Select all

--[[
wii charger if last seen more than 5 days ago, then turn on for 1 hour.
If wii is turned off; then turn on for 2 hours.
wii heet Wii en heeft IDX: 62
wii charger heet Wii-charger en heeft IDX: 63
https://www.domoticz.com/forum/viewtopic.php?f=71&p=217143
https://www.domoticz.com/forum/viewtopic.php?f=59&t=28730&p=219525&hilit=Nintendo#p219525       <-- variabele
https://www.domoticz.com/forum/viewtopic.php?f=59&t=28347&p=230959#p230959                      <-- error debugged
--]]
return
{
    on =
    {
        timer = { 'at 00:58' },
        devices = { 62 } -- 'Wii' }
    },
    data = { wiion = { initial = false } },
    logging =
    {
        level   =   domoticz.LOG_DEBUG,
--       level   =   domoticz.LOG_ERROR,    -- change to LOG_ERROR when script executes OK
        marker  =   'wii charger',
    },

    execute = function(domoticz, item)
        local charger = domoticz.devices(63) -- 'Wii-charger')
        local wii = domoticz.devices(62) -- 'Wii')  -- local wii = 62
        -- variabele om constant opladen te voorkomen.
        -- local wiion = false --'wordt nu in regel 17 gezet als persistent value.
        if (wiion == true ) then domoticz.log('wiion variabele is: True') else domoticz.log('wiion variabele is: False') end
        if (item.isTimer and wii.lastUpdate.daysAgo > 4 )  then
            charger.switchOn().checkFirst()
            domoticz.log('Charging the Wii controllers periodically',domoticz.LOG_FORCE)
            wii.switchOff().silent()                    -- force an update otherwise it will trigger each following night.
        elseif (item.isDevice and item.active) then
             wiion = true
                domoticz.log('wiion is waar')
        elseif (item.isDevice and wiion and not item.active) then
             wiion = false
             charger.switchOn().checkFirst().forHour(2)
             domoticz.log('Charging the Wii controllers after usage',domoticz.LOG_FORCE)
                domoticz.notify('Wii', 'Charging the Wii controllers after usage', domoticz.PRIORITY_NORMAL, domoticz.SOUND_NONE, nil,domoticz.NSS_PUSHOVER)
        end
    end
}
I have tested turning On and then Off and this produces the following log:

Code: Select all

2019-12-20 10:20:05.797  Status: User: Admin initiated a switch command (62/Wii/On)
2019-12-20 10:20:05.804  (Dummy) Light/Switch (Wii)
2019-12-20 10:20:05.938  Status: dzVents: Info: Handling events for: "Wii", value: "On"
2019-12-20 10:20:05.941  Status: dzVents: Info: wii charger: ------ Start external script: wii.lua: Device: "Wii (Dummy)", Index: 62
2019-12-20 10:20:05.942  Status: dzVents: Debug: wii charger: Processing device-adapter for Wii-charger: Switch device adapter
2019-12-20 10:20:05.943  Status: dzVents: Info: wii charger: wiion variabele is: False
2019-12-20 10:20:05.943  Status: dzVents: Info: wii charger: wiion is waar
2019-12-20 10:20:05.943  Status: dzVents: Info: wii charger: ------ Finished wii.lua
2019-12-20 10:20:38.971  Status: User: Admin initiated a switch command (62/Wii/Off)
2019-12-20 10:20:38.976  (Dummy) Light/Switch (Wii)
2019-12-20 10:20:39.102  Status: dzVents: Info: Handling events for: "Wii", value: "Off"
2019-12-20 10:20:39.104  Status: dzVents: Info: wii charger: ------ Start external script: wii.lua: Device: "Wii (Dummy)", Index: 62
2019-12-20 10:20:39.105  Status: dzVents: Debug: wii charger: Processing device-adapter for Wii-charger: Switch device adapter
2019-12-20 10:20:39.105  Status: dzVents: Info: wii charger: wiion variabele is: False
2019-12-20 10:20:39.106  Status: dzVents: Info: wii charger: ------ Finished wii.lua
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: attempt to index local 'variable' (a number value)

Post by waaren »

imdos wrote: Friday 20 December 2019 10:38 Damn; I thought I had solved my problem. But it still doesn't work exactly. I must be doing something wrong with the persistent data. Could someone have a look.
If you read from or write to persistent data you need to be aware that persistent variables only exist in domoticz.data. So in your case use domoticz.data.wiion

Code: Select all

--[[
wii charger if last seen more than 5 days ago, then turn on for 1 hour.
If wii is turned off; then turn on for 2 hours.
wii heet Wii en heeft IDX: 62
wii charger heet Wii-charger en heeft IDX: 63
https://www.domoticz.com/forum/viewtopic.php?f=71&p=217143
https://www.domoticz.com/forum/viewtopic.php?f=59&t=28730&p=219525&hilit=Nintendo#p219525       <-- variabele
https://www.domoticz.com/forum/viewtopic.php?f=59&t=28347&p=230959#p230959                      <-- error debugged
--]]

return
{
    on =
    {
        timer = { 'at 00:58' },
        devices = { 62 } -- 'Wii' }
    },
    
    data = { wiion = { initial = false } },
    
    logging =
    {
        level   =   domoticz.LOG_DEBUG,
--       level   =   domoticz.LOG_ERROR,    -- change to LOG_ERROR when script executes OK
        marker  =   'wii charger',
    },

    execute = function(domoticz, item)
        local charger = domoticz.devices(63) -- 'Wii-charger')
        local wii = domoticz.devices(62) -- 'Wii')  -- local wii = 62
        
        -- Persistent var om constant opladen te voorkomen.
        if domoticz.data.wiion == true then 
            domoticz.log('wiion variabele is: True') 
        else 
            domoticz.log('wiion variabele is: False') 
        end
        
        if (item.isTimer and wii.lastUpdate.daysAgo > 4 )  then
            charger.switchOn().checkFirst()
            domoticz.log('Charging the Wii controllers periodically',domoticz.LOG_FORCE)
            wii.switchOff().silent()                    -- force an update otherwise it will trigger each following night.
        elseif (item.isDevice and item.active) then
             domoticz.data.wiion = true
             domoticz.log('Persistent wiion set to true', domoticz.LOG_DEBUG)
        elseif (item.isDevice and domoticz.data.wiion == true and not(item.active)) then
             domoticz.data.wiion = false
             charger.switchOn().checkFirst().forHour(2)
             domoticz.log('Charging the Wii controllers after usage',domoticz.LOG_FORCE)
             domoticz.notify('Wii', 'Charging the Wii controllers after usage', domoticz.PRIORITY_NORMAL, domoticz.SOUND_NONE, nil,domoticz.NSS_PUSHOVER)
        end
    end
}
[/quote]
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: attempt to index local 'variable' (a number value)

Post by imdos »

Thanks as always waaren and happy Christmas!
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest