Error: state is nil for item.level: nil

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

Moderator: leecollings

Post Reply
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Error: state is nil for item.level: nil

Post by HvdW »

Hi,
I am not able to find an example on Google to retrieve the cause of this error message.

Code: Select all

2024-11-20 18:35:00.651 Error: dzVents: Car charging on PV Power: Error: state is nil for item.level: nil
The script runs smooth but I want to get rid of this error message.
Checked and logged everything state and level, all OK
Bugs bug me.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Error: state is nil for item.level: nil

Post by waltervl »

I never have seen this error but it could be a misformed table of item.
Is item an domoticz object or a json from external?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Error: state is nil for item.level: nil

Post by HvdW »

The only item word in the script is triggeredItem that's why I am wondering.
I have put a lot of debug statements inside, all flawless.
As mentioned, a Google search didn't give me a clue where to search either.
Bugs bug me.
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Error: state is nil for item.level: nil

Post by Kedi »

Beter post the complete script.
Logic will get you from A to B. Imagination will take you everywhere.
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Error: state is nil for item.level: nil

Post by HvdW »

Well...

Code: Select all

return {
    on = {   
        timer = {'every 1 minutes', }, -- at daytime
    },

    logging = {
        level = domoticz.LOG_DEBUG,
        marker = 'Car charging on PV Power'
    },

    data = {
        desiredLevel = { initial = 'Off' },
        lastChangeTime = { initial = 0 },
    },

    execute = function(domoticz, triggeredItem)
        if triggeredItem then
        domoticz.log('#11111 triggeeredItem ', domoticz.LOG_DEBUG)

            
            end
        -- domoticz.devices('Charging Level').dump()
        local ChargingSwitchState = domoticz.devices('Charging Level').levelName
        local EVSwitch_16A = domoticz.devices('EVSE Switch 16A')
        local chargeLevelSet = domoticz.devices('XC40 battery charge set').level
	    local chargeLevelActual = domoticz.devices('XC40-ChargeLevel').nValue -- is een int en niet iets met een komma XC40-ChargeLevel
        local EVSE_Connected = domoticz.globalData.EVSE_Connected
        local newLevel = 'Off'
        
        -- Define power levels
        local powerLevels = {
            Off = 0,
            ['6A'] = 1400,
            ['8A'] = 1850,
            ['10A'] = 2300,
            ['13A'] = 3000,
            ['16A'] = 3700
        }
        
        -- Get power level based on charging switch state
        local PowerLaadpaal = powerLevels[ChargingSwitchState] or 0
        
        -- Get power data
        local PowerReturn = tonumber(domoticz.devices('Power').rawData[6]) -- Solar power generation
        local PowerConsumption = tonumber(domoticz.devices('Power').rawData[5]) -- Home power consumption
        local availableEnergy = PowerLaadpaal + PowerReturn - PowerConsumption

        -- Logging
        --domoticz.log('#0 EVSE connected: ' .. domoticz.globalData.EVSE_Connected, domoticz.LOG_DEBUG)
        domoticz.log('#1 Energy usage from the network: ' .. PowerConsumption, domoticz.LOG_DEBUG)
        domoticz.log('#2 Energy return to the network: ' .. PowerReturn, domoticz.LOG_DEBUG)
        domoticz.log('#3 Actual power on laadpaal: ' .. PowerLaadpaal, domoticz.LOG_DEBUG)
        domoticz.log('#4 Available energy: ' .. availableEnergy, domoticz.LOG_DEBUG)
        
        -- Check if SmartEVSE is OK
        if domoticz.globalData.EVSE_CommunicationError ~= 'None' then
           domoticz.notify('@1 There is an EVSE Communication Error', domoticz.PRIORITY_NORMAL)
           domoticz.devices('Auto laden').switchOff().forSec(30)
        end
        
        -- Determine the desired charging level
        local newLevel = 'Off'
        local currentLevel = domoticz.devices('Charging Level').state
        domoticz.log('###### currentLevel : ' .. currentLevel, domoticz.LOG_DEBUG)
        for level, power in pairs(powerLevels) do
            if availableEnergy >= power then
                newLevel = level
            end
        end
        domoticz.log('###### newLevel : ' .. newLevel, domoticz.LOG_DEBUG)
        
        -- Check if chargeLevelActual >= chargeLevelSet
        if chargeLevelActual >= chargeLevelSet and EVSE_Connected == 'Connected' then
            domoticz.devices('Charging Level').switchSelector('Off')
            domoticz.log('#5 Set to: ' .. newLevel, domoticz.LOG_DEBUG)
        -- Check if chargeLevelActual < chargeLevelSet
        elseif chargeLevelActual < chargeLevelSet and EVSE_Connected == 'Connected' and EVSwitch_16A.state == 'Off' then
            local currentTime = os.time()
            if availableEnergy < 900 then  -- 1400 - 500 = 900
                newLevel = 'Off'
            end
            domoticz.devices('Charging Level').switchSelector(newLevel)
            domoticz.log('#5 Set to: ' .. newLevel, domoticz.LOG_DEBUG)
            
            -- Notify charging state
            -- It sends the notification one time when the level has changed
            local current_time = os.date('%d-%m-%Y %H:%M')
            local subject, message
            if newLevel == 'Off' and (currentLevel ~= newLevel) then 
                subject = '@2 Charging on PV power has ended.'
                message = 'Charging Off, Energy : ' .. availableEnergy .. '\nTime : ' .. current_time
                if domoticz.LOG_DEBUG then
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
                    domoticz.log('#6 Notification sent: ' .. subject, domoticz.LOG_DEBUG)
                end
            elseif newLevel ~= 'Off'and (currentLevel ~= newLevel) then
                subject = '@3 We are charging on PV power.'
                message = 'Charging ' .. newLevel .. ', Energy : ' .. availableEnergy .. '\nTime : ' .. current_time
                if domoticz.LOG_DEBUG then
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
                    domoticz.log('#7 Notification sent: ' .. subject, domoticz.LOG_DEBUG)
                end
            end
            domoticz.log('#7 Car charging on PV Power END of test loop', domoticz.LOG_DEBUG)
        end
    end
}
Bugs bug me.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Error: state is nil for item.level: nil

Post by jvdz »

Guess it has to be this line?:

Code: Select all

local chargeLevelSet = domoticz.devices('XC40 battery charge set').level
Check the devicename and type whether it really has field level.
To make sure that is the one, you could change the line to:

Code: Select all

local chargeLevelSet = domoticz.devices('XC40 battery charge set').level or '??'
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Error: state is nil for item.level: nil

Post by HvdW »

Code: Select all

local chargeLevelSet = 82 
gives the same result
Here's the full log

Code: Select all

 2024-11-21 13:03:00.161 dzVents: Car charging on PV Power: ------ Start internal script: Car charging on PV Power:, trigger: "every 1 minutes"
2024-11-21 13:03:00.162 dzVents: Debug: Car charging on PV Power: #11111 triggeeredItem
2024-11-21 13:03:00.164 dzVents: Debug: Car charging on PV Power: Processing device-adapter for Charging Level: Switch device adapter
2024-11-21 13:03:00.164 dzVents: Debug: Car charging on PV Power: Processing device-adapter for EVSE Switch 16A: Switch device adapter
2024-11-21 13:03:00.164 dzVents: Debug: Car charging on PV Power: #1 Energy usage from the network: 0
2024-11-21 13:03:00.165 dzVents: Debug: Car charging on PV Power: #2 Energy return to the network: 760
2024-11-21 13:03:00.165 dzVents: Debug: Car charging on PV Power: #3 Actual power on laadpaal: 0
2024-11-21 13:03:00.165 dzVents: Debug: Car charging on PV Power: #4 Available energy: 760
2024-11-21 13:03:00.165 dzVents: Debug: Car charging on PV Power: ###### currentLevel : Off
2024-11-21 13:03:00.165 dzVents: Debug: Car charging on PV Power: ###### newLevel : Off
2024-11-21 13:03:00.165 dzVents: Debug: Car charging on PV Power: Constructed timed-command: Set Level 0
2024-11-21 13:03:00.165 dzVents: Debug: Car charging on PV Power: #5 Set to: Off
2024-11-21 13:03:00.165 dzVents: Debug: Car charging on PV Power: #7 Car charging on PV Power END of test loop
2024-11-21 13:03:00.167 dzVents: Car charging on PV Power: ------ Finished Car charging on PV Power
2024-11-21 13:03:00.175 Error: dzVents: Car charging on PV Power: Error: state is nil for item.level: nil
2024-11-21 13:03:00.245 Status: EventSystem: Script event triggered: /home/hein/domoticz/dzVents/runtime/dzVents.lua
2024-11-21 13:03:00.284 Status: EventSystem: reset all device statuses...
2024-11-21 13:03:00.372 Dummy: Light/Switch (Charging Level)
2024-11-21 13:03:00.446 dzVents: Handling events for: "Charging Level", value: "Off"
2024-11-21 13:03:00.452 Error: dzVents: Car charging on PV Power: Error: state is nil for item.level: nil 
The nil message is at 13:03:00.175 and at 13:03:00.452 in the same run.
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest