Jump to content
  • 0

HC2: CentralSceneEvent, two triggers


Question

I have used a LUA script which is triggered by pushing the switch 2 on a Fibaro switch. Depending on whether the key is pushed once, twice or held, I control another device (a light) according to set rules. How the key is pushed, is found by the variable pressSource = fibaro:getSourceTrigger().event.data. This solution has been used previously by several contributors on the forum.

My current code looks like this (only first part shown):

--[[
%% properties
%% events
394 CentralSceneEvent
%% globals
--]]
-- Uses first slave for "CentralSceneEvent" ID (not master, not switch itself). Here 394 
 
local trigger = fibaro:getSourceTrigger()
if (trigger.type == "other"then
    fibaro:debug("Scene started by clicking 'start' button")
else
    
    local pressSource = fibaro:getSourceTrigger().event.data
    fibaro:debug("New: CentralSceneEvent received from key: " .. pressSource.keyId)
    if (pressSource.keyId == 2then
        local ledId = "306" 
        local ledValue = fibaro:getValue(ledId, "value")
        
        if pressSource.keyAttribute == "Pressed" then
            fibaro:debug("Pressed")
            if ledValue == "0" then
                fibaro:call(ledId, "turnOn")
            else
                fibaro:call(ledId, "turnOff")
            end
        elseif pressSource.keyAttribute == "Pressed2" then
            fibaro:debug("Pressed 2 times")
            fibaro:call(ledId, "setValue""100")
        elseif pressSource.keyAttribute == "HeldDown" then
            
            -- etc. (complete code not shown)

 

I now want to add a second trigger, i.e. another Fibaro switch being pushed. Depending on which switch is used, I want different lights to be adjusted. Below is my first edit (still incomplete). In line 5, the new trigger 494 is added. How do I know if the scene was triggered by 394 or 494? The answer is probably in fibaro:getSourceTrigger(), but I cannot find the complete specification for this function documented anywhere. Specifically:

- How do I find whether 394 or 494 was the trigger? The syntax trigger.event.id in line 14 is my guest guess. What is correct?

- Once I have the right triggerID, I can easily make the variable ledID dependent on triggerID, by having two related lists (arrays): {394, 494} for the triggers and {306, 406} for the corresponding LED lights. This needs to be implemented in line 18. 

 

Can anyone please provide a more complete specification for the data returned by fibaro:getSourceTrigger in HC2, and show me how I can use it to implement double triggers? Or are you all too busy converting to HC3?

 

PS: I have simplified this to TWO triggers. In reality I want to have several more. But if I can do two, I can do any number.

 

--[[
%% properties
%% events
394 CentralSceneEvent
494 CentralSceneEvent   -- <<--- NEW TRIGGER
%% globals
--]]
-- Uses first slave for "CentralSceneEvent" ID (not master, not switch itself). Here 394 and 494 respectively.
 
local trigger = fibaro:getSourceTrigger()
if (trigger.type == "other"then
    fibaro:debug("Scene started by clicking 'start' button")
else
    local triggerID = trigger.event.id --  <<--- WHAT IS CORRECT SYNTAX??
    local pressSource = fibaro:getSourceTrigger().event.data
    fibaro:debug("New: CentralSceneEvent received from key: " .. pressSource.keyId)
    if (pressSource.keyId == 2then
        local ledId = "306" -- <<<< NEEDS TO BE CHANGED TO CORRESPOND TO triggerID
        local ledValue = fibaro:getValue(ledId, "value")
        
        if pressSource.keyAttribute == "Pressed" then
            fibaro:debug("Pressed")
            if ledValue == "0" then
                fibaro:call(ledId, "turnOn")
            else
                fibaro:call(ledId, "turnOff")
            end
        elseif pressSource.keyAttribute == "Pressed2" then
            fibaro:debug("Pressed 2 times")
            fibaro:call(ledId, "setValue""100")
        elseif pressSource.keyAttribute == "HeldDown" then
            fibaro:debug("HeldDown")
 
            -- etc. (complete code not shown)
Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

You could use a different script for each switch. That is at least my solution. But I am also curious, if what you ask is possible.

Link to post
Share on other sites
  • 0

Using a different script for each switch is, of course, possible. But that is precisely what I wanted to avoid, as I increase the number of cases from one to eight! Let's see if someone can come up with a good solution!

Link to post
Share on other sites
  • 0

Well, you can make it arbitrary simple/complex...

--[[
%% properties
%% events
394 CentralSceneEvent
494 CentralSceneEvent   -- <<--- NEW TRIGGER
%% globals
--]]
-- Uses first slave for "CentralSceneEvent" ID (not master, not switch itself). Here 394 and 494 respectively.

local trigger = fibaro:getSourceTrigger()
if trigger.type == "other" then
  fibaro:debug("Scene started by clicking 'start' button")
  return
end

local deviceId = trigger.event.data.deviceId
local keyId = trigger.event.data.keyId
local keyAttribute = trigger.event.data.keyAttribute

local handlers = {

  ["394:2:Pressed"] = function(deviceId,keyId,keyAttribute) -- Match deviceId=394, keyId=2 and keyAttribute="Pressed"
    local ledId = 306 -- <<<< NEEDS TO BE CHANGED TO CORRESPOND TO triggerID
    local ledValue = fibaro:getValue(ledId, "value")
    if ledValue == "0" then
      fibaro:call(ledId, "turnOn")
    else
      fibaro:call(ledId, "turnOff")
    end
  end,

  ["394:2:Pressed2"] = function(deviceId,keyId,keyAttribute)
    fibaro:call(ledId, "setValue", "100")
  end,

  ["394:1:*"] = function(deviceId,keyId,keyAttribute) -- Match deviceId=394, keyId=1 and any keyAttribute...
    ---
  end,

  ["494:1:Pressed"] = function(deviceId,keyId,keyAttribute)
    ---
  end,

  ["494:2:*"] = function(deviceId,keyId,keyAttribute)
    ---
  end,
}

local key = string.format("%d:%s:%s",deviceId,keyId,keyAttribute)
for k,f in pairs(handlers) do
  local  km = k:gsub("([^:]+)",function(s) return "("..(s=='*' and "[^:]+" or s)..")" end)
  local id,kid,pa = key:match(km)
  if id then 
    fibaro:debug("Key "..key.." matched "..k) 
    f(tonumber(id),tonumber(kid),pa) 
    return -- exit after first match
  end
end
fibaro:debug("No handler matched "..key)

 

  • Thanks 1
Link to post
Share on other sites
  • 0

Thanks, @jgab ! I need some time to digest this. It got a little more complicated than I anticipated, but you have given me several clues as to how trigger.event.data calls are set up. I will certainly give it a try.

Link to post
Share on other sites
  • 0

Having reviewed your suggestion, I realize I can do it a little simpler. This is because in my case, the action to be taken is the same for all devices (on/off/change dim level, depending on the attribute). Only the device this is applied to is changed dependent on the trigger deviceId. With a few IF statements and a table of matching device id pairs, my problem is solved. Your contribution was still very valuable, as you showed me the syntax for the three functions/variables:

On 9/14/2020 at 5:30 PM, jgab said:

local deviceId = trigger.event.data.deviceId

local keyId = trigger.event.data.keyId

local keyAttribute = trigger.event.data.keyAttribute

 

Which raises the question: Where are these HC2 variables documented? Cut-and-paste from experts like you is handy, but I would prefer to have a comprehensive documentation available. Previously I have copied and used fibaro:getSourceTrigger().event.data, which is not well documented either. Is there a complete description available anywhere of all these trigger event data variables? 

 

Again - thanks a lot @jgab for solving my immediate problem!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...