Page 1 of 1

dzVents script made by ChatGpt throws error

Posted: Thursday 23 October 2025 11:42
by BartSr
ChatGpt is a great help for creating scripts.
I asked for a dzVents scripts which turns off lights once a determined time has been exceeded.
This was given. But once I tried the error as shown down below popped up. Any idea what goes wrong and how to solve?

Code: Select all

-- Configuratie: Lijst van lampen en hun respectieve tijdslimieten (in seconden)
-- applied lamps with value (seconds) after which light goes off by this script since light was switched on
local lampen_tijdslimieten = {
    [1823] = 3600, -- Lamp met IDX 123 heeft een tijdslimiet van 1 uur (3600 seconden)
    [1829] = 7200,  -- Lamp met IDX 124 heeft een tijdslimiet van 2 uur (7200 seconden)

  
}

return {
    on = {
        timer = {'every 1 minutes'}  -- Het script wordt elke minuut uitgevoerd
    },
    
    execute = function(domoticz)
        -- Loop door alle lampen in de lijst
        for lamp_idx, tijdslimiet in pairs(lampen_tijdslimieten) do
            local lamp = domoticz.devices(lamp_idx)

            -- Controleer of de lamp aanstaat
            if lamp and lamp.state == 'On' then
                -- Bereken hoe lang de lamp al aanstaat via 'lastUpdate'
 -->>>>>>> hereunder the line with error
                local tijd_aan = os.time() - lamp.lastUpdate.timestamp
                -- Als de lamp langer dan de tijdslimiet aanstaat, zet deze uit
                if tijd_aan > tijdslimiet then
                    print("De lamp " .. lamp.name .. " staat al langer dan de tijdslimiet (" .. tijdslimiet / 60 .. " minuten) aan. Zet de lamp uit.")
                    lamp.switchOff()
                end
            end
        end
    end
}
thrown error:

Code: Select all

2025-10-23 11:07:00.489 Error: dzVents: An error occurred when calling event handler Script #2
2025-10-23 11:07:00.490 Error: dzVents: ...domoticz/scripts/dzVents/generated_scripts/Script #2.lua:24: attempt to index a nil value (field 'lastChange')

Re: dzVents script made by ChatGpt throws error

Posted: Thursday 23 October 2025 13:16
by jvdz
The error says "lastChanged" and the script contains "lastUpdate", so does the error belong to this version of the script?
This was given. But once I tried the error as shown down below popped up. Any idea what goes wrong and how to solve?
PS: Is it fair to assume you are trying to learn how to do this yourself in the future?

Re: dzVents script made by ChatGpt throws error

Posted: Thursday 23 October 2025 13:20
by waltervl
I think you should ask ChatGPT why the script throws this error and how to solve it :)
Script does not make sense as LastUpdate is not necessarily the time it was switched on. Especially when you have other scripting switching the light on without checking its status.

Did you use the lastChange value in another script or global helper?
viewtopic.php?p=267199&hilit=lastChange#p267199

Re: dzVents script made by ChatGpt throws error

Posted: Thursday 23 October 2025 13:29
by waltervl
By the way, Domoticz has a build in function for the automatic switch off after x seconds, so no need to script this.
https://wiki.domoticz.com/Managing_Devices#On_Off_Delay
The Off delay on switch device causes a device to switch Off with a delay of the set amount of seconds after the device switched to On.

Re: dzVents script made by ChatGpt throws error

Posted: Thursday 23 October 2025 14:17
by BartSr
thanks to both of you.
Indeed I asked chatgpt about the error but the answer did not made sense.
I looked for a solution but it's very hard to find the information in the wiki.
I know about device.idx, device.name, device.switchon. etc. But 'device.lastupdate' I couldnot find.
@ Walter. Thanks for the link. I'll invest it and hopefully it might help me.
Oke, I checked the info from the link but as far as I can see this does not make sense to solve my problem.
The reason why I like to have this script:
For simpicity and testing I took two lights but for final use it concerns a bathroomfan and alleyway light.
Then fan is controlled by the script given as example jn domoticz wiki. The allyway light is controlled bg the universal LUA exemple with motion detector.
For some reason I can't figure out somtimes for both auto switch off fails. So I looked for a solution to auto switch off the devices after xx seconds the device state changed lasttime. Hopefully this makes sense.
TIA
Bart

Re: dzVents script made by ChatGpt throws error

Posted: Thursday 23 October 2025 15:07
by madpatrick
Asked ChatGPT :o

Code: Select all

Ah, ik zie meteen waar het probleem zit. De error:

attempt to index a nil value (field 'lastChange')


komt doordat je probeert lamp.lastUpdate.timestamp te gebruiken, terwijl het veld waarschijnlijk niet bestaat in de versie van Domoticz / dzVents die je gebruikt. In dzVents heet het veld meestal lastUpdate of lastUpdate.minutesAgo, en sommige oudere versies gebruiken lastChange.

Je probeert dit te doen:

local tijd_aan = os.time() - lamp.lastUpdate.timestamp


Maar lamp.lastUpdate is nil, of lamp.lastUpdate.timestamp bestaat niet. Daarom krijg je de error.
You must give everytime feedback to ChatGPT and eventually it gives you a good script.
i've tried it also a couple of time, but it needs sometimes a few sessions

Re: dzVents script made by ChatGpt throws error

Posted: Thursday 23 October 2025 15:48
by BartSr
Hi Madpatrick,
This is what I did already but without getting a real solution. And yes I know you might ask several times (when I asked first time, ChatGpt kept returning a LUA script several times until I referred to something like 'lastseen or lastupdate' (can't remember).

Re: dzVents script made by ChatGpt throws error

Posted: Thursday 23 October 2025 19:05
by waltervl
So you modify your script and do something like

Code: Select all

if myDevice.lastUpdate.minutesAgo > 60 then 
	myDevice.switchOff() 
end
Or do not script and use the build in delay off function ;)

Re: dzVents script made by ChatGpt throws error

Posted: Thursday 23 October 2025 19:09
by madpatrick
Try this

Code: Select all

               local tijd_aan = os.time() - lamp.lastUpdate.timestamp
To

Code: Select all

                local tijd_aan = os.time() - lamp.lastUpdate.minutesago
You are trying to calculate with a delay in minutes and a timestamp (current time)

Re: dzVents script made by ChatGpt throws error

Posted: Thursday 23 October 2025 23:32
by BartSr
That's what I tried but it results in switching off the light 1 minute after switch on. But I thought os.time() returns the number of seconds since 1jan1970 wheras xxxx.minutesago returns minutes since last update.

Re: dzVents script made by ChatGpt throws error

Posted: Friday 24 October 2025 7:18
by madpatrick
This setting is with every number even with 1 minute

Code: Select all

if tijd_aan > tijdslimiet then
Maybe this works

Code: Select all

lamp.switchOff().afterSec(tijdslimiet)

Re: dzVents script made by ChatGpt throws error

Posted: Friday 24 October 2025 10:15
by FlyingDomotic
When using timed commands, it's a good idea to cancel all pending actions before asking for a new one:

Code: Select all

lamp.cancelQueuedCommands()
lamp.switchOff().afterSec(tijdslimiet)
Not canceling pending actions leads to amazing things :D

Re: dzVents script made by ChatGpt throws error

Posted: Friday 24 October 2025 10:29
by BartSr
Makes me curious about those 'amazing things'.

Re: dzVents script made by ChatGpt throws error

Posted: Friday 24 October 2025 11:53
by FlyingDomotic
Mainly, if you don't cancel queued events, they stay in queue and are executed when due...

When you have multiple events that are time related, it's just impossible to restart them properly.

Let's imagine you turn a lamp on (by a PIR) at 00:00 for 3 minutes, so you'll queue an "off" at 00:03.

Then at 00:02, you're triggered again by the PIR, as you're still moving in room. This will queue an "Off" at 00:08.

But at 00:03 (meaning only one minute later), the lamp will be turned off by the "off" at 00:03.

More amazing things happens when you mix switchOn() and switchOff() and after/for :D

Re: dzVents script made by ChatGpt throws error

Posted: Friday 24 October 2025 13:09
by waltervl
But again for this simple thing to switch off a light after 60 minutes you do not need to script.....

Re: dzVents script made by ChatGpt throws error

Posted: Sunday 26 October 2025 19:36
by BartSr
@Flying Domotic, thanks for clarification which I do understand. Once I asked ChatGpt again for a script with same functionality as before next was returned. I adapted yr advice and it's up and running.

Code: Select all

=====================================================
-- Script: auto_off.lua
-- Doel : Schakelt apparaten uit als ze langer dan X min aan staan
-- Auteur: ChatGPT
-- =====================================================

return {
    on = {
        timer = { 'every minute' }   -- controleer elke minuut
    },

    data = {
        lastStateChange = { initial = {} }
    },

    execute = function(domoticz)

        -- Instellingen: apparaten en maximale "aan"-tijd (in minuten)
        local devices = {
            ["Staande lamp in serre"] = 30,
            ["Staande lamp bij kast"] =245,
            ["Staande lamp bij klok"] = 60
        }

        local now = os.time()

        for name, maxOnMinutes in pairs(devices) do
            local device = domoticz.devices(name)
            if device then
                if device.state == 'On' then
                    -- Hoe lang staat hij al aan?
                    local lastChange = device.lastUpdate.secondsAgo / 60

                    if lastChange > maxOnMinutes then
                        domoticz.log(name .. ' staat al ' .. math.floor(lastChange) ..
                                     ' minuten aan → wordt uitgeschakeld', domoticz.LOG_INFO)
                        device.cancelQueuedCommands()
                        device.switchOff()
                    end
                end
            else
                domoticz.log('⚠️ Apparaat "' .. name .. '" niet gevonden in Domoticz', domoticz.LOG_ERROR)
            end
        end
    end
}

Re: dzVents script made by ChatGpt throws error

Posted: Sunday 26 October 2025 23:46
by waltervl
Proves again chatcpt cannot program in dzvents. And probably more AI environment have this issue as dzvents is a very niche programming language.

Better stop sharing this as it only makes AI worse as your program will train AI.

Re: dzVents script made by ChatGpt throws error

Posted: Monday 27 October 2025 8:36
by BartSr
Walter, you underestimate AI.
I often asked chatgpt for a script. Problem as described here is first time for me I faced a problem. And yes, of course you have to check.
Also AI has good knowledge of linux. I would say give it a try for yourself.

Re: dzVents script made by ChatGpt throws error

Posted: Monday 27 October 2025 8:45
by waltervl
I am not underestimating AI, I use it myself too for example Python, a well known programming language.
There are lots of example in this forum where AI decided to create a fake dzvents function. It seems AI is not trained enough on dzvents. And that is no wonder as there is only limited examples on the internet.