Page 1 of 1

Lua script to update counter needs modernization

Posted: Saturday 02 May 2020 17:55
by philchillbill
I used to have the following script in use a few years ago (it worked). I just tried to re-use it in Domoticz 2020.2 and the log shows an error

Error: EventSystem: in updateCounters: [string "commandArray = {}..."]:3: bad argument #3 to 'format' (number has no integer representation)

which I do not understand. Any pointers to what I'm doing wrong?

Code: Select all

commandArray = {}
local function updatenum(dev, value1)
    local cmd = string.format("%d|0|%d", otherdevices_idx[dev], value1)
    table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
if (devicechanged['Quooker'] == 'On') then
 updatenum('Quooker Top-Ups', tonumber(otherdevices['Quooker Top-Ups']) + 1)
end
return commandArray

Re: Lua script to update counter needs modernization

Posted: Saturday 02 May 2020 18:30
by waaren
philchillbill wrote: Saturday 02 May 2020 17:55 I used to have the following script in use a few years ago (it worked). I just tried to re-use it in Domoticz 2020.2 and the log shows an error
Error: EventSystem: in updateCounters: [string "commandArray = {}..."]:3: bad argument #3 to 'format' (number has no integer representation)

which I do not understand. Any pointers to what I'm doing wrong?
Could be related to changes in Lua 5.3 where integers are a new number type.

can you try this?

Code: Select all

commandArray = {}
local function updatenum(dev, value1)
    local cmd = string.format("%d|0|%d", otherdevices_idx[dev], math.floor(value1))
    table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
if (devicechanged['Quooker'] == 'On') then
	print("otherdevices['Quooker Top-Ups'] : " .. tostring(otherdevices['Quooker Top-Ups']))
	updatenum('Quooker Top-Ups', tonumber(otherdevices['Quooker Top-Ups']) + 1)
end
return commandArray

Re: Lua script to update counter needs modernization

Posted: Saturday 02 May 2020 18:46
by philchillbill
Thanks @waaren, that did the trick !

Any idea how I could reset that counter every day after midnight?

Re: Lua script to update counter needs modernization

Posted: Saturday 02 May 2020 21:21
by waaren
philchillbill wrote: Saturday 02 May 2020 18:46 Thanks @waaren, that did the trick !

Any idea how I could reset that counter every day after midnight?
I would do that using a dzVents script. What is the type / subType of the device ?

Re: Lua script to update counter needs modernization

Posted: Saturday 02 May 2020 23:03
by philchillbill
waaren wrote: Saturday 02 May 2020 21:21
philchillbill wrote: Saturday 02 May 2020 18:46 Thanks @waaren, that did the trick !

Any idea how I could reset that counter every day after midnight?
I would do that using a dzVents script. What is the type / subType of the device ?
Spoiler: show

Code: Select all

		{
			"AddjMulti" : 1.0,
			"AddjMulti2" : 1.0,
			"AddjValue" : 27000.0,
			"AddjValue2" : 0.0,
			"BatteryLevel" : 255,
			"Counter" : "44388 Firings",
			"CounterToday" : "17311 Firings",
			"CustomImage" : 0,
			"Data" : "44388 Firings",
			"Description" : "",
			"Favorite" : 0,
			"HardwareID" : 9,
			"HardwareName" : "Dummy",
			"HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
			"HardwareTypeVal" : 15,
			"HaveTimeout" : false,
			"ID" : "82751",
			"LastUpdate" : "2020-05-02 22:41:40",
			"Name" : "Quooker Top-Ups",
			"Notifications" : "false",
			"PlanID" : "0",
			"PlanIDs" : 
			[
				0
			],
			"Protected" : false,
			"ShowNotifications" : true,
			"SignalLevel" : "-",
			"SubType" : "Counter Incremental",
			"SwitchTypeVal" : 3,
			"Timers" : "false",
			"Type" : "General",
			"TypeImg" : "counter",
			"Unit" : 1,
			"Used" : 1,
			"ValueQuantity" : "Firings",
			"ValueUnits" : "Firings",
			"XOffset" : "0",
			"YOffset" : "0",
			"idx" : "751"
		}

Re: Lua script to update counter needs modernization

Posted: Sunday 03 May 2020 0:09
by waaren
philchillbill wrote: Saturday 02 May 2020 23:03 Any idea how I could reset that counter every day after midnight?
This script will update the incremental counter but I don't think I understand your request to reset the counter at midnight.
On the GUI you will see the daily total (which will reset to 0 at midnight) and the overall total.

The overall total cannot be reset without poking directly in the database.

Code: Select all

return 
{
    on = 
    { 
        devices =  
        { 
            'Quooker'  
        }
    }, 

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Quooker',
    },
    
    execute = function(dz, item)
        local topsUp = dz.devices('Quooker Top-Ups')
        
        if item.active then 
            topsUp.incrementCounter(1)
        end
    end
}

Lua script to update counter needs modernization

Posted: Sunday 03 May 2020 8:06
by philchillbill
Thanks for the script, @waaren.

The thing is that the GUI counter does not reset at midnight any more, like it use to do in the past (total/today). I have power metering on individual circuit-breakers so I know when the Quooker is drawing power to keep the water boiling. I used to count the 'firings' of this to see how often the heat top-up kicks in during a day. A long time ago, it stopped working and I just ignored it, but now I'd like to have it working again because I dislike not understanding what broke the functionality.

Re: Lua script to update counter needs modernization

Posted: Sunday 03 May 2020 15:32
by waaren
philchillbill wrote: Sunday 03 May 2020 8:06 The thing is that the GUI counter does not reset at midnight any more, like it use to do in the past (total/today).
For me this would be a signal that something is wrong.
  • Is domoticz active at midnight?
  • do you see anything suspicious in the log just after midnight?
  • did you recently checked your database integrity?
  • what do you see in the database tables Meter and Meter_Calendar for this device?

Re: Lua script to update counter needs modernization

Posted: Sunday 03 May 2020 22:34
by philchillbill
Yes, the machine Domoticz runs on never sleeps. There's noting strange in the logs at midnight either. I have not checked the DB integrity so I will do that. WIth your modified Lua code, I just noticed the following entry in the log:

Code: Select all

2020-05-03 22:28:33.259 Status: LUA: otherdevices['Quooker Top-Ups'] : 18446744073709550265
2020-05-03 22:28:33.259 Error: EventSystem: in updateCounters: [string "commandArray = {}..."]:3: bad argument #3 to 'format' (number has no integer representation)
So the value is a crazy-large wannabe-integer: 18446744073709550265.

Is there any way that the device name with dash in it (Quooker Top-Ups) is risky in Lua?

Re: Lua script to update counter needs modernization

Posted: Sunday 03 May 2020 23:24
by waaren
philchillbill wrote: Sunday 03 May 2020 22:34 Yes, the machine Domoticz runs on never sleeps. There's noting strange in the logs at midnight either. I have not checked the DB integrity so I will do that. WIth your modified Lua code, I just noticed the following entry in the log:

Code: Select all

2020-05-03 22:28:33.259 Status: LUA: otherdevices['Quooker Top-Ups'] : 18446744073709550265
2020-05-03 22:28:33.259 Error: EventSystem: in updateCounters: [string "commandArray = {}..."]:3: bad argument #3 to 'format' (number has no integer representation)
So the value is a crazy-large wannabe-integer: 18446744073709550265.
Looks like an overflow 2^64 - 1 ?
Is there any way that the device name with dash in it (Quooker Top-Ups) is risky in Lua?
No this should work

Re: Lua script to update counter needs modernization

Posted: Monday 04 May 2020 14:35
by philchillbill
I did an integrity check on the db and the result was ok. Just for kicks, I deleted the counter device and created a new one with a different idx and so no history. The counter seems to increment exponentially when the underlying on/off switch used in the Lua script switches from off to on. First it counted 1 (correct), then 3, now 6. Looks like a Domoticz issue (I'm on stable 2020.2).

Re: Lua script to update counter needs modernization

Posted: Monday 04 May 2020 17:00
by waaren
philchillbill wrote: Monday 04 May 2020 14:35 I did an integrity check on the db and the result was ok. Just for kicks, I deleted the counter device and created a new one with a different idx and so no history. The counter seems to increment exponentially when the underlying on/off switch used in the Lua script switches from off to on. First it counted 1 (correct), then 3, now 6. Looks like a Domoticz issue (I'm on stable 2020.2).
Isn't this because you are using an incremental counter? You add the current total + 1 to the current total. (the behavior of incremental counter changed in V4.10102)
That's why I add only 1 in the dzVents script.

Re: Lua script to update counter needs modernization

Posted: Tuesday 05 May 2020 10:43
by philchillbill
waaren wrote: Monday 04 May 2020 17:00
philchillbill wrote: Monday 04 May 2020 14:35 I did an integrity check on the db and the result was ok. Just for kicks, I deleted the counter device and created a new one with a different idx and so no history. The counter seems to increment exponentially when the underlying on/off switch used in the Lua script switches from off to on. First it counted 1 (correct), then 3, now 6. Looks like a Domoticz issue (I'm on stable 2020.2).
Isn't this because you are using an incremental counter? You add the current total + 1 to the current total. (the behavior of incremental counter changed in V4.10102)
That's why I add only 1 in the dzVents script.
Wow, @waaren - you're a life saver. I had not idea the behavior of the counter type had changed. Many thanks for pointing that out.

Re: Lua script to update counter needs modernization

Posted: Monday 06 February 2023 15:13
by edgarhildering
Pls look at this:

Code: Select all

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> dev=1423
> value1=1
> string.format("%d|0|%d",dev,value1)
1423|0|1
> value1=1.1
> string.format("%d|0|%d",dev,value1)
stdin:1: bad argument #3 to 'format' (number has no integer representation)
stack traceback:
	[C]: in function 'string.format'
	stdin:1: in main chunk
	[C]: in ?
> string.format("%d|0|%d",dev,math.floor(value1+0.5))
1423|0|1
> string.format("%d|0|%0.f",dev,value1)
1423|0|1
The value you receive in your update function turns out to be a floating point number. The counter may ONLY be updated with integer values. So you can either convert the float value1 to an integer using math.floor or you can change the format string to deliver the integer part of the float. The latter is my favourite. :D

Code: Select all

local function updatenum(dev, value1)
    local cmd = string.format("%d|0|%0.f", otherdevices_idx[dev], value1)
    table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
kind regards, --Edgar