Handy functions for dzVents

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

Moderator: leecollings

Post Reply
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Handy functions for dzVents

Post by HvdW »

I used to 'C', so I missed the IFF(this,then that,else) in dzVents
Here's mine:

Code: Select all

        IIF = function(condition, trueValue, falseValue)
            if condition then
                return trueValue
            else
                return falseValue
            end
        end,
The function is placed in the global_data script under helpers.
To call the function (example)

Code: Select all

domoticz.helpers.IIF(mySwitch.state == 'On', otherSwitch.switchOn(),otherSwitch.switchOff() )
Like the example in Quickstart in the dzVents WiKi.

There is a quick sleep(seconds) function as well:

Code: Select all

        sleep = function(seconds)
            os.execute("sleep " .. tonumber(seconds))
        end,
Called with

Code: Select all

domoticz.helpers.sleep(3)
The sleep() command produces some kind of error when time is set to >= 7 seconds.
The log displays

Code: Select all

Finished Test Script after >7 seconds. (using 0.040 seconds CPU time !)
Bugs bug me.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Handy functions for dzVents

Post by waltervl »

Never use sleep in a dzvents script! It will halt dzvents and other Domoticz executions.

That is also why we have the asynchronous os execute function in dzvents to prevent that these os executions halt Domoticz unexpectedly.
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
RonkA
Posts: 95
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2023.2
Location: Harlingen
Contact:

Re: Handy functions for dzVents

Post by RonkA »

Nice little global_data knowledge nugget, thanks for that.

This has the potential to shorten my scripts a lot by centralizing all the same functions that are used in multiple scripts..

These type of handy uses of dzVents feel buried in the wiki and are not easily found and understand by people that have limited coding skills like me..
SolarEdge ModbusTCP - Open Weather Map - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Handy functions for dzVents

Post by HvdW »

spaces() is a function to help you align data when writing to a Text Sensor

Code: Select all

-- Helper functions
local function spaces(count)
    return string.rep(" ", count)
end

local function round(num, decimals)
    local mult = 10^(decimals or 0)
    return math.floor(num * mult + 0.5) / mult
end
Here is an example how to use it, plus it is a structured way to fill a Text Sensor with text

Code: Select all

        -- Update text display
        domoticz.devices('YourTextSensor').updateText(string.format(
            'lostChargeTime today %s%s min\n' ..
            'Connected Time today %s%s min\n' ..
            'NOT Connected Time today %s%s min',
            spaces(9),domoticz.data.lostChargeTime,
            spaces(8),domoticz.data.connectedTime,
            spaces(0),domoticz.data.notConnectedTime
        ))
 
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest