Disable Use of Switch or Group  [Solved]

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

Moderator: leecollings

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

Re: Disable Use of Switch or Group

Post by waaren »

djdevil wrote: Monday 05 August 2019 23:05 The name of switch is "Presenza"
It looks like the script is not triggered.

So please answer these questions:
  • which version are you on (domoticz and dzVents (look an the about page))
  • did you change the name in the script to reflect the name of your device, like below ?
  • is the script saved as dzVents and as 'On'

Code: Select all

-- Secure devices using protect API
-- Add device IDX to protect to deviceList table

return 
{
    on = 
    { devices = { 'Presenza' }}, -- !! change to the name of your switch !!
   
    logging = 
    {
        level = domoticz.LOG_DEBUG,    -- Set to error when script is OK
        marker = 'Secure trigger2'
    },

    execute = function(dz, item)
        local deviceList = {    35,  -- !! Replace /  Add your device IDX numbers here !!
                                2228, 
                                2277,
                           }

        local protectMe = not(item.active) 
        
        local function protect(idx)
            local url = dz.settings['Domoticz url'] ..  '/json.htm?type=setused&used=true' ..
                                                        '&protected=' .. tostring(protectMe) ..
                                                        '&idx=' .. idx
            dz.openURL(url)
        end

        for _, idx in ipairs(deviceList)  do
            protect(idx)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Disable Use of Switch or Group

Post by djdevil »

I'm really stupid 😅 I didn't change the name of the switch! now it works perfectly thank you very much, the only thing I tried to protect a scene but it doesn't work you need to add something to the code? thank you so much
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Disable Use of Switch or Group

Post by waaren »

djdevil wrote: Tuesday 06 August 2019 8:25 I'm really stupid 😅 I didn't change the name of the switch! now it works perfectly thank you very much, the only thing I tried to protect a scene but it doesn't work you need to add something to the code? thank you so much
Good that it works now.
I asked for your version but have not received an answer. In the most recent version of dzVents (2.4.27) the methods protectionOn() and protectionOff() are natively available for devices, scenes and groups.

for now you can use this.

Code: Select all

-- Secure devices, groups, scenes using protect API
-- Add IDX to protect to protectList table

return 
{
    on = 
    { devices = { 'Presenza' }}, -- !! change to the name of your switch !!
   
    logging = 
    {
        level = domoticz.LOG_DEBUG,    -- Set to error when script is OK
        marker = 'Secure trigger2'
    },

    execute = function(dz, item)
        local protectList = {   [35]    = 'device',  -- !! Replace /  Add your device, group , scene IDX numbers here !!
                                [2228]  = 'device',  -- !! and enter to right type (device, scene or group )
                                [44]    = 'group',
                                [43]    = 'scene',
                           }

        local protectMe = not(item.active) 

        local function protect(idx, protectItem)
            local url, sceneType, protectItemObject 
            if protectItem ~= 'device' then 
                if protectItem == 'group' then
                    sceneType = 1
                    protectItemObject = dz.groups(idx)
                elseif protectItem == 'scene' then
                    sceneType = 0
                    protectItemObject = dz.scenes(idx)
                else
                    dz.log('Wrong protectItem. Must be device, scene or group) not: ' .. tostring(protectItem), dz.LOG_ERROR )
                    return    
                end
                url = dz.settings['Domoticz url'] .. '/json.htm?type=updatescene&scenetype=' .. sceneType .. 
                        '&protected=' .. tostring(protectMe) ..
                        '&idx=' .. idx ..
                        '&name='.. dz.utils.urlEncode(protectItemObject.name) ..
                        '&description=' .. dz.utils.urlEncode(protectItemObject.description) 
            elseif protectItem == 'device' then
                url = dz.settings['Domoticz url'] ..  '/json.htm?type=setused&used=true' ..
                      '&protected=' .. tostring(protectMe) ..
                      '&idx=' .. idx
            end
            dz.openURL(url)
        end

        for idx, protectItem in pairs(protectList)  do
            protect(idx, protectItem)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Disable Use of Switch or Group

Post by waaren »

waaren wrote: Tuesday 06 August 2019 9:46
djdevil wrote: Tuesday 06 August 2019 8:25 I'm really stupid 😅 I didn't change the name of the switch! now it works perfectly thank you very much, the only thing I tried to protect a scene but it doesn't work you need to add something to the code? thank you so much
Good that it works now.
I asked for your version but have not received an answer. In the most recent version of dzVents (2.4.27) the methods protectionOn() and protectionOff() are natively available for devices, scenes and groups.

for now you can use this. (untested so please make sure you have a backup of your database)

Code: Select all

-- Secure devices, groups, scenes using protect API
-- Add IDX to protect to protectList table

return 
{
    on = 
    { devices = { 'Presenza' }}, -- !! change to the name of your switch !!
   
    logging = 
    {
        level = domoticz.LOG_DEBUG,    -- Set to error when script is OK
        marker = 'Secure trigger2'
    },

    execute = function(dz, item)
        local protectList = {   [35]    = 'device',  -- !! Replace /  Add your device, group , scene IDX numbers here !!
                                [2228]  = 'device',  -- !! and enter to right type (device, scene or group )
                                [44]    = 'group',
                                [43]    = 'scene',
                           }

        local protectMe = not(item.active) 

        local function protect(idx, protectItem)
            local url, sceneType, protectItemObject 
            if protectItem ~= 'device' then 
                if protectItem == 'group' then
                    sceneType = 1
                    protectItemObject = dz.groups(idx)
                elseif protectItem == 'scene' then
                    sceneType = 0
                    protectItemObject = dz.scenes(idx)
                else
                    dz.log('Wrong protectItem. Must be device, scene or group) not: ' .. tostring(protectItem), dz.LOG_ERROR )
                    return    
                end
                url = dz.settings['Domoticz url'] .. '/json.htm?type=updatescene&scenetype=' .. sceneType .. 
                        '&protected=' .. tostring(protectMe) ..
                        '&idx=' .. idx ..
                        '&name='.. dz.utils.urlEncode(protectItemObject.name) ..
                        '&description=' .. dz.utils.urlEncode(protectItemObject.description) 
            elseif protectItem == 'device' then
                url = dz.settings['Domoticz url'] ..  '/json.htm?type=setused&used=true' ..
                      '&protected=' .. tostring(protectMe) ..
                      '&idx=' .. idx
            end
            dz.openURL(url)
        end

        for idx, protectItem in pairs(protectList)  do
            protect(idx, protectItem)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Disable Use of Switch or Group

Post by djdevil »

Thx for reply i have this version
Version: 4.10717
Build Hash: b38b49e5
Compile Date: 2019-05-09 13:04:08
dzVents Version: 2.4.19
Python Version: 3.4.4 (default, Apr 17 2016, 16:02:33) [GCC 5.3.1 20160409]

how can I upgrade to the latest version?
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Disable Use of Switch or Group  [Solved]

Post by djdevil »

I tried the code and it works perfectly 8-) :) I don't know how to thank you I hope it can be of help also to the other users with the same need
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Disable Use of Switch or Group

Post by waaren »

djdevil wrote: Tuesday 06 August 2019 10:15 how can I upgrade to the latest version?
You are on the latest stable version. If you have a need for a bugfix or function that is only available in a Beta version then you can switch to the Beta channel.
[settings][system] and choose Check for updates (Beta) under Software updates.

If everything is working and without bugs for your environment it is probably better to stay on the stable branch.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Disable Use of Switch or Group

Post by djdevil »

hello the script not work anymore after i change switch with other dummy virtual switcher this is debug

2019-08-06 23:30:30.874 Status: dzVents: Debug: Processing device-adapter for Presenza: Switch device adapter
2019-08-06 23:30:30.875 Status: dzVents: Debug: - Device: Presenza
2019-08-06 23:30:30.909 Status: dzVents: Info: Handling events for: "Presenza", value: "On"
2019-08-06 23:30:30.909 Status: dzVents: Info: Secure trigger2: ------ Start internal script: Script #2: Device: "Presenza (Posizione)", Index: 273
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Disable Use of Switch or Group

Post by waaren »

djdevil wrote: Tuesday 06 August 2019 23:31 hello the script not work anymore after i change switch with other dummy virtual switcher this is debug
2019-08-06 23:30:30.909 Status: dzVents: Info: Secure trigger2: ------ Start internal script: Script #2: Device: "Presenza (Posizione)", Index: 273
Please share your script as it is now and the complete log. If there is a Start of the script there should also be an End or an Error.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Disable Use of Switch or Group

Post by djdevil »

Code: Select all

-- Secure devices, groups, scenes using protect API
-- Add IDX to protect to protectList table

return 
{
    on = 
    { devices = { 'Presenza' }}, -- !! change to the name of your switch !!
   
    logging = 
    {
        level = domoticz.LOG_DEBUG,    -- Set to error when script is OK
        marker = 'Secure trigger2'
    },

    execute = function(dz, item)
        local protectList = {   [42]    = 'Living 2',
                                         [2]    = 'Tapparelle', -- !! Replace /  Add your device, group , scene IDX numbers here !!
                                
                           }

        local protectMe = not(item.active) 

        local function protect(idx, protectItem)
            local url, sceneType, protectItemObject 
            if protectItem ~= 'device' then 
                if protectItem == 'group' then
                    sceneType = 1
                    protectItemObject = dz.groups(idx)
                elseif protectItem == 'scene' then
                    sceneType = 0
                    protectItemObject = dz.scenes(idx)
                else
                    dz.log('Wrong protectItem. Must be device, scene or group) not: ' .. tostring(protectItem), dz.LOG_ERROR )
                    return    
                end
                url = dz.settings['Domoticz url'] .. '/json.htm?type=updatescene&scenetype=' .. sceneType .. 
                        '&protected=' .. tostring(protectMe) ..
                        '&idx=' .. idx ..
                        '&name='.. dz.utils.urlEncode(protectItemObject.name) ..
                        '&description=' .. dz.utils.urlEncode(protectItemObject.description) 
            elseif protectItem == 'device' then
                url = dz.settings['Domoticz url'] ..  '/json.htm?type=setused&used=true' ..
                      '&protected=' .. tostring(protectMe) ..
                      '&idx=' .. idx
            end
            dz.openURL(url)
        end

        for idx, protectItem in pairs(protectList)  do
            protect(idx, protectItem)
        end
    end
}

2019-08-07 00:24:04.519 Status: dzVents: Debug: Processing device-adapter for Presenza: Switch device adapter
2019-08-07 00:24:04.520 Status: dzVents: Debug: dzVents version: 2.4.19
2019-08-07 00:24:04.520 Status: dzVents: Debug: Event triggers:
2019-08-07 00:24:04.520 Status: dzVents: Debug: - Device: Presenza
2019-08-07 00:24:04.587 Status: dzVents: Info: Handling events for: "Presenza", value: "Off"
2019-08-07 00:24:04.587 Status: dzVents: Info: Secure trigger2: ------ Start internal script: Script #2: Device: "Presenza (Posizione)", Index: 273
2019-08-07 00:24:04.587 Status: dzVents: Error (2.4.19): Secure trigger2: Wrong protectItem. Must be device, scene or group) not: Living 2
2019-08-07 00:24:04.587 Status: dzVents: Error (2.4.19): Secure trigger2: Wrong protectItem. Must be device, scene or group) not: Tapparelle
2019-08-07 00:24:04.587 Status: dzVents: Info: Secure trigger2: ------ Finished Script #2

Living 2 is a blind
Tapparalle is a group
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Disable Use of Switch or Group

Post by waaren »

djdevil wrote: Wednesday 07 August 2019 0:25 Living 2 is a blind
Tapparalle is a group
You should not give the name but the baseType (device, group or scene)
Try this.

Code: Select all

-- Secure devices, groups, scenes using protect API
-- Add IDX to protect to protectList table

return 
{
    on = 
    { devices = { 'Presenza' }}, -- !! change to the name of your switch !!
   
    logging = 
    {
        level = domoticz.LOG_DEBUG,    -- Set to error when script is OK
        marker = 'Secure trigger2'
    },

    execute = function(dz, item)
        local protectList = {   [42]    = 'device',
                                 [2]    = 'group', -- !! Replace /  Add your device, group , scene IDX numbers here !!
                           }

        local protectMe = not(item.active) 

        local function protect(idx, protectItem)
            local url, sceneType, protectItemObject 
            if protectItem ~= 'device' then 
                if protectItem == 'group' then
                    sceneType = 1
                    protectItemObject = dz.groups(idx)
                elseif protectItem == 'scene' then
                    sceneType = 0
                    protectItemObject = dz.scenes(idx)
                else
                    dz.log('Wrong protectItem. Must be device, scene or group) not: ' .. tostring(protectItem), dz.LOG_ERROR )
                    return    
                end
                url = dz.settings['Domoticz url'] .. '/json.htm?type=updatescene&scenetype=' .. sceneType .. 
                        '&protected=' .. tostring(protectMe) ..
                        '&idx=' .. idx ..
                        '&name='.. dz.utils.urlEncode(protectItemObject.name) ..
                        '&description=' .. dz.utils.urlEncode(protectItemObject.description) 
            elseif protectItem == 'device' then
                url = dz.settings['Domoticz url'] ..  '/json.htm?type=setused&used=true' ..
                      '&protected=' .. tostring(protectMe) ..
                      '&idx=' .. idx
            end
            dz.openURL(url)
        end

        for idx, protectItem in pairs(protectList)  do
            protect(idx, protectItem)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Disable Use of Switch or Group

Post by djdevil »

now it works perfectly thank you very much for the time you gave me to solve the problem :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest