In global_data, how to call a helper function  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
mm148881
Posts: 16
Joined: Sunday 13 October 2019 17:48
Target OS: Linux
Domoticz version: 4.11499
Location: Royaume de France et de Navarre
Contact:

In global_data, how to call a helper function

Post by mm148881 »

Hi,
I am new to dzvents, sorry if this is trivial, but I cannot find a way of calling a helper function B from another function A both defined in global_data.lua. Just to be concrete, here is the code I am trying to run.

Code: Select all

return{
        helpers={
        mysplit=function(inputstr, sep)
            if sep == nil then
                sep = "%s"
            end
            local t={}
            for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                table.insert(t, str)
            end
            return t
        end,     
        timeEqual=function(myTime)
            local Time = require('Time')
            local now= Time() -- current time
            local hour=now.hour
            local minute=now.min
            local Time0=mysplit(myTime,':')
            if(hour == tonumber(Time0[1]) and minute == tonumber(Time0[2]))then
                return true
            else
                return false
            end
        end
        
        },
       data = {
          temperatures = { history = true, maxItems = 10 },
          temp = { initial={},history = true, maxItems = 10 },
          T_0= {initial=24.0 }
       },
}
In this specific case mysplit is not seen by timeEqual.
Thanks beforehand!
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: In global_data, how to call a helper function

Post by elmortero »

domoticz.helpers.mysplit(inputstr, sep)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: In global_data, how to call a helper function  [Solved]

Post by waaren »

mm148881 wrote: Wednesday 13 November 2019 12:00 Hi,
I am new to dzvents, sorry if this is trivial, but I cannot find a way of calling a helper function B from another function A both defined in global_data.lua.
In this specific case mysplit is not seen by timeEqual.
A helper function calling another helper function also need to use domoticz.helpers.<helper function name> and therefore also need to have domoticz in the header.
Also data defined with history = true cannot have an initial value.

So it would become something like.

Code: Select all

return 
{       
   data = 
   {
      temperatures = { history = true, maxItems = 10 },
      temp = { history = true, maxItems = 10 },
      T_0 = { initial = 24.0 },
   },

   helpers = 
   {
        mysplit = function(inputstr, sep)
            if sep == nil then
                sep = "%s"
            end
            local t= {}
            for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                table.insert(t, str)
            end
            return t
        end,     

        timeEqual = function(myTime,dz)
            local Time = require('Time')
            local now = Time() -- current time
            local hour = now.hour
            local minute = now.min
            local Time0 = dz.helpers.mysplit(myTime,':')
            if (hour == tonumber(Time0[1]) and minute == tonumber(Time0[2])) then
                return true
            else
                return false
            end
        end
    },
}
I don't know what you try to compare here but dzVents has a native function for comparing times. (see below example)

Code: Select all

--[[
        
         function (time object).compare(other time obkect) returns a table with the following keys 
         
         hours
         ms
         secs
         mins
         minutes
         milliseconds
         seconds
         compare (1 or -1) --past of future
         days
]]

return 
{
    on = { timer = { 'every minute' }},

    execute = function(dz)
        local myDevice = dz.devices(132)
        dz.utils.dumpTable(dz.time.compare(myDevice.lastUpdate))
    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
mm148881
Posts: 16
Joined: Sunday 13 October 2019 17:48
Target OS: Linux
Domoticz version: 4.11499
Location: Royaume de France et de Navarre
Contact:

Re: In global_data, how to call a helper function

Post by mm148881 »

Thank you for the solution to the problem. I would have hoped that there was some object like self or this (which I tried) to reference to the global data.

Answering waaren's question, I am trying to compare only the hour and minutes of time, to trigger action on some device every day. Thus, I would give timeEqual a string like '18:30' for a given time of day to compare to the current time. I looked at the documentation, but as far as I understood comparison is between time objects, which include also days, months and years, which meant that I would have to create a time including those information on the current day and each day. I didn't find a way of using compare hours and minutes of time object, but I wouldn't be surprised that that is possible. I read the documentation quite rapidly, and also I don't really know lua as I have a C++, C and python background.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: In global_data, how to call a helper function

Post by waaren »

mm148881 wrote: Wednesday 13 November 2019 15:11 I am trying to compare only the hour and minutes of time, to trigger action on some device every day.
If I understand your intended functionality correctly these examples both do what you want.

solution 1

Code: Select all

return 
{
    on = { timer = { 'at 17:23' }},

    execute = function(dz)
        dz.log('it\'s now ' .. dz.time.rawTime)
    end
}
solution 2

Code: Select all

return 
{
    on = { timer = { 'every minute' },
    devices = {' devicename' },
},

    execute = function(dz)
    	if dz.time.matchesRule('at 19:29') then 
            dz.log('it\'s now ' .. dz.time.rawTime)
        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
mm148881
Posts: 16
Joined: Sunday 13 October 2019 17:48
Target OS: Linux
Domoticz version: 4.11499
Location: Royaume de France et de Navarre
Contact:

Re: In global_data, how to call a helper function [Solved]

Post by mm148881 »

Thanks for the great help! Solution 1 is for me the cleanest solution.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest