dzVents script made by ChatGpt throws error

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

Moderator: leecollings

Post Reply
BartSr
Posts: 489
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

dzVents script made by ChatGpt throws error

Post 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')
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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?
User avatar
waltervl
Posts: 6677
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waltervl
Posts: 6677
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
BartSr
Posts: 489
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
User avatar
madpatrick
Posts: 758
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
BartSr
Posts: 489
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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).
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
User avatar
waltervl
Posts: 6677
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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 ;)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
madpatrick
Posts: 758
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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)
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
BartSr
Posts: 489
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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.
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
User avatar
madpatrick
Posts: 758
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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)
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
FlyingDomotic
Posts: 463
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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
BartSr
Posts: 489
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

Re: dzVents script made by ChatGpt throws error

Post by BartSr »

Makes me curious about those 'amazing things'.
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
FlyingDomotic
Posts: 463
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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
User avatar
waltervl
Posts: 6677
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: dzVents script made by ChatGpt throws error

Post by waltervl »

But again for this simple thing to switch off a light after 60 minutes you do not need to script.....
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
BartSr
Posts: 489
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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
}
Last edited by BartSr on Monday 27 October 2025 8:31, edited 1 time in total.
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
User avatar
waltervl
Posts: 6677
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
BartSr
Posts: 489
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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.
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
User avatar
waltervl
Posts: 6677
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: dzVents script made by ChatGpt throws error

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest