dzVents for-loop doesn't trigger  [Solved]

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

Moderator: leecollings

Post Reply
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

dzVents for-loop doesn't trigger

Post by imdos »

Hi all,

I'm trying to get this function to work; but it doesn't trigger due to some logic problem or other error. Someone who could help me out with this.

When I change the timer to every minute and turn on debug I only get the following information.

Code: Select all

Status: dzVents: Info:  Nintendo: ------ Start external script: Nintendo.lua:, trigger: every minute
Status: dzVents: Debug: Nintendo: Processing device-adapter for $blink_blauw: Switch device adapter
Status: dzVents: Debug: Nintendo: Processing device-adapter for $blink_geel: Switch device adapter
Status: dzVents: Debug: Nintendo: Processing device-adapter for $Vloer-LED-B: Switch device adapter
Status: dzVents: Debug: Nintendo: Processing device-adapter for $Vloer-LED-Y: Switch device adapter
Status: dzVents: Info:  Nintendo: ------ Finished Nintendo.lua
All LEDs are off.

The actual script

Code: Select all

--[[
 Nintendo
als de switch of wii langer dan 35 minuten aanstaat, dan de blauwe LED laten knipperen. Als dat apparaat uit is dan LED weer uitzetten.
]]--
return {
        active = true,
        on = {
				devices = { 'Wii', 'N_Switch' },
                timer = { 'every 5 minutes' },
            },
logging = {
        level = domoticz.LOG_ERROR,		-- change to LOG_ERROR when script executes OK
	-- domoticz.LOG_DEBUG
        marker = 'Nintendo',
        },
-- dzVents 2.4 functie:
        execute = function(domoticz,item)
        local blauwblink = domoticz.devices('$blink_blauw')
        local geelblink = domoticz.devices('$blink_geel')
        local blauw = domoticz.devices('$Vloer-LED-B')
        local geel = domoticz.devices('$Vloer-LED-Y')
        -- local deviceToCheck = domoticz.devices().forEach(function(devicesToCheck)
        local devicesToCheck = { 'Wii', 'N_Switch' }
        local thresholdx = 35
	local Time = require('Time')
	-- setup 
        if ( item.isTimer ) then 
		for i, deviceToCheck in pairs(devicesToCheck) do
                -- local minuten = domoticz.devices(deviceToCheck).lastUpdate.minutesAgo
			local minuten = Time(deviceToCheck.lastUpdate)
				-- Actual check!
		-- if ( deviceToCheck.active and minuten.minutesAgo > thresholdx ) then
                if ( deviceToCheck.active and minuten.minutesAgo > thresholdx ) then
                        if ( blauw.active ) then
                                domoticz.log('De blauwe LED staat al aan!')
                        else
                                blauwblink.switchOn()
                                domoticz.log('De blauwe LED knippert...')
                                domoticz.notify("Domoticz", "De " .. domoticz.devices(deviceToCheck) .. "staat al meer dan 35 minuten aan", domoticz.PRIORITY_NORMAL,domoticz.SOUND_DEFAULT, "" , "telegram")
                        end
               	end
            end   	
		end
		if ( item.isDevice and not item.active ) then
			blauwblink.switchOff().checkFirst()
			-- blauw.switchOff().checkFirst()
            domoticz.log('De blauwe LED knippert niet meer!')
        end
  end
}
The previous checks are commented right now and I have adjusted my code to resemble the example from the wiki.
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents for-loop doesn't trigger  [Solved]

Post by waaren »

imdos wrote: Tuesday 16 July 2019 13:18 I'm trying to get this function to work; but it doesn't trigger due to some logic problem or other error. Someone who could help me out with this.
Could well be I do not completely understand your requirements but can you test this and revert with your findings ?

Code: Select all

--[[
 Nintendo
 als de switch of wii langer dan 35 minuten aanstaat, dan de blauwe LED laten knipperen. Als dat apparaat uit is dan LED weer uitzetten.
]]--

local allDevices = {'Wii', 'N_Switch'}

return 
{
    on = { devices = allDevices },

    logging = { level = domoticz.LOG_DEBUG, marker = 'Nintendo' },

    execute = function(dz, item)
        local blauwblink = dz.devices('$blink_blauw')
        local maxOnTime = 35
        local allWithinLimits = true

        for _, name in ipairs(allDevices) do
            if dz.devices(name).active then
                blauwblink.switchOn().afterMin(maxOnTime).checkFirst()
                allWithinLimits = false
            end
        end

        if allWithinLimits then
            blauwblink.cancelQueuedCommands()
            blauwblink.switchOff().checkFirst()
        end
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest