How to view the output of a persistant data filter function?

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

Moderator: leecollings

Post Reply
Ray-Man
Posts: 35
Joined: Friday 12 December 2014 19:03
Target OS: Linux
Domoticz version: 2021.1
Location: Zwolle
Contact:

How to view the output of a persistant data filter function?

Post by Ray-Man »

Hello all,

Breaking my head over the following:
I have a persistent data table.
this code:

Code: Select all

print('Today item 01 = '..dz.data.TMPTODAY.get(1).data[1]..', '..dz.data.TMPTODAY.get(1).data[2])
print('Today item 48 = '..dz.data.TMPTODAY.get(48).data[1]..','..dz.data.TMPTODAY.get(48).data[2])
print('Today item 96 = '..dz.data.TMPTODAY.get(96).data[1]..','..dz.data.TMPTODAY.get(96).data[2])


Provides this output:
Today item 01 = 2025-10-05 00:00:00, 0.1477
Today item 48 = 2025-10-05 11:45:00, 0.1452
Today item 96 = 2025-10-05 23:45:00, 0.152

From this list I only want to filter future data (or filter out previous data per now). I tried..

Code: Select all

local MyVar = dz.data.TMPTODAY
    local VanafNu = MyVar.filter(function (item)
        return (dz.time.dateToDate(item.data[1], 'yyyy-mm-dd hh:MM:ss', 'yyyy-mm-dd hh:MM:ss', 0) >= dz.time.rawDateTime)
        end)
... and it doesn't give any errors, followed by code to at least show the filtered data in the log.
I tried (new line after 'end)')
print('TEST OUTPUT = '..VanafNu)
print('TEST OUTPUT = '..VanafNu[1])
but all I get is
lua:214: attempt to concatenate a table value (local 'VanafNu')
lua:214: attempt to concatenate a nil value (field '?')

How to access the filtered data?
User avatar
waltervl
Posts: 6677
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: How to view the output of a persistant data filter function?

Post by waltervl »

I never used the filter function of dzvents but it looks like it is not designed for your use case.
And the Domoticz time object is a real complex object as it is a kind of a table also.
So perhaps you have to create your own filter function.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
Ragdag
Posts: 169
Joined: Friday 30 March 2018 13:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to view the output of a persistant data filter function?

Post by Ragdag »

Ray-Man wrote: Sunday 05 October 2025 17:57 Hello all,

Breaking my head over the following:
I have a persistent data table.
this code:

Code: Select all

print('Today item 01 = '..dz.data.TMPTODAY.get(1).data[1]..', '..dz.data.TMPTODAY.get(1).data[2])
print('Today item 48 = '..dz.data.TMPTODAY.get(48).data[1]..','..dz.data.TMPTODAY.get(48).data[2])
print('Today item 96 = '..dz.data.TMPTODAY.get(96).data[1]..','..dz.data.TMPTODAY.get(96).data[2])


Provides this output:
Today item 01 = 2025-10-05 00:00:00, 0.1477
Today item 48 = 2025-10-05 11:45:00, 0.1452
Today item 96 = 2025-10-05 23:45:00, 0.152

From this list I only want to filter future data (or filter out previous data per now). I tried..

Code: Select all

local MyVar = dz.data.TMPTODAY
    local VanafNu = MyVar.filter(function (item)
        return (dz.time.dateToDate(item.data[1], 'yyyy-mm-dd hh:MM:ss', 'yyyy-mm-dd hh:MM:ss', 0) >= dz.time.rawDateTime)
        end)
... and it doesn't give any errors, followed by code to at least show the filtered data in the log.
I tried (new line after 'end)')
print('TEST OUTPUT = '..VanafNu)
print('TEST OUTPUT = '..VanafNu[1])
but all I get is
lua:214: attempt to concatenate a table value (local 'VanafNu')
lua:214: attempt to concatenate a nil value (field '?')

How to access the filtered data?
Give this a try - I'm not 100% sure but I believe this is the issue:

The .filter() method returns a new historical data object (a table), not a simple value. You can't concatenate it directly with a string, and you need to use the same accessor methods (.get(), .size, etc.) as on the original data.

Here's how to properly access the filtered data:

Code: Select all

local MyVar = dz.data.TMPTODAY
local VanafNu = MyVar.filter(function(item)
    return item.data[1] >= dz.time.rawDateTime
end)

-- Check how many items passed the filter
print('Filtered count: ' .. VanafNu.size)

-- Access individual items using .get()
if VanafNu.size > 0 then
    print('First future item: ' .. VanafNu.get(1).data[1] .. ', ' .. VanafNu.get(1).data[2])
end

-- Or iterate through all filtered items
VanafNu.forEach(function(item)
    print('Future: ' .. item.data[1] .. ', ' .. item.data[2])
end)
A few notes:
  1. Since your datetime strings are already in yyyy-mm-dd hh:MM:ss format, they're lexicographically sortable. You can compare them directly as strings against dz.time.rawDateTime without the dateToDate conversion.
  2. The filtered result behaves like the original historical data object. Use .get(n) to access items, .size for the count, and .forEach() for iteration.
  3. Why your attempts failed:
    • ..VanafNu - Can't concatenate a table
    • ..VanafNu[1] - The object uses .get(1) not [1] for access
Let us know if this works!
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests