Page 1 of 1

NaN comparison

Posted: Thursday 28 February 2019 20:56
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?

Re: NaN comparison

Posted: Friday 01 March 2019 1:08
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
}

Re: NaN comparison

Posted: Friday 01 March 2019 18:46
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

Re: NaN comparison

Posted: Friday 01 March 2019 23:23
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
}

Re: NaN comparison

Posted: Saturday 02 March 2019 9:34
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

Re: NaN comparison

Posted: Saturday 02 March 2019 10:46
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

Re: NaN comparison

Posted: Saturday 02 March 2019 13:14
by iganin
It now works. Super!!
Thank you for the help!!