Need help for understand dzVents

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

Moderator: leecollings

albebert
Posts: 20
Joined: Friday 18 September 2020 17:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Need help for understand dzVents

Post by albebert »

waaren wrote: Tuesday 23 February 2021 0:26
albebert wrote: Monday 22 February 2021 19:02 many thanks and send me your paypal adres in PM wan't to offer you a beer :)
Very kind of you but not needed.
i know but it's a pleasure for me, so as you want :)

Many thanks for your help !

after some test, seems that all is working. just a smal issue, i don't know why but when amp is Off, the script send a volume=0 to the amp, not a real problem because when the amp is off it ignore the command.

So i share m'y 2 scripts that are running (i've 2 zone to control)

zone Main (Called zone 1 for me) :

Code: Select all

local scriptVar = 'Yamaha'

return {
    on = {
        timer =
        {
            'every minute' -- just an example to trigger the request
        },
        devices =
        {
            'Ampli - Zone 1',
        },
        httpResponses =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        local maxVolume = 161

        local volume = dz.devices('Ampli - Zone 1') -- switch (dimmer)
        local input = dz.devices('Ampli Input') -- selector switch with all possible input strings as levels

        if item.isTimer then
            dz.openURL({
                url = 'http://192.168.200.67/YamahaExtendedControl/v1/main/getStatus',
                callback = scriptVar,
            })
        elseif item.isDevice then
            dz.openURL('http://192.168.200.67/YamahaExtendedControl/v1/main/setVolume?volume=' .. (math.floor( item.level * maxVolume / 100) ))
        elseif item.isHTTPResponse and item.json and item.json.power == 'on' then
            dz.log('or another here ?', dz.LOG_DEBUG)                           
            volume.dimTo(math.floor(item.json.volume / maxVolume * 100)).silent()
            input.switchSelector(item.json.input).silent()
        end
        if item.isHTTPResponse and item.json and item.json.power == 'standby' then
           volume.quietOff()
        end
    end
}
Zone 2 :

Code: Select all

local scriptVar = 'Yamaha2'

return {
    on = {
        timer =
        {
            'every minute' -- just an example to trigger the request
        },
        devices =
        {
            'Ampli - Zone 2',
        },
        httpResponses =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        local maxVolume = 161

        local volume2 = dz.devices('Ampli - Zone 2') -- switch (dimmer)
        local input = dz.devices('Ampli Input') -- selector switch with all possible input strings as levels

        if item.isTimer then
            dz.openURL({
                url = 'http://192.168.200.67/YamahaExtendedControl/v1/zone2/getStatus',
                callback = scriptVar,
            })
        elseif item.isDevice then
            dz.openURL('http://192.168.200.67/YamahaExtendedControl/v1/zone2/setVolume?volume=' .. (math.floor( item.level * maxVolume / 100) ))
        elseif item.isHTTPResponse and item.json and item.json.power == 'on' then
            dz.log('or another here ?', dz.LOG_DEBUG)                           
            volume2.dimTo(math.floor(item.json.volume / maxVolume * 100)).silent()
            input.switchSelector(item.json.input).silent()
        end
        if item.isHTTPResponse and item.json and item.json.power == 'standby' then
           volume2.quietOff()
        end

    end
}
albebert
Posts: 20
Joined: Friday 18 September 2020 17:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Need help for understand dzVents

Post by albebert »

so now i undestand a bit more dzvent, i'm trying to make a net radio selector.

Code: Select all

local scriptVar = 'Yamaha3'

return {
    on = {
        timer =
        {
            'every minute' -- just an example to trigger the request
        },
        httpResponses =
        {
            scriptVar
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)


        local input3 = dz.devices('Net Radio Selector') -- selector switch with all possible input strings as levels

        if item.isTimer then
            dz.openURL({
                url = 'http://192.168.200.67/YamahaExtendedControl/v1/netusb/getPlayInfo',
                callback = scriptVar,
            })
            return
        end


        if item.isHTTPResponse and item.json and item.json.input == 'net_radio' then
            dz.utils.dumpTable(item.json) -- just for test / debug
			input3.switchSelector(item.json.artist).silent()
        end

    end
}
but i've 1 problem and 1 question :

question : it's possible to add a wildcard to input3.switchselector ?
Api send "La Grosse Radio Rock (France/French)" but wan't only" La Grosse Radio Rock" in selector, it's possible ?

problem : when the script run, it's seems to change the input causing amp to reopen radio (1s audio cut due tu buffering), ".silent" does not act as "quietoff" ?

(not shure that i'am clear, i'm french and not good in english, if you don't understant i will try to reformulate)

Thanks
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Need help for understand dzVents

Post by waaren »

albebert wrote: Tuesday 23 February 2021 10:56 question : it's possible to add a wildcard to input3.switchselector ?
No the switchSelector() must match the exact level name or level number (0,10,20 etc)
Api send "La Grosse Radio Rock (France/French)" but wan't only" La Grosse Radio Rock" in selector, it's possible ?
Yes you could use standard Lua string manipulation or a translation table

Code: Select all

-- string manipulation 
sourceStr = "La Grosse Radio Rock (France/French)" 
targetStr = sourceStr:match("(.+)%(") -- Anything up to the first ( 
targetStr = targetStr:gsub("^%s*(.-)%s*$", "%1") -- remove leading and trailing spaces

print("Only part up to first ( will be printed of '" .. sourceStr .. "'. Result: '" .. targetStr .. "'")

-- translationTable
translationTable =
{
	["La Grosse Radio Rock (France/French)"] = "La Grosse Radio Rock",
	["something else"] = "something new",
}

print('"' .. sourceStr .. '" will be translated into "' .. translationTable[sourceStr] .. '"')
print('"something else"' .. ' will be translated into "' .. translationTable["something else"] ..'"')
problem : ".silent" does not act as "quietoff" ?
No silent() only prevents the device update to lead to another event trigger. It will perform the requested action on the device
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
albebert
Posts: 20
Joined: Friday 18 September 2020 17:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Need help for understand dzVents

Post by albebert »

ok all is working now :)

Code: Select all

local scriptVar = 'Yamaha_net_radio'

return {
    on = {
        timer =
        {
            'every minute' -- just an example to trigger the request
        },
        httpResponses =
        {
            scriptVar
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)


        local input3 = dz.devices('Net Radio Selector') -- selector switch with all possible input strings as levels

        if item.isTimer then
            dz.openURL({
                url = 'http://192.168.200.67/YamahaExtendedControl/v1/netusb/getPlayInfo',
                callback = scriptVar,
            })
            return
        end
--dz.log(input3.switchSelector(state, dz.LOG_DEBUG)      

        if item.isHTTPResponse and item.json and item.json.input == 'net_radio' then
            dz.utils.dumpTable(item.json) -- just for test / debug
            sourceStr = item.json.artist 
            targetStr = sourceStr:match("(.+)%(") -- Anything up to the first ( 
            targetStr = targetStr:gsub("^%s*(.-)%s*$", "%1") -- remove leading and trailing spaces
			input3.switchSelector(targetStr).silent().checkFirst()
        end

    end
}
i've added .checkFirst() to switchSelector so now i've a cut only if i change radio from outside domoticz.

another many thanks !
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest