Page 1 of 1

Switch dzvents script to off

Posted: Tuesday 18 August 2020 20:52
by DRB2019
Hello,

I'am running the script below to switch 2 lights on in my living room, but
sometimes i do not want the script to run.

1. Is it possible with a dummy switch in domoticz to switch the script to off (i mean the switch on/off in the events editor),
2. or is it better to implement a dummy switch state in the script to exit the script when this switch is on.

If the answer is option 2, can you please tell me how to implement this in the script below.

Thnx in advance,
René

Code: Select all

return
{
    on = {
        timer   = {'every 10 minutes between 15:30 and 22:00'},  
    },

    logging =
    {
        level = domoticz.LOG_ERROR,  -- change to LOG_ERROR when ok (was LOG_DEBUG)
        marker = 'dusk sensor',
    },

    execute = function(domoticz)
        local dusk = domoticz.devices('Schemerschakelaar1')

        if  dusk.state == 'On' and domoticz.devices('Staande_lamp').state == 'Off' then
            domoticz.devices('Staande_lamp').switchOn()
            domoticz.devices('Dressoir').switchOn()
            domoticz.log('Device ' .. item.name .. ' was updated', domoticz.LOG_INFO)
        elseif 
            dusk.state == 'Off' and domoticz.devices('Staande_lamp').state == 'On' then
            domoticz.devices('Staande_lamp').switchOff()
            domoticz.devices('Dressoir').switchOff()
        end
    end
}

Re: Switch dzvents script to off  [Solved]

Posted: Tuesday 18 August 2020 22:31
by waaren
DRB2019 wrote: Tuesday 18 August 2020 20:52 I'am running the script below to switch 2 lights on in my living room, but
sometimes i do not want the script to run.
Two options; both based on a virtual switch. (when switch is active, script continues)

option 1 => Advantage: script is not executed at all. Disadvantage: check is done on every device update and every minute

Code: Select all

return
{
    active = function(domoticz) 
                return domoticz.devices('scriptController').active
             end,
             
    on = 
    {
        timer   = 
        {
            'every 10 minutes between 15:30 and 22:00',
        },  
    },

    logging =
    {
        level = domoticz.LOG_ERROR,  -- change to LOG_ERROR when ok (was LOG_DEBUG)
        marker = 'dusk sensor',
    },

    execute = function(domoticz)
        local dusk = domoticz.devices('Schemerschakelaar1')

        if  dusk.state == 'On' and domoticz.devices('Staande_lamp').state == 'Off' then
            domoticz.devices('Staande_lamp').switchOn()
            domoticz.devices('Dressoir').switchOn()
            domoticz.log('Device ' .. item.name .. ' was updated', domoticz.LOG_INFO)
        elseif 
            dusk.state == 'Off' and domoticz.devices('Staande_lamp').state == 'On' then
            domoticz.devices('Staande_lamp').switchOff()
            domoticz.devices('Dressoir').switchOff()
        end
    end
}


option 2 => Advantage: Less checks. Disadvantage: Script need to execute before check is done.

Code: Select all

return
{
    on = 
    {
        timer   = 
        {
            'every 10 minutes between 15:30 and 22:00',
        },  
    },

    logging =
    {
        level = domoticz.LOG_ERROR,  -- change to LOG_ERROR when ok (was LOG_DEBUG)
        marker = 'dusk sensor',
    },

    execute = function(domoticz)
        if not( domoticz.devices('scriptController').active ) then
              return
       	end
        
        local dusk = domoticz.devices('Schemerschakelaar1')

        if  dusk.state == 'On' and domoticz.devices('Staande_lamp').state == 'Off' then
            domoticz.devices('Staande_lamp').switchOn()
            domoticz.devices('Dressoir').switchOn()
            domoticz.log('Device ' .. item.name .. ' was updated', domoticz.LOG_INFO)
        elseif 
            dusk.state == 'Off' and domoticz.devices('Staande_lamp').state == 'On' then
            domoticz.devices('Staande_lamp').switchOff()
            domoticz.devices('Dressoir').switchOff()
        end
    end
}

Re: Switch dzvents script to off

Posted: Wednesday 19 August 2020 19:18
by DRB2019
Hello waaren,
Thnx for the quick reply!, at this moment i go for option 1.

I 'am able to check the script tonight. I will let you know.

waaren wrote: Tuesday 18 August 2020 22:31 Two options; both based on a virtual switch. (when switch is active, script continues)

Re: Switch dzvents script to off

Posted: Wednesday 19 August 2020 20:46
by DRB2019
waaren wrote: Tuesday 18 August 2020 22:31 Two options; both based on a virtual switch. (when switch is active, script continues)
The script works perfect !!
I also made a new script for when then dummy switch is switched to off.

Code: Select all

return
{
    on = {
         devices = {'SchemerControl'}
    },

    logging =
    {
        level = domoticz.LOG_ERROR, 
    },
    execute = function(domoticz, item)
        if  item.state == 'Off' 
        then
            domoticz.devices('Staande_lamp').switchOff()
            domoticz.devices('Dressoir').switchOff()
        end
    end
}