script produces error [Solved]

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

Moderator: leecollings

Post Reply
jkimmel
Posts: 129
Joined: Monday 25 November 2013 17:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Mallorca
Contact:

script produces error [Solved]

Post by jkimmel »

I switched from Raspi to Windows. The scripts were fine under Buster

Here is the script

Code: Select all

-- assumptions:
-- the setpoint is set by a selector dummy device where the values are numeric temperatures
-- but you can easily change it to a setpoint device

local BOILER_DEVICE = 'FB 23 Heizkreis Buero' -- switch device
local SETPOINT_DEVICE = 'Solltemperatur Buero' -- selector dummy device
local TEMPERATURE_SENSOR = 'Buero'
local MODUS_DEVICE = 'Modus Waermepumpe' -- selector dummy device
local LOGGING = false

return 
{
    on = 
    {
        timer = 
        {
            'every 3 minutes'
        },
    },

    logging =   
    {
        level = LOGGING and domoticz.LOG_DEBUG or domoticz.LOG_ERROR or domoticz.LOG_INFO,
        marker = 'HZ Buero', 
    },
    active = true,
    execute = function(domoticz)

        local boiler = domoticz.devices(BOILER_DEVICE)

        local temperature = domoticz.devices(TEMPERATURE_SENSOR).temperature
        local setpoint = domoticz.devices(SETPOINT_DEVICE).levelName
        local modusState = domoticz.devices(MODUS_DEVICE).state

        local boilerState = boiler.state
        local setpointValue = tonumber(setpoint)

        domoticz.log('Current temperature: ' .. temperature, domoticz.LOG_DEBUG)
        domoticz.log('Setpoint: ' .. setpointValue, domoticz.LOG_DEBUG)
        domoticz.log('Current boiler state: ' .. boilerState, domoticz.LOG_DEBUG)

        -- now determine what to do
       if setpoint == 'Off' or modusState ~= 'Heizen' then
             boiler.switchOff()
        elseif temperature > setpointValue and boilerState == 'On' then
            boiler.switchOff()
        elseif temperature <= setpointValue and boilerState == 'Off' and modusState == 'Heizen'  then
            boiler.switchOn()
        end
        
         if temperature > setpointValue then
            domoticz.log('Target temperature reached, boiler off', domoticz.LOG_INFO)
        else
            domoticz.log('Target temperature not reached, boiler on', domoticz.LOG_INFO)
        end
    end
}
producing the error:

Code: Select all

2020-05-01 12:09:00.400 Error: dzVents: Error: (3.0.2) HZ Buero: An error occurred when calling event handler Klima Buero
2020-05-01 12:09:00.400 Error: dzVents: Error: (3.0.2) HZ Buero: ...oticz\scripts\dzVents\generated_scripts/Klima Buero.lua:39: attempt to concatenate a nil value (local 'setpointValue')
Last edited by jkimmel on Friday 01 May 2020 19:33, edited 2 times in total.
Rfxcom
Raspi 4
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Two similar scripts one produces error the other not

Post by waaren »

jkimmel wrote: Friday 01 May 2020 12:13 -- the setpoint is set by a selector dummy device where the values are numeric temperatures
It seems that the setpoint (string) cannot be converted to a number.
can you change line 39 to

Code: Select all

        domoticz.log('Setpoint: ' .. tostring(setpoint), domoticz.LOG_DEBUG)
and show the result?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jkimmel
Posts: 129
Joined: Monday 25 November 2013 17:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Mallorca
Contact:

Re: script produces error

Post by jkimmel »

Now this:

Code: Select all

2020-05-01 13:03:00.383 Error: dzVents: Error: (3.0.2) HZ Buero: An error occurred when calling event handler Klima Buero
2020-05-01 13:03:00.383 Error: dzVents: Error: (3.0.2) HZ Buero: ...oticz\scripts\dzVents\generated_scripts/Klima Buero.lua:51: attempt to compare nil with number
It seems to me handling of setpointvalue is different running Windows, because under Buster no problems
Rfxcom
Raspi 4
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: script produces error

Post by waaren »

jkimmel wrote: Friday 01 May 2020 13:04 Now this:

Code: Select all

2020-05-01 13:03:00.383 Error: dzVents: Error: (3.0.2) HZ Buero: An error occurred when calling event handler Klima Buero
2020-05-01 13:03:00.383 Error: dzVents: Error: (3.0.2) HZ Buero: ...oticz\scripts\dzVents\generated_scripts/Klima Buero.lua:51: attempt to compare nil with number
It seems to me handling of setpointvalue is different running Windows, because under Buster no problems
You need to set the level of logging to debug to see what is happening.

So change this part

Code: Select all

    logging =   
    {
        level = LOGGING and domoticz.LOG_DEBUG or domoticz.LOG_ERROR or domoticz.LOG_INFO,
        marker = 'HZ Buero', 
    },
to

Code: Select all

    logging =   
    {
       -- level = LOGGING and domoticz.LOG_DEBUG or domoticz.LOG_ERROR or domoticz.LOG_INFO,
        level = domoticz.LOG_DEBUG,
        marker = 'HZ Buero', 
    },
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jkimmel
Posts: 129
Joined: Monday 25 November 2013 17:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Mallorca
Contact:

Re: script produces error

Post by jkimmel »

Status

Code: Select all

2020-05-01 14:06:00.222 Status: dzVents: Info: HZ Buero: ------ Start internal script: Klima Buero:, trigger: "every 3 minutes"
2020-05-01 14:06:00.232 Status: dzVents: Debug: HZ Buero: Processing device-adapter for FB 23 Heizkreis Buero: Switch device adapter
2020-05-01 14:06:00.232 Status: dzVents: Debug: HZ Buero: Processing device-adapter for Buero: Temperature+humidity device adapter
2020-05-01 14:06:00.232 Status: dzVents: Debug: HZ Buero: Processing device-adapter for Solltemperatur Buero: Switch device adapter
2020-05-01 14:06:00.232 Status: dzVents: Debug: HZ Buero: Processing device-adapter for Modus Waermepumpe: Switch device adapter
2020-05-01 14:06:00.232 Status: dzVents: Debug: HZ Buero: Current temperature: 23.39999961853
2020-05-01 14:06:00.240 Status: dzVents: Debug: HZ Buero: Setpoint: 25
2020-05-01 14:06:00.240 Status: dzVents: Debug: HZ Buero: Current boiler state: Off
2020-05-01 14:06:00.240 Status: dzVents: Debug: HZ Buero: Constructed timed-command: On
2020-05-01 14:06:00.240 Status: dzVents: Info: HZ Buero: Target temperature not reached, boiler on
2020-05-01 14:06:00.240 Status: dzVents: Info: HZ Buero: ------ Finished Klima Buero
2020-05-01 14:06:00.240 Status: dzVents: Debug: Commands sent to Domoticz:
2020-05-01 14:06:00.240 Status: dzVents: Debug: - FB 23 Heizkreis Buero = On
2020-05-01 14:06:00.240 Status: dzVents: Debug: 
Error:

Code: Select all

2020-05-01 14:03:00.211 Error: dzVents: Error: (3.0.2) HZ Buero: An error occurred when calling event handler Klima Buero
2020-05-01 14:03:00.211 Error: dzVents: Error: (3.0.2) HZ Buero: ...oticz\scripts\dzVents\generated_scripts/Klima Buero.lua:52: attempt to compare nil with number
Rfxcom
Raspi 4
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: script produces error

Post by waaren »

jkimmel wrote: Friday 01 May 2020 14:13

Code: Select all

Error: (3.0.2) HZ Buero: ...oticz\scripts\dzVents\generated_scripts/Klima Buero.lua:52: attempt to compare nil with number
The script uses a mix of string and number without checking these. You need to add an additional check for that.

Can you try this?

Code: Select all

-- assumptions:
-- the setpoint is set by a selector dummy device where the values are numeric temperatures
-- but you can easily change it to a setpoint device

local BOILER_DEVICE = 'FB 23 Heizkreis Buero' -- switch device
local SETPOINT_DEVICE = 'Solltemperatur Buero' -- selector dummy device
local TEMPERATURE_SENSOR = 'Buero'
local MODUS_DEVICE = 'Modus Waermepumpe' -- selector dummy device
local LOGGING = false

return 
{
    on = 
    {
        timer = 
        {
            'every 3 minutes'
        },
    },

    logging =   
    {
        -- level = LOGGING and domoticz.LOG_DEBUG or domoticz.LOG_ERROR or domoticz.LOG_INFO,
        level = domoticz.LOG_DEBUG,
        marker = 'HZ Buero', 
    },
    active = true,
    execute = function(domoticz)

        local boiler = domoticz.devices(BOILER_DEVICE)

        local temperature = domoticz.devices(TEMPERATURE_SENSOR).temperature
        local setpoint = domoticz.devices(SETPOINT_DEVICE).levelName
        local modusState = domoticz.devices(MODUS_DEVICE).state

        local boilerState = boiler.state
        local setpointValue = tonumber(setpoint) or -99  -- set it to -99 when setpoint is a string that cannot be converted to a number)  

        domoticz.log('Current temperature: ' .. temperature, domoticz.LOG_DEBUG)
        domoticz.log('Setpoint: ' .. setpointValue, domoticz.LOG_DEBUG)
        domoticz.log('Current boiler state: ' .. boilerState, domoticz.LOG_DEBUG)

       -- now determine what to do
        if setpoint == 'Off' or modusState ~= 'Heizen' then
             boiler.switchOff()
             return
        end
        
        if setpointValue ~= -99 then
            if temperature > setpointValue and boilerState == 'On' then
                boiler.switchOff()
            elseif temperature <= setpointValue and boilerState == 'Off' and modusState == 'Heizen'  then
                boiler.switchOn()
            end
            
             if temperature > setpointValue then
                domoticz.log('Target temperature reached, boiler off', domoticz.LOG_INFO)
            else
                domoticz.log('Target temperature not reached, boiler on', domoticz.LOG_INFO)
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jkimmel
Posts: 129
Joined: Monday 25 November 2013 17:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Mallorca
Contact:

Re: script produces error

Post by jkimmel »

Answer as quick as ever.
Many thanks
Rfxcom
Raspi 4
HvdW
Posts: 663
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: script produces error [Solved]

Post by HvdW »

Hi,
I'm following this thread with interest.
Can you explain what is:

Code: Select all

local MODUS_DEVICE = 'Modus Waermepumpe' -- selector dummy device
Is it to know if the Boiler_device is on or off?
If so can you explain how to create a dummy device that shows the state?
Bugs bug me.
jkimmel
Posts: 129
Joined: Monday 25 November 2013 17:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Mallorca
Contact:

Re: script produces error [Solved]

Post by jkimmel »

stboiler
HvdW wrote: Saturday 02 May 2020 0:52 Hi,
I'm following this thread with interest.
Can you explain what is:

Code: Select all

local MODUS_DEVICE = 'Modus Waermepumpe' -- selector dummy device
Is it to know if the Boiler_device is on or off?
If so can you explain how to create a dummy device that shows the state?
In my case it is a selector which can be changed from "off" to heating or cooling. Nothing to do with the boiler.

Code: Select all

local boilerState = boiler.state
is what you're looking for
Rfxcom
Raspi 4
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest