NaN comparison

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

Moderator: leecollings

Post Reply
iganin
Posts: 47
Joined: Tuesday 28 October 2014 17:55
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Aalter, Belgium
Contact:

NaN comparison

Post by iganin »

I have got a LUX sensor which is show "nan" in Domoticz when it's too dark.
The idea is to switch the light when the value is below 5 or equal to "nan".
I'm having difficulties to detect when the sensor value is "nan".
Can some help me with the script?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: NaN comparison

Post by waaren »

[EDIT] 20190301 08:17 changed state to rawData[1]
Check this and if not yet giving the results needed send output of dump() command via PM to help me understand what the idea behind nan of NaN is.

Code: Select all

return {
            on =    {
                        devices     =   { 
                                            79              -- change to your Lux device idx
                                        }
                    },
                
      ogging    =   {   
                        level       =  domoticz.LOG_DEBUG,   
                        marker      =  "Lux 2 light" 
                    },    

    execute = function(dz, item)
        local light = dz.devices(1488)      -- change to idx you want to switch on / off
        
        if tonumber(item.rawData[1]) < 5 or tostring(item.rawData[1]) == "nan" then         
            light.switchOn().checkFirst()
            dz.log("On: " .. item.state,dz.LOG_FORCE)
        else
            light.switchOff().checkFirst()
            dz.log("Off: " .. item.state,dz.LOG_FORCE)
        end
       --  item.dump()           -- This will dump all relevant information from your lux device to log. Can be removed when not needed anymore
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
iganin
Posts: 47
Joined: Tuesday 28 October 2014 17:55
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Aalter, Belgium
Contact:

Re: NaN comparison

Post by iganin »

Getting error:

2019-03-01 18:45:27.390 Status: dzVents: Info: Handling events for: "Corridor Lux", value: "-nan"
2019-03-01 18:45:27.431 Status: dzVents: Info: ------ Start internal script: Light 1nd floor Test forum: Device: "Corridor Lux (Main)", Index: 536
2019-03-01 18:45:27.432 Status: dzVents: Error (2.4.7): An error occured when calling event handler Light 1nd floor Test forum
2019-03-01 18:45:27.432 Status: dzVents: Error (2.4.7): ...dzVents/generated_scripts/Light 1nd floor Test forum.lua:16: attempt to compare nil with number
2019-03-01 18:45:27.433 Status: dzVents: Info: ------ Finished Light 1nd floor Test forum

-------------------------------------------------------------------------------------
Forgot to mention: I2C sensor TSL2561 Illuminance
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: NaN comparison

Post by waaren »

iganin wrote: Friday 01 March 2019 18:46 Getting error:

2019-03-01 18:45:27.390 Status: dzVents: Info: Handling events for: "Corridor Lux", value: "-nan"
2019-03-01 18:45:27.431 Status: dzVents: Info: ------ Start internal script: Light 1nd floor Test forum: Device: "Corridor Lux (Main)", Index: 536
2019-03-01 18:45:27.432 Status: dzVents: Error (2.4.7): An error occured when calling event handler Light 1nd floor Test forum
2019-03-01 18:45:27.432 Status: dzVents: Error (2.4.7): ...dzVents/generated_scripts/Light 1nd floor Test forum.lua:16: attempt to compare nil with number
2019-03-01 18:45:27.433 Status: dzVents: Info: ------ Finished Light 1nd floor Test forum

-------------------------------------------------------------------------------------
Forgot to mention: I2C sensor TSL2561 Illuminance
Can you try this ?

Code: Select all

return {
            on =    {
                        devices     =   { 
                                            79              -- change to your Lux device idx
                                        }
                    },
                
      logging    =   {   
                        level       =  domoticz.LOG_DEBUG,   
                        marker      =  "Lux 2 light" 
                    },    

    execute = function(dz, item)
        local light = dz.devices(1488)      -- change to idx you want to switch on / off
        
		dz.log("item.rawData[1] ===>> " .. tostring(item.rawData[1]),dz.LOG_DEBUG )
        
		
		if ( tonumber(item.rawData[1]) or 6 ) < 5 or tostring(item.rawData[1]) == "nan" then         
            light.switchOn().checkFirst()
            dz.log("On: " .. item.state,dz.LOG_FORCE)
        else
            light.switchOff().checkFirst()
            dz.log("Off: " .. item.state,dz.LOG_FORCE)
        end
       --  item.dump()           -- This will dump all relevant information from your lux device to log. Can be removed when not needed anymore
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
iganin
Posts: 47
Joined: Tuesday 28 October 2014 17:55
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Aalter, Belgium
Contact:

Re: NaN comparison

Post by iganin »

I see no error but nothing happens.


2019-03-02 09:33:18.278 Status: dzVents: Info: Handling events for: "Corridor Lux", value: "-nan"
2019-03-02 09:33:18.279 Status: dzVents: Info: Lux 2 light: ------ Start internal script: Light 1nd floor Test forum: Device: "Corridor Lux (Main)", Index: 536
2019-03-02 09:33:18.280 Status: dzVents: Debug: Lux 2 light: Processing device-adapter for Corridor 1st Floor Light: Switch device adapter
2019-03-02 09:33:18.280 Status: dzVents: Debug: Lux 2 light: item.rawData[1] ===>> -nan
2019-03-02 09:33:18.281 Status: dzVents: Debug: Lux 2 light: Constructed timed-command: Off
2019-03-02 09:33:18.281 Status: dzVents: !Info: Lux 2 light: Off: -nan
2019-03-02 09:33:18.281 Status: dzVents: Info: Lux 2 light: ------ Finished Light 1nd floor Test forum
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: NaN comparison

Post by waaren »

iganin wrote: Saturday 02 March 2019 9:34 I see no error but nothing happens.


2019-03-02 09:33:18.278 Status: dzVents: Info: Handling events for: "Corridor Lux", value: "-nan"
2019-03-02 09:33:18.279 Status: dzVents: Info: Lux 2 light: ------ Start internal script: Light 1nd floor Test forum: Device: "Corridor Lux (Main)", Index: 536
2019-03-02 09:33:18.280 Status: dzVents: Debug: Lux 2 light: Processing device-adapter for Corridor 1st Floor Light: Switch device adapter
2019-03-02 09:33:18.280 Status: dzVents: Debug: Lux 2 light: item.rawData[1] ===>> -nan
2019-03-02 09:33:18.281 Status: dzVents: Debug: Lux 2 light: Constructed timed-command: Off
2019-03-02 09:33:18.281 Status: dzVents: !Info: Lux 2 light: Off: -nan
2019-03-02 09:33:18.281 Status: dzVents: Info: Lux 2 light: ------ Finished Light 1nd floor Test forum
That is because the script is designed to react on the string "nan" but your sensor produces the string "-nan". If you change line 19 to

Code: Select all

		if ( tonumber(item.rawData[1]) or 6 ) < 5 or tostring(item.rawData[1]) == "-nan" then   
the script should work
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
iganin
Posts: 47
Joined: Tuesday 28 October 2014 17:55
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Aalter, Belgium
Contact:

Re: NaN comparison

Post by iganin »

It now works. Super!!
Thank you for the help!!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest