Sudden error: attempt to compare number with table  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Sudden error: attempt to compare number with table

Post by FearNaBoinne »

I had a working script, and added a few LOG lines to it (All of them AFTER the error line #27! The log line before there was already there!), and now, suddenly, I get this error:

2020-01-02 10:00:11.305 Status: LUA: Device based event fired on 'Z-Wave node status', value 'Alive'
2020-01-02 10:00:11.321 Status: Set UserVariable Alive state = 1
2020-01-02 10:00:11.288 Error: dzVents: Error: (2.4.28) Z-wave alive monitor: An error occurred when calling event handler Z-Wave dead
2020-01-02 10:00:11.288 Error: dzVents: Error: (2.4.28) Z-wave alive monitor: ...moticz/scripts/dzVents/generated_scripts/Z-Wave dead.lua:27: attempt to compare number with table

I am probably missing something extremely basic, but here's the script details:

Line 27 reads:

Code: Select all

if (now >= ss30after) then
With the 2 variables set earlier as:

Code: Select all

local ss30after = domoticz.time.sunsetInMinutes + 30
local Time = require('Time')
local now = Time() -- current time
The whole script below:

Code: Select all

return {
	on = {
		devices = {
			'Z-Wave node status'
		}
	},
    logging = {
        level = domoticz.LOG_DEBUG,
        marker = "Z-wave alive monitor"
    },
	execute = function(domoticz, device)
	    local ss30before = domoticz.time.sunsetInMinutes - 30
	    local ss0before = domoticz.time.sunsetInMinutes
	    local ss30after = domoticz.time.sunsetInMinutes + 30
	    local Time = require('Time')
     	    local now = Time() -- current time
	    local Dressoir = domoticz.devices('Dressoir')
		
		domoticz.log('Device ' .. device.name .. ' was changed to '..device.state, domoticz.LOG_INFO)
		if (device.state=='Dead' and domoticz.variables('Alive state').value ~= 0) then
		    domoticz.scenes('Dressoir RED').switchOn()
	            domoticz.variables('Alive state').set(0)
        	    domoticz.notify("ALARM", 'Een Z-Wave node has gone DEAD!', domoticz.PRIORITY_EMERGENCY)
	        elseif (device.state=='Alive' and domoticz.variables('Alive state').value ~= 1) then
        	    domoticz.notify("ALARM", 'All Z-Wave nodes are ALIVE!', domoticz.PRIORITY_EMERGENCY)
  	            domoticz.variables('Alive state').set(1)
        	    if (now >= ss30after) then
        		domoticz.log('30 minutes after sunset, setting accordingly...', domoticz.LOG_INFO)
	                Dressoir.setColor(0, 0, 0, 3, 0, 0, 2, 192)
        	    elseif (now >= ss0before) then
        		domoticz.log('Less than 30 minutes after sunset, setting accordingly...', domoticz.LOG_INFO)
	                Dressoir.setColor(0, 0, 0, 6, 0, 0, 2, 128)
	       	    elseif (now >= ss30before) then
        		domoticz.log('30 minutes before sunset, setting accordingly...', domoticz.LOG_INFO)
       		        Dressoir.setColor(0, 0, 0, 10, 0, 0, 2, 96)
	            else
        		domoticz.log('Not even close to sunset, turning off...', domoticz.LOG_INFO)
	                Dressoir.switchOff()
	            end
        	end
		domoticz.log("I'm done, have a nice day!...", domoticz.LOG_INFO)
	end
}
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: Sudden error: attempt to compare number with table

Post by FearNaBoinne »

Never mind... Since I wrote this, it started working again without the error and without any changes from my side! Love it when unexplicable things like this happen... :o
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: Sudden error: attempt to compare number with table

Post by FearNaBoinne »

Double never mind, realize now what is going on! Going to fix it...
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Sudden error: attempt to compare number with table

Post by waaren »

FearNaBoinne wrote: Thursday 02 January 2020 10:20 Double never mind, realize now what is going on! Going to fix it...
for other members following this topic:
now is defined as time object ( a table ) and later in the script compared with a number. One way of handling this is to create now also as a number.
Probably the code where the comparison takes place was never reached before.

Code: Select all

local now = domoticz.time.secondsSinceMidnight / 60
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: Sudden error: attempt to compare number with table

Post by FearNaBoinne »

waaren wrote: Thursday 02 January 2020 10:34
FearNaBoinne wrote: Thursday 02 January 2020 10:20 Double never mind, realize now what is going on! Going to fix it...
for other members following this topic:
now is defined as time object ( a table ) and later in the script compared with a number. One way of handling this is to create now also as a number.
Probably the code where the comparison takes place was never reached before.

Code: Select all

local now = domoticz.time.secondsSinceMidnight / 60
Exactly! I was going to post the fixed script when I was done, but then ran in to another snag, which took a bit to figure out: In the wiki, one of the domoticz.time objects mentioned is minutesSinceMidnight... That one obviously does NOT exist!... :o

Almost done cleaning up and then I will post the working script for future reference!
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Sudden error: attempt to compare number with table

Post by waaren »

FearNaBoinne wrote: Thursday 02 January 2020 10:39 Exactly! I was going to post the fixed script when I was done, but then ran in to another snag, which took a bit to figure out: In the wiki, one of the domoticz.time objects mentioned is minutesSinceMidnight... That one obviously does NOT exist!... :o
If you look again you will see that minutestSinceMidnight was introduced in dzVents version 2.5.4
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: Sudden error: attempt to compare number with table

Post by FearNaBoinne »

waaren wrote: Thursday 02 January 2020 10:47
FearNaBoinne wrote: Thursday 02 January 2020 10:39 Exactly! I was going to post the fixed script when I was done, but then ran in to another snag, which took a bit to figure out: In the wiki, one of the domoticz.time objects mentioned is minutesSinceMidnight... That one obviously does NOT exist!... :o
If you look again you will see that minutestSinceMidnight was introduced in dzVents version 2.5.4
Ah!... Missed that damn small small set of numbers!
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: Sudden error: attempt to compare number with table  [Solved]

Post by FearNaBoinne »

The cleaned-up, working code:

Code: Select all

return {
	on = {
		devices = {
			'Z-Wave node status'
		}
	},
    	logging = {
        level = domoticz.LOG_DEBUG,
        marker = "Z-wave alive monitor"
    	},
	execute = function(domoticz, device)

		domoticz.log('Device ' .. device.name .. ' was changed to '..device.state, domoticz.LOG_INFO)

		if (device.state=='Dead' and domoticz.variables('Alive state').value ~= 0) then
		    domoticz.scenes('Dressoir RED').switchOn()
            	    domoticz.variables('Alive state').set(0)
	            domoticz.notify("ALARM", 'Een Z-Wave node has gone DEAD!', domoticz.PRIORITY_EMERGENCY)
        	elseif (device.state=='Alive' and domoticz.variables('Alive state').value ~= 1) then
	    	    local ss30before = domoticz.time.sunsetInMinutes - 30
	    	    local ss0before = domoticz.time.sunsetInMinutes
	    	    local ss30after = domoticz.time.sunsetInMinutes + 30
	            local now = domoticz.time.secondsSinceMidnight/60  -- current time
	    	    local Dressoir = domoticz.devices('Dressoir')

	            domoticz.notify("ALARM", 'All Z-Wave nodes are ALIVE!', domoticz.PRIORITY_EMERGENCY)
	            domoticz.variables('Alive state').set(1)

	            if (now >= ss30after) then
        		domoticz.log('30 minutes after sunset, setting accordingly...', domoticz.LOG_INFO)
	                Dressoir.setColor(0, 0, 0, 3, 0, 0, 2, 192)
        	    elseif (now >= ss0before) then
        		domoticz.log('Less than 30 minutes after sunset, setting accordingly...', domoticz.LOG_INFO)
	                Dressoir.setColor(0, 0, 0, 6, 0, 0, 2, 128)
        	    elseif (now >= ss30before) then
        		domoticz.log('30 minutes before sunset, setting accordingly...', domoticz.LOG_INFO)
	                Dressoir.setColor(0, 0, 0, 10, 0, 0, 2, 96)
        	    else
        		domoticz.log('Not even close to sunset, turning off...', domoticz.LOG_INFO)
	                Dressoir.switchOff()
        	    end

	        end
		domoticz.log("I'm done, have a nice day!...", domoticz.LOG_INFO)
	end
}
Some explanation:
The first part is obvious, if a Zwave node dies, turn the dresser light on and make it RED!
The second part is to return the dresser light to it's intended state. Since it turns on 30 minutes before sunset, and over 90 minutes is tuned down in brightness and goes to a warmer white, I needed 4 passes: before 30 min before sunset it is turned off, 30-0 min before sunset it is turned on to 10% brightness and colder white. At sunset it goes down to 6% and smack middle between cold and warm, and 30 minutes later it goes down to 3% and a pretty warm white.
The logic needs to work backwards, because 30min after sunset and sunset are both after 30 min before sunset, so if I'd start with that, I'd never get to the other steps...

And - for completeness - the crontab bash-script that goes with it:

Code: Select all

#!/bin/bash

/usr/bin/curl 'http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=8' 2>/dev/null | /bin/grep Dead > /dev/null 2>&1

if [ $? == 0 ]                         # 'Dead' was found in the Z-Wave output, which is NOT good
then
        /usr/bin/curl 'http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=666&nvalue=4&svalue=Dead' > /dev/null 2>&1
                                       # Set red alert
else
        /usr/bin/curl 'http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=666&nvalue=1&svalue=Alive' > /dev/null 2>&1
                                       # Set green alert
fi
First curl URI: idx=8 => 8 is the hardware device# of your Z-wave adapter
Second and third curl URI: idx=666 => 666 is the device# of the virtual text device used to trigger the script, which name is "Z-Wave node status"
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest