nil is supposed to be false in conditionals but errors in dzVents

Moderator: leecollings

Post Reply
User avatar
user4563
Posts: 96
Joined: Tuesday 19 October 2021 17:24
Target OS: Windows
Domoticz version: 2023.2
Contact:

nil is supposed to be false in conditionals but errors in dzVents

Post by user4563 »

In conditionals in Lua I read nil is considered false: https://www.tutorialspoint.com/lua/lua_ ... making.htm

For example if this sensor battery level is nil then this should be false and not fire:

Code: Select all

        if domoticz.devices('Garage side door sensor').batteryLevel < 100 then
                        
        -- do something
        end
Instead it errors out:

Code: Select all

2023-07-13 19:00:00.304 Error: dzVents: Error: (3.1.8) An error occurred when calling event handler Low battery notification
2023-07-13 19:00:00.304 Error: dzVents: Error: (3.1.8) ...s\dzVents\generated_scripts/Low battery notification.lua:61: attempt to compare nil with number


I did workaround it:

Code: Select all

        if domoticz.devices('Garage side door sensor').batteryLevel ~= nil and domoticz.devices('Garage side door sensor').batteryLevel < 100 then
                        
        --do something
        end
I noticed that dzVents / Lua is somewhat lagging Lua itself so I'm not sure if that is the issue?

https://www.lua.org/versions.html#5.4
Domoticz 2023.2 running as a service on Windows 10 w/ Aeotec Z-Stick Gen5+, HAProxy (remote access)
Domoticz for Android
Domoticz Scenes add-on in Kodi
User avatar
habahabahaba
Posts: 190
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: nil is supposed to be false in conditionals but errors in dzVents

Post by habahabahaba »

Not all sensors supports .batteryLevel
Have few motion sensors with such problem.
Check documentation on sensor
plugge

Re: nil is supposed to be false in conditionals but errors in dzVents

Post by plugge »

user4563 wrote: Friday 14 July 2023 2:48 In conditionals in Lua I read nil is considered false: https://www.tutorialspoint.com/lua/lua_ ... making.htm

For example if this sensor battery level is nil then this should be false and not fire:
Wrong assumption: you are not evaluating the sensor battery level, but are comparing nil with a number. That raises -correctly- an error.
Read the Lua manuals and try it yourself in Lua.
There's a diffence between:
1. evaluates correctly

Code: Select all

local a = nil
if a then print('ok') else print('not ok') end
and:
2. raises an error

Code: Select all

local a = nil
if a < 10 then print('ok') else print(not ok') end
Try it.
User avatar
user4563
Posts: 96
Joined: Tuesday 19 October 2021 17:24
Target OS: Windows
Domoticz version: 2023.2
Contact:

Re: nil is supposed to be false in conditionals but errors in dzVents

Post by user4563 »

I see what you mean. I assumed nil is false regardless but it's actually only like a boolean false.

Thank you for the information!
Domoticz 2023.2 running as a service on Windows 10 w/ Aeotec Z-Stick Gen5+, HAProxy (remote access)
Domoticz for Android
Domoticz Scenes add-on in Kodi
User avatar
boum
Posts: 130
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: nil is supposed to be false in conditionals but errors in dzVents

Post by boum »

i don't see your point. it would not work anyway. Neither nil nor false are automagically converted to numbers.

Code: Select all

> print( false > 17 )
input:1: attempt to compare number with boolean
User avatar
user4563
Posts: 96
Joined: Tuesday 19 October 2021 17:24
Target OS: Windows
Domoticz version: 2023.2
Contact:

Re: nil is supposed to be false in conditionals but errors in dzVents

Post by user4563 »

"Lua programming language assumes any combination of Boolean true and non-nil values as true, and if it is either boolean false or nil, then it is assumed as false value. It is to be noted that in Lua, zero will be considered as true."
Domoticz 2023.2 running as a service on Windows 10 w/ Aeotec Z-Stick Gen5+, HAProxy (remote access)
Domoticz for Android
Domoticz Scenes add-on in Kodi
User avatar
boum
Posts: 130
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: nil is supposed to be false in conditionals but errors in dzVents

Post by boum »

Yes. It works only one way. All values are implicitly converted to boolean. Most convert to "true".
But boolean and nil values are not converted to numbers. They cannot be compared to number values without error.
User avatar
user4563
Posts: 96
Joined: Tuesday 19 October 2021 17:24
Target OS: Windows
Domoticz version: 2023.2
Contact:

Re: nil is supposed to be false in conditionals but errors in dzVents

Post by user4563 »

Thank you everyone for your knowledge!
Domoticz 2023.2 running as a service on Windows 10 w/ Aeotec Z-Stick Gen5+, HAProxy (remote access)
Domoticz for Android
Domoticz Scenes add-on in Kodi
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest