[solved] Checking persistent data with history, while the script initializes, gives an error  [SOLVED]

Moderator: leecollings

Post Reply
jake
Posts: 742
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

[solved] Checking persistent data with history, while the script initializes, gives an error

Post by jake »

Checking persistent data with history, while the script initializes, gives an error. The result is that the script stops and doesn't create the __data xyz file with a fresh first data point.

This part of the script makes it stop when I try to run it for the first time. With 'history true' you can't give an initial value

Code: Select all

	local counter = domoticz.data.showerCounter
	
	if counter.getLatest().data == nil then
		counter.add(0)
		domoticz.log('ShowerCounter op 0 gezet, initial')
	end
This is a real pity, because the script can't check the value of the data, because it might be 'nil' which still causes the script to quit. On the other hand when I simply .add (0) I might overwrite an existing value every minute, while that is not wanted.

Luckily I had an other device that I could check the 'latest update' from and use that as a decision whether it was safe to put a '0' value in there. Now the __data file is created, I could use the above code again (but of course it doesn't make sense anymore).
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Checking persistent data with history, while the script initializes, gives an error

Post by waaren »

can you please share the complete script ? It is kind of hard to judge what is happening based in this code snippet.

To set an initial value with history true: data = { varname = { initial = 0, history = true, maxItems = 100 } },
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Checking persistent data with history, while the script initializes, gives an error  [SOLVED]

Post by dannybloe »

getLatest() returns a data table or nil if there is no data. Your check is not correct:

Code: Select all

local latest = counter.getLatest()
if (latest == nil) then
    ...
You can also check the size:

Code: Select all

if (counter.size == 0) then
   ...
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jake
Posts: 742
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Checking persistent data with history, while the script initializes, gives an error

Post by jake »


waaren wrote:can you please share the complete script ? It is kind of hard to judge what is happening based in this code snippet.

To set an initial value with history true: data = { varname = { initial = 0, history = true, maxItems = 100 } },
dannybloe wrote:getLatest() returns a data table or nil if there is no data. Your check is not correct:

Code: Select all

local latest = counter.getLatest()
if (latest == nil) then
    ...
You can also check the size:

Code: Select all

if (counter.size == 0) then
   ...
Thanks to both of you. I'm sorry that I forgot to add the initialisation of the script, where I define the 'persistent data'. I was not aware that I could add the 'initial = var2' to this type of data as well. I have added this to the example in the wiki.

I also saw that I used the wrong check in the IF statement. I fixed that now as well, just as a double check to prevent an error if the data file would become corrupt.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest