Handy functions for dzVents

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

Moderator: leecollings

Post Reply
HvdW
Posts: 565
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: 5502
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: 98
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 - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
HvdW
Posts: 565
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

        -- Create or update text sensor
        local displayText = string.format(
            "Solar Power: %d\n" ..
            "Power Return: %d\n" ..
            "Power Consumption: %d\n" ..
            "Power Available: %d\n" ..
            "Battery Level: %d\n" ..
            spaces(15) .. "Battery Set: %d\n" ..
            spaces(15) .. "EVSE Switch: %s\n" ..
            spaces(15) .. "Charging Level: %s\n",
            solarPower,
            PowerReturn,
            PowerConsumption,
            availableEnergy,
            chargeLevelActual,
            chargeLevelSet,
            EVSwitch_16A.state,
            ChargingSwitchState
        )
        domoticz.devices('Car Charging Actual').updateText(displayText)
 
Bugs bug me.
HvdW
Posts: 565
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 »

Here are some other dzVents examples for your code

Code: Select all

local message = (temperature > 30) and "It is very warm!" or "It is cool."
which replaces

Code: Select all

if temperature > 30 then
	local message =  "It is very warm!"
else
	local message =  "It is very warm!"
end
or

Code: Select all

local result = (a > b) and "greater" or "less or equal"
Happy programming.
Bugs bug me.
HvdW
Posts: 565
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 »

Having answered in this topic I have been thinking about documentation of my scripts.
So I asked DeepSeek to have a look at my Car Charging on EV Power script and here is the result.

Code: Select all

--[[
    Script Name: Car Charging on PV Power
    Description: This script manages the charging of an electric vehicle (EV) using surplus solar power (PV).
                 It dynamically adjusts the charging level based on available solar energy and ensures that
                 the EV charger is only active when the car is connected and there is sufficient solar power.
                 The script also prevents unintended charging by turning off the charger when the car is
                 disconnected or when the battery reaches the desired charge level.

    Key Features:
    - Adjusts charging level based on available solar power.
    - Turns off the charger when the car is disconnected.
    - Prevents charging when the battery reaches the desired level.
    - Logs and notifies charging status changes.

    Devices Involved:
    - 'Zonnepanelen': Solar power generation (WhActual).
    - 'Charging Level': Selector switch for setting the charging level (6A, 8A, 10A, 13A, 16A, Off).
    - 'EVSE Switch 16A': Main switch for enabling/disabling the EV charger.
    - 'XC40 battery charge set': Desired battery charge level (percentage).
    - 'XC40-ChargeLevel': Current battery charge level (percentage).
    - 'Power': Device providing power consumption and return data.
    - 'Auto laden': Device for handling EVSE communication errors.
    - 'Car Charging Actual': Text sensor for displaying charging status and power data.

    Global Data:
    - EVSE_Connected: Indicates whether the car is connected to the charger.
    - EVSE_CommunicationError: Indicates if there is a communication error with the EVSE.

    Logic:
    - The script checks the available solar power and adjusts the charging level accordingly.
    - If the car is disconnected, the charger is turned off.
    - If the battery reaches the desired charge level, the charger is turned off.
    - Notifications are sent when charging starts or stops.

    Notes:
    - The script runs every minute to ensure timely adjustments.
    - Human intervention may be required if the EVSE_Connected status lags when connecting the car.
]]--
So from now on I'll strive to document the scripts as well as the sensors in the descriptions.
edit.jpg
edit.jpg (41.52 KiB) Viewed 256 times
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests