Selector Switch - Setting up states with HW button [Solved]

Easy to use, 100% Lua-based event scripting framework.
Post Reply
misko903
Posts: 50
Joined: Thursday 27 September 2018 22:58
Target OS: Raspberry Pi
Domoticz version: Latest
Location: Trencin, Slovakia
Contact:

Selector Switch - Setting up states with HW button [Solved]

Post by misko903 » Monday 08 October 2018 15:15

Hi guys,
I have created a virtual Selector switch with 4 options:

0-Off
10-Slovakia
20-Lumen
30-Jemne

I want to control the Selector switch (playing internet radio) with a physical button (change between Radio stations). How should the code looks like?
I spend several hours on it, trying to read the manual, but nothing works.
The code looks like this:

Code: Select all

return {

   on = {
       devices = {
       29,           -- idx HW Switch 
       79,           -- idx of Selector switch
                }
       },       
               
   execute = function(dz, item )
        if item.state == "Click" then

        if domoticz.device(79).switchSelector(10) then -- checking if Selector switch is 10=Slovensko  ?
        		dz.devices(79).switchSelector(20)     -- set the selector switch to 20=Lumen
  
          elseif item.state == "Double Click" then
            dz.devices(79).toggleSwitch()    -- Radio
        elseif item.state == "Long Click" then
            dz.devices(79).switchOff()      -- Radia
        end
--	dz.log('Device ' .. device.name .. ' was changed', dz.LOG_INFO)
   end
}
Last edited by misko903 on Tuesday 09 October 2018 10:57, edited 1 time in total.

User avatar
waaren
Posts: 1358
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Selector Switch - Setting up states with HW button

Post by waaren » Monday 08 October 2018 22:46

misko903 wrote:
Monday 08 October 2018 15:15
I have created a virtual Selector switch with 4 options:

Code: Select all

0-Off
10-Slovakia
20-Lumen
30-Jemne
I want to control the Selector switch (playing internet radio) with a physical button (change between Radio stations). How should the code looks like?
Not sure if I understand you correctly but try this. [EDIT: 20180910 - 09:30] Removed maxLevel. Now calc from #levelNames (count number of levelNames)

Code: Select all

 -- selectRadioStations.lua

 return {
    on = { devices = { 1082 }},             -- device IDX of Button
    
    logging =   {   level   =   domoticz.LOG_DEBUG,
                    marker  =   "selectRadioStations" },    

    execute = function(dz,button)
        local buttonState       = button.levelName
        local radioSelector     = dz.devices(1081)        -- device IDX of radio selector
        local levelTable        = radioSelector.levelNames
        local maxLevel          = (#levelTable - 1) * 10  
        
        if buttonState == "Click" then
            radioSelector.switchSelector(20)         -- Choose favoriete channel
        elseif buttonState == "Long Click" then      -- Switch Off  
            radioSelector.switchSelector(0)
        elseif buttonState == "Double Click" then    -- Cycle through stations 
            local nextLevel = radioSelector.level + 10
            if nextLevel <= maxLevel then 
               radioSelector.switchSelector(nextLevel)
            else
                radioSelector.switchSelector(10)
            end
        end        
        local levelTable = radioSelector.levelNames
   end   
}
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

misko903
Posts: 50
Joined: Thursday 27 September 2018 22:58
Target OS: Raspberry Pi
Domoticz version: Latest
Location: Trencin, Slovakia
Contact:

Re: Selector Switch - Setting up states with HW button

Post by misko903 » Tuesday 09 October 2018 10:56

Yeah, it works, thank you!
to understand the language reading the manual is quite hard. There are too less examples - also on the forum.

Thanks again,
Michal

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests