One script for 2 identical switches  [Solved]

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

Moderator: leecollings

Post Reply
nitpicker
Posts: 69
Joined: Tuesday 29 July 2014 10:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Contact:

One script for 2 identical switches

Post 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...
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: One script for 2 identical switches

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
nitpicker
Posts: 69
Joined: Tuesday 29 July 2014 10:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Contact:

Re: One script for 2 identical switches

Post 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.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: One script for 2 identical switches

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
nitpicker
Posts: 69
Joined: Tuesday 29 July 2014 10:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Contact:

Re: One script for 2 identical switches  [Solved]

Post 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!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest