Page 1 of 1

Script modification for switching devices by rain

Posted: Thursday 20 June 2024 19:45
by madpatrick
Hi,

I need a bit of help to modify this script which i've found here : https://domoticaboard.nl/index.php?topic=44.0
The script is running, but i like to trigger more devices then the one now
How can i add a list of devices that will be switch by the script

So far i know you can do it with the funcrion "pairs"

Code: Select all

-- Define all the sensors which needs to be considered for the sunscreen to close
local sensors = {
    rain = {
        active = true,
        device = 'RegenData',
        closeRule = function(device)
            return device.rainRate > 0
        end
    }
}

local sunscreenDevice   = 'TestSwitch'      -- Define the name of your sunscreen device
local dryRun            = false             -- Enable dry run mode to test the sunscreen script without actually activating the sunscreen


return {
    active = true,
    on = {
        --timer = {'every minute'},
        devices = {290},
    },
    logging = {
        level = domoticz.LOG_DEBUG,
        marker = 'Sunscreen'
    },
    execute = function(domoticz)

        local function switchOn(sunscreen)
            if (sunscreen.state == 'Off') then
                if (not dryRun) then
                sunscreen.switchOn()
                --domoticz.notify('Sunscreen', message)
                end
                domoticz.log(message, domoticz.LOG_INFO)
            end
        end

        local function switchOff(sunscreen)
            if (sunscreen.state == 'On') then
                if (not dryRun) then
                sunscreen.switchOff()
                --domoticz.notify('Sunscreen', message)
                end
                domoticz.log(message, domoticz.LOG_INFO)
            end
        end

        local sunscreen = domoticz.devices(sunscreenDevice)

        -- Check all sensor tresholds and if any exeeded close sunscreen
        for sensorType, sensor in pairs(sensors) do

            if (sensor['active'] == true) then

                local device = domoticz.devices(sensor['device'])
                local closeRule = sensor['closeRule']

                domoticz.log('Checking sensor: ' .. sensorType, domoticz.LOG_DEBUG)

                if (closeRule(device)) then

                    switchOn(sunscreen, sensorType .. ' treshold exceeded, Sunscreen up')

                    domoticz.log(sensorType .. ' treshold exceeded', domoticz.LOG_DEBUG)
                    -- Return early when we exeed any tresholds
                    return
                end
            else
                domoticz.log('Sensor not active skipping: ' .. sensorType, domoticz.LOG_DEBUG)
            end
        end

    end
}

Re: Script modification for switching devices by rain

Posted: Thursday 20 June 2024 22:07
by waltervl
You can also make a Domoticz group or scene, add the devices you want to switch to the group and switch the group by the script.

Else you have to change the switching(sunscreen) function....

Re: Script modification for switching devices by rain

Posted: Friday 21 June 2024 15:26
by madpatrick
Thanks. Maing a group was also the easiest solution i could think of.
It is working !