Lua script to update counter needs modernization Topic is solved

Moderator: leecollings

Post Reply
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Lua script to update counter needs modernization

Post 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
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lua script to update counter needs modernization

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Lua script to update counter needs modernization

Post by philchillbill »

Thanks @waaren, that did the trick !

Any idea how I could reset that counter every day after midnight?
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lua script to update counter needs modernization

Post 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 ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Lua script to update counter needs modernization

Post 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"
		}
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lua script to update counter needs modernization

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Lua script to update counter needs modernization

Post 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.
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lua script to update counter needs modernization

Post 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?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Lua script to update counter needs modernization

Post 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?
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lua script to update counter needs modernization

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Lua script to update counter needs modernization

Post 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).
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lua script to update counter needs modernization

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Lua script to update counter needs modernization

Post 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.
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
edgarhildering
Posts: 31
Joined: Thursday 11 June 2015 22:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: Lua script to update counter needs modernization

Post 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
RaspberryPi running Domoticz 2023.2
PIRs + lightswitches
thermometers + heating control
hygrometers + dehumifiers
TUYA switches
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest