Page 1 of 1

One script for 2 identical switches

Posted: Saturday 26 September 2020 13:47
by nitpicker
I can't figure this out and I hope someone can help me. I'm sure it is very simple, but I don't see it... :|

This one works, with one switch

Code: Select all

     return {
        on = {
          devices = {
             'A'
          }
        },
        execute = function(domoticz, switch)
          if (switch.state == 'B1') then
             domoticz.devices('Light A').switchOn().checkFirst()
          elseif (switch.state == 'B4') then
             domoticz.devices('Light A').switchOff().checkFirst()
          end
        end
    }
I tried the following for 2 switches:

Code: Select all

return 
{
    on = { devices = {  'A', 'B' }},

    execute = function(dz, triggerObject)
        if dz.devices('A').state == 'B1' then
            dz.devices('Light A').switchOn().checkFirst()
        elseif dz.devices('B').state == 'B1' then
            dz.devices('Light B').switchOn().checkFirst()
        elseif dz.devices('A').state == 'B4' then
            dz.devices('Light A').switchOff().checkFirst()
        elseif dz.devices('B').state == 'B4' then
            dz.devices('Light B').switchOff().checkFirst()
        end
        dz.log('Device ' .. triggerObject.name .. ' state: ' .. triggerObject.state, dz.LOG_INFO)
    end
}
But it doesn't work.. I don't see why...

Re: One script for 2 identical switches

Posted: Saturday 26 September 2020 14:16
by waaren
nitpicker wrote: Saturday 26 September 2020 13:47 I can't figure this out and I hope someone can help me. I'm sure it is very simple, but I don't see it... :|
Cannot be sure without seeing the log but I guess it's because only the first action after the it/elseif, evaluating to true, is executed. So if

dz.devices('A').state == 'B1' then only dz.devices('Light A').switchOn().checkFirst() wil be executed.


Can you try this?

Code: Select all

return 
{
    on = { devices = {  'A', 'B' }},

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'AB switcher',
    },
    
    execute = function(dz, item)

        dz.log('Device ' .. item.name .. ' state: ' .. item.state, dz.LOG_DEBUG)

        local light = dz.devices('Light ' .. item.name)

        if item.state == 'B1' then
            light.switchOn().checkFirst()
        elseif item.state == 'B4'then
            light.switchOff().checkFirst()
        end

    end
}

Re: One script for 2 identical switches

Posted: Saturday 26 September 2020 15:14
by nitpicker
Too bad, nothing in the logs and not working...

I can create 2 small scripts, but I was convinced it should also working in one script.

Re: One script for 2 identical switches

Posted: Saturday 26 September 2020 15:26
by waaren
nitpicker wrote: Saturday 26 September 2020 15:14 Too bad, nothing in the logs and not working...
Are you sure the A or B device are updated in domoticz? If so and dzVents is enabled you should see at least that the script is triggered.

Re: One script for 2 identical switches  [Solved]

Posted: Saturday 26 September 2020 16:04
by nitpicker
My mistake... I set one device 'unused' instead of using the $, so the switch didn't work.

It works like a charm! Thanks!