Sunscreen script

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

Moderator: leecollings

Post Reply
Rupserdub
Posts: 1
Joined: Monday 13 July 2020 10:57
Target OS: -
Domoticz version:
Contact:

Sunscreen script

Post by Rupserdub »

Code: Select all

-- Define all the sensors which needs to be considered for the sunscreen to close
local sensors = {

    temperature = {
        active = true,
        device = 'Feel Temperature',
        closeRule = function(device)
            return device.temperature <= 14
        end
    },
    wind = {
        active = true,
        device = 'Wind',
        closeRule = function(device)
            return  device.speed >= 50 or device.gust >= 300
        end
    },
    rain = {
        active = false,
        device = 'Rain Intensity',
        closeRule = function(device)
            return  device.rainRate < 0
        end
    },
    rainExpected = {
        active = true,
        device = 'Rain Intensity', -- This needs to be a virtual sensor of type 'percentage'
        closeRule = function(device)
            return  device.percentage > 0
        end
    },
    uv = {
        active = false,
        device = 'Zonnepanelen',
        closeRule = function(device)
            return device.usage <= 250.000
        end
    },
    Zonnepanelen = {
        active = true,
        device = 'Zonnepanelen Rasp',
        closeRule = function(device)
            return  device.usage  <= 150.000
        end
    }
}   

-- Define the name of your sunscreen device
local sunscreenDevice = 'Zonnescherm'

-- Enable dry run mode to test the sunscreen script without actually activating the sunscreen
local dryRun = false

-- Define the name of a virtual switch which you can use to disable the sunscreen automation script
-- Set to false to disable this feature
local manualOverrideSwitch = false

-- Minutes to wait after a sunscreen close before opening it again.
local timeBetweenOpens = 2

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

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

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

        if (manualOverrideSwitch and domoticz.devices(manualOverrideSwitch).state == 'On') then
            domoticz.log('Automatic sunscreen script is manually disabled', domoticz.LOG_DEBUG)
            return
        end

        local sunscreen = domoticz.devices(sunscreenDevice)

        -- Sunscreen must always be up during nighttime
        if (domoticz.time.isNightTime) then
            switchOff(sunscreen, 'Closing sunscreen, It is night')
            return
        end

        -- 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

                    switchOff(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

        -- All tresholds OK, sunscreen may be lowered
        if (sunscreen.lastUpdate.minutesAgo > timeBetweenOpens) then
            switchOn(sunscreen, 'Sun is shining, all thresholds OK, lowering sunscreen')
        end
    end
}


So i'm working on a script that makes the screen go up when bad weather is coming and that part works, but the problem is that the screen is suppose to work with our solar panels. The part at 'zonnepanelen' is not working properly due to the .usage i'm guessing.

can anyone help me with this?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Sunscreen script

Post by waaren »

Rupserdub wrote: Monday 13 July 2020 11:03 So i'm working on a script that makes the screen go up when bad weather is coming and that part works, but the problem is that the screen is suppose to work with our solar panels. The part at 'zonnepanelen' is not working properly due to the .usage i'm guessing.

can anyone help me with this?
I added a log statement. It will show you the value of the zonnepanelen.usage

Code: Select all

return {
    active = true,
    on = {
        timer = {'every minute'}
    },
    logging = {
        level = domoticz.LOG_DEBUG,
        marker = 'Zonnescherm'
    },
    execute = function(domoticz)
	
	-- Define all the sensors which needs to be considered for the sunscreen to close
local sensors = {

    temperature = {
        active = true,
        device = 'Feel Temperature',
        closeRule = function(device)
            return device.temperature <= 14
        end
    },
    wind = {
        active = true,
        device = 'Wind',
        closeRule = function(device)
            return  device.speed >= 50 or device.gust >= 300
        end
    },
    rain = {
        active = false,
        device = 'Rain Intensity',
        closeRule = function(device)
            return  device.rainRate < 0
        end
    },
    rainExpected = {
        active = true,
        device = 'Rain Intensity', -- This needs to be a virtual sensor of type 'percentage'
        closeRule = function(device)
            return  device.percentage > 0
        end
    },
    uv = {
        active = false,
        device = 'Zonnepanelen',
        closeRule = function(device)
            return device.usage <= 250.000
        end
    },
    Zonnepanelen = {
        active = true,
        device = 'Zonnepanelen Rasp',
        closeRule = function(device)
			domoticz.log('Zonnepanelen usage ' .. tostring(device.usage), domoticz.LOG_DEBUG)
            return  device.usage  <= 150.000
        end
    }
}  

 

-- Define the name of your sunscreen device
local sunscreenDevice = 'Zonnescherm'

-- Enable dry run mode to test the sunscreen script without actually activating the sunscreen
local dryRun = false

-- Define the name of a virtual switch which you can use to disable the sunscreen automation script
-- Set to false to disable this feature
local manualOverrideSwitch = false

-- Minutes to wait after a sunscreen close before opening it again.
local timeBetweenOpens = 2

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

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

        if (manualOverrideSwitch and domoticz.devices(manualOverrideSwitch).state == 'On') then
            domoticz.log('Automatic sunscreen script is manually disabled', domoticz.LOG_DEBUG)
            return
        end

        local sunscreen = domoticz.devices(sunscreenDevice)

        -- Sunscreen must always be up during nighttime
        if (domoticz.time.isNightTime) then
            switchOff(sunscreen, 'Closing sunscreen, It is night')
            return
        end

        -- 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

                    switchOff(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

        -- All tresholds OK, sunscreen may be lowered
        if (sunscreen.lastUpdate.minutesAgo > timeBetweenOpens) then
            switchOn(sunscreen, 'Sun is shining, all thresholds OK, lowering sunscreen')
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: Sunscreen script

Post by Jan Jansen »

I'am playing around with this script, but I run into a problem. OMW does not generate a gust value. I retrieve this value via NodeRed from Buienradar and make it a user variable. I adapted the script to that (I guess?). In the log I see an error that I cannot fix myself. What is wrong?
Log.png
Log.png (46.9 KiB) Viewed 1654 times
Script Sun screen.png
Script Sun screen.png (6.92 KiB) Viewed 1654 times
Thanks in advance!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Sunscreen script

Post by waaren »

Jan Jansen wrote: Monday 27 July 2020 16:36 I'am playing around with this script, but I run into a problem. OMW does not generate a gust value. I retrieve this value via NodeRed from Buienradar and make it a user variable. I adapted the script to that (I guess?). In the log I see an error that I cannot fix myself. What is wrong?
If you post a script issue then please share your complete code and log within code tags </>.
If you need the value of a uservariable you should use something like domoticz.variables('varName').value
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pgas37
Posts: 99
Joined: Wednesday 06 December 2017 19:44
Target OS: -
Domoticz version:
Contact:

Re: Sunscreen script

Post by pgas37 »

Hello,

I am using the script, added some features, all works fine accept for one thing:

Windvlaag_Ri = {
active = true,
device = 'Wind BR',
closeRule = function(device)
return (device.direction >= 60 or device.direction <= 247) and (device.gust >= 13.9) -
end
see script

I want the Wind closeRule be active only if the wind is coming from a direction between 60 and 247degrees. That works fine with the 'or' function:
return (device.direction >= 60 or device.direction <= 247) but
if add e.g. the device.gust,
return (device.direction >= 60 or device.direction <= 247 and device.gust >= 10.8) it does not work.
return (device.direction >= 60 or device.direction <= 247 or device.gust >= 10.8), is a accepted so "return" only accept 1 value. In this case >=60 or <=247 or >=10.8.
I did a lot of experiments in the script but i can not get it to work the way i want (winddirection and gust or winddirection and speed)

Is there a workaround to solve this?

thanks for the help
Paul




Code: Select all

-- werkt samen met script LuxTemp (gemiddelde lux meting)

-- Define all the sensors which needs to be considered for the sunscreen to close
local sensors = {
    temperature = {
        active = true,
        device = 'Temp/hum ms',
        closeRule = function(device)
            return device.temperature <= 10 --15
        end
    },
 
    WindSnelheid_Ri = {
        active = false,
        device = 'Wind BR',
        closeRule = function(device)
            return (device.direction >= 60 or device.direction <= 247 and device.speed >= 10.8)          -- =Bft 6. in m/s. 5 bft= 8..10.7m/s.  6bft= 10.8..13.8 m/s
        end
    },

  
   
    Windvlaag_Ri = {
        active = true,
        device = 'Wind BR',
        closeRule = function(device)
            return (device.direction >= 60 or device.direction <= 247) and (device.gust >= 13.9)             -
         end
    },


    rain = {
        active = true,
        device = 'Rain BR',
        closeRule = function(device)
            return device.rainRate > 0
        end
    },
    rainExpected = {
        active = true,
        device = 'Possible Rain BR', -- This needs to be a virtual sensor of type 'percentage'
        closeRule = function(device)
            return device.state == 'On'
    --[[    return device.percentage > 15 --]]
        end
    },
 
 
 --[[
     uv = {
        active = true,
        device = 'Ultraviolet',
        closeRule = function(device)
            return device.uv <= 3
        end
    },


    lux = {
        active = true,
        device = 'Illuminance',
        closeRule = function(device)
            return device.lux <= 2000
        end
}
--]]


    lux = {
        active = true,
        device = 'Lux dummy gem 30min',                             -- device is gedefinieerd in script 'LuxTemp average'
        closeRule = function(device)
           -- local startmoment = device.lux                          -- toegevoegd
            --print('=== startmoment average 30min:'..startmoment)
            return device.lux <= 300        --3000                            -- Lux_30min_switch staat op 3100Lux
        end
    }

}

-- Define the name of your sunscreen device
local sunscreenDevice = 'sunscreenblinds'

-- Enable dry run mode to test the sunscreen script without actually activating the sunscreen
local dryRun = false

-- Define the name of a virtual switch which you can use to disable the sunscreen automation script
-- Set to false to disable this feature
local manualOverrideSwitch = 'manualOverrideBlinds'
--local manualOverrideSwitch = true           --false

-- Minutes to wait after a sunscreen close before opening it again.
local timeBetweenOpens = 0 --10

return {
    active = true,
    on = {
        timer = {'every minute'}
    },
    logging = {
        
        -- uncomment level en marker om debugging aan te zetten
        --level = domoticz.LOG_DEBUG,
        --marker = 'Sunscreenmarker='
    },

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

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

-- Locals voor Alarm tekstmeldingen-------------------------------------------------------------------------------
domoticz.log ("=Graden=Gusts=Speed=======================================",  domoticz.LOG_DEBUG)
--local graden = domoticz.devices('Wind BR').direction
local graden = 180
domoticz.log ('=Graden===================' ..graden, domoticz.LOG_DEBUG)

local richting = domoticz.devices('Wind BR').directionString
domoticz.log('=windrichting=========' ..richting, domoticz.LOG_DEBUG)

--local Wvlaag = domoticz.devices('Wind BR').gust
local Wvlaag = 14
domoticz.log ("=Windvlaag================" ..Wvlaag, domoticz.LOG_DEBUG)

--local Wsnelh = domoticz.devices('Wind BR').speed
local Wsnelh = 8
domoticz.log ("=Windsnelheid============" ..Wsnelh, domoticz.LOG_DEBUG)

local Ws_niet_afgerond = Wsnelh       --domoticz.devices('Wind BR').speed
local Ws_afgerond = math.floor((Ws_niet_afgerond) * 10)/10       --math.floor((Ws_niet_afgerond + 0.05) * 10)/10
domoticz.log ("=Windsnelheid===afgerond=========" ..Ws_afgerond, domoticz.LOG_DEBUG)
domoticz.log ('=Graden===' ..graden .. ' , Windvlaag: '..Wvlaag .. ', Windsnelheid : ' ..Ws_afgerond, domoticz.LOG_DEBUG)

local Regen = ''
    if domoticz.devices('Is it Raining').state == 'On' then
        Regen = 'Het regent in Bergen'
    else
        Regen = 'Geen regen in Bergen'
        domoticz.log ('=Regen: ' ..Regen, domoticz.LOG_DEBUG)
        end


local lum1 = domoticz.devices('Lux dummy gem 30min').lux
domoticz.log(' lumen======:' ..lum1, domoticz.LOG_DEBUG)

    
-- uitschakelen van de manualOverrideSwitch bij sunrise nadat isNightTime is afgelopen
        local T1 = (domoticz.time.secondsSinceMidnight/60)
        local T2 = (domoticz.time.sunriseInMinutes)
         domoticz.log ("minuten  na middernacht=================" ..T1, domoticz.LOG_DEBUG)
         domoticz.log ("Sunrise in minuten na middernacht=======" ..T2, domoticz.LOG_DEBUG)
         
            if T2 == T1 then
             domoticz.devices(manualOverrideSwitch).switchOff()
            end
         -- ------------------------------------------------------------------------------------


        if (domoticz.devices(manualOverrideSwitch).state == 'On') then              --manualOverrideSwitch and 
            domoticz.log('Automatic sunscreen script is manually disabled', domoticz.LOG_DEBUG)
            return
        end

        
        local sunscreen = domoticz.devices(sunscreenDevice)
 
-- Sunscreen must always be up during nighttime
        if (domoticz.time.isNightTime) then
            switchOff(sunscreen, 'Closing sunscreen, It is night')
                local txt =  ('ManualOverrideSwitch = On.<br> Screens up.<br>Nighttime')
                domoticz.devices('Alarm Wind').updateAlertSensor(1,txt) 
            domoticz.devices(manualOverrideSwitch).switchOn()
            domoticz.devices('Lux_30min_switch').switchOn()         
           return
        end
        
        
-- -----omrekening windsnelheid m/s naar beaufort en knopen-----------------------------------------------------
local Bft = 0
local Kn = 0
         --domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
    if  Ws_afgerond >= 24.5 and Ws_afgerond <= 28.4 then Bft = 10  Kn = math.floor((Ws_afgerond * 1.944) * 10/10)
        domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen========== :' ..Kn, domoticz.LOG_DEBUG)
   end      
         
    if  Ws_afgerond >= 20.8 and Ws_afgerond <= 24.4 then Bft = 9 Kn = math.floor((Ws_afgerond * 1.944) * 10/10)
        domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen========== :' ..Kn, domoticz.LOG_DEBUG)
   end 
   if  Ws_afgerond >= 17.2 and Ws_afgerond <= 20.7 then Bft = 8 Kn = math.floor((Ws_afgerond * 1.944) * 10/10)
        domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen========== :' ..Kn, domoticz.LOG_DEBUG)
   end 
    if Ws_afgerond >= 13.9 and Ws_afgerond <= 17.1 then Bft = 7 Kn = math.floor((Ws_afgerond * 1.944) * 10/10)
        domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen========== :' ..Kn, domoticz.LOG_DEBUG)
    end
    if  Ws_afgerond >= 10.8 and Ws_afgerond <= 13.8 then Bft = 6 Kn = math.floor((Ws_afgerond * 1.944) * 10/10)
        domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen========== :' ..Kn, domoticz.LOG_DEBUG)
   end
       if  Ws_afgerond >= 8.0 and Ws_afgerond <= 10.7 then Bft =  5 Kn = math.floor((Ws_afgerond * 1.944) * 10/10)   --5 Kn = (Ws_afgerond * 1.944) 
        domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen========== :' ..Kn, domoticz.LOG_DEBUG)
   end 
    if  Ws_afgerond >= 5.5 and Ws_afgerond <= 7.9 then Bft = 4 Kn = math.floor((Ws_afgerond * 1.944) * 10/10)
        domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen========== :' ..Kn, domoticz.LOG_DEBUG)
   end
    if  Ws_afgerond >= 3.4 and Ws_afgerond <= 5.4 then Bft = 3 Kn = math.floor((Ws_afgerond * 1.944) * 10/10)
        domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen========== :' ..Kn, domoticz.LOG_DEBUG)
   end 
    if  Ws_afgerond >= 1.6 and Ws_afgerond <= 3.3 then Bft = 2 Kn = math.floor((Ws_afgerond * 1.944) * 10/10)
        domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen========== :' ..Kn, domoticz.LOG_DEBUG)
   end 
    if  Ws_afgerond >= 0.3 and Ws_afgerond <= 1.5 then Bft = 1 Kn = math.floor((Ws_afgerond * 1.944) * 10/10)
        domoticz.log ('=Beaufort========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen========== :' ..Kn, domoticz.LOG_DEBUG)
   end 
   
   

-- -------omrekening windvlagen m/s naar beaufort en knopen--------------------------------
local Bftvl = 0
local Kn_vl  = 0
         --domoticz.log ('=Beaufort windvlaag========== :' ..Bftvl, domoticz.LOG_DEBUG)
    if  Wvlaag >= 24.5 and Wvlaag <= 28.4 then Bftvl = 10 Kn_vl = math.floor((Wvlaag * 1.944) * 10/10)
        domoticz.log ('=Beaufort windvlaag========== :' ..Bft, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen windvlaag========== :' ..Kn_vl, domoticz.LOG_DEBUG)
   end      
         
    if  Wvlaag >= 20.8 and Wvlaag <= 24.4 then Bftvl = 9 Kn_vl = math.floor((Wvlaag * 1.944) * 10/10)
        domoticz.log ('=Beaufort windvlaag==========' ..Bftvl, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen windvlaag========== :' ..Kn_vl, domoticz.LOG_DEBUG)
   end 
   if  Wvlaag >= 17.2 and Wvlaag <= 20.7 then Bftvl = 8 Kn_vl = math.floor((Wvlaag * 1.944) * 10/10)
        domoticz.log ('=Beaufort windvlaag==========' ..Bftvl, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen windvlaag========== :' ..Kn_vl, domoticz.LOG_DEBUG)
   end 
    if Wvlaag >= 13.9 and Wvlaag <= 17.1 then Bftvl = 7 Kn_vl = math.floor((Wvlaag * 1.944) * 10/10)
        domoticz.log ('=Beaufort windvlaag==========' ..Bftvl, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen windvlaag========== :' ..Kn_vl, domoticz.LOG_DEBUG)
    end
    if  Wvlaag >= 10.8 and Wvlaag <= 13.8 then Bftvl = 6 Kn_vl = math.floor((Wvlaag * 1.944) * 10/10)
        domoticz.log ('=Beaufort windvlaag==========' ..Bftvl, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen windvlaag========== :' ..Kn_vl, domoticz.LOG_DEBUG)
   end
       if  Wvlaag >= 8.0 and Wvlaag <= 10.7 then Bftvl = 5 Kn_vl = math.floor((Wvlaag * 1.944) * 10/10)
        domoticz.log ('=Beaufort windvlaag==========' ..Bftvl, domoticz.LOG_DEBUG)
   end 
    if  Wvlaag >= 5.5 and Wvlaag <= 7.9 then Bftvl = 4 Kn_vl = math.floor((Wvlaag * 1.944) * 10/10)
        domoticz.log ('==Beaufort windvlaag==========' ..Bftvl, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen windvlaag========== :' ..Kn_vl, domoticz.LOG_DEBUG)
   end
    if  Wvlaag >= 3.4 and Wvlaag <= 5.4 then Bftvl = 3 Kn_vl = math.floor((Wvlaag * 1.944) * 10/10)
        domoticz.log ('==Beaufort windvlaag==========' ..Bftvl, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen windvlaag========== :' ..Kn_vl, domoticz.LOG_DEBUG)
   end 
    if  Wvlaag >= 1.6 and Wvlaag <= 3.3 then Bftvl = 2 Kn_vl = math.floor((Wvlaag * 1.944) * 10/10)
        domoticz.log ('==Beaufort windvlaag==========' ..Bftvl, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen windvlaag========== :' ..Kn_vl, domoticz.LOG_DEBUG)
   end 
    if  Wvlaag >= 0.3 and Wvlaag <= 1.5 then Bftvl = 1 Kn_vl = math.floor((Wvlaag * 1.944) * 10/10)
        domoticz.log ('=Beaufort windvlaag==========' ..Bftvl, domoticz.LOG_DEBUG)
        domoticz.log ('=Knopen windvlaag========== :' ..Kn_vl, domoticz.LOG_DEBUG)
   end    

-- ---Wind Alarm sensor--text feed-----------------------------------------------------
    -- oorspronkelijke txt string. Vond ik lang en onoverzichtelijk. aangepast hieronder
    
    if (graden >= 60 and graden <= 247) and (Ws_afgerond <= 10.7 or Wvlaag <= 13.9) then
                local txt =  ('<FONT COLOR=red><u>1 Windrichting waarschuwing.</u></FONT><br><br><br><br><br><FONT COLOR=grey>Wrichting tussen (>60..<247): ' ..graden.. 'gr. ' ..richting..'</FONT><br>Wsnelheid niet >10.7m/s: ' ..Ws_afgerond .. 'm/s. Kn:'..Kn..'.  Bft:' ..Bft.. '<br> Wvlaag niet >13.9m/s:.....'..Wvlaag .. 'm/s. Kn_vl:' ..Kn_vl..'.  Bftvl:' ..Bftvl.. '<br>' ..Regen)
                domoticz.devices('Alarm Wind').updateAlertSensor(0,txt)         -- 0= grey, 2= yellow
            else
                --local txt =  ('<FONT COLOR=blue>Geen wind waarschuwing voor sunblinds.</FONT><br><br> Wrichting (>60..<247): ' ..graden.. 'gr. ' .. richting..'<br> Wsnelheid >10.7: ' ..Ws_afgerond .. 'm/s.  Kn: '..Kn..'.  Bft :' ..Bft.. '<br> Wvlaag >13.9: '..Wvlaag .. 'm/s. Kn_vl:' ..Kn_vl..'. Bftvl:' ..Bftvl..'<br>' ..Regen)
                local txt1 = ('<FONT COLOR=blue>Geen wind waarschuwing voor sunblinds.</FONT><br><br><br><br><br>')
                local txt2 = ('<FONT COLOR=green>Wrichting (>60..<247): ' ..graden.. 'gr. ' .. richting..'<br></FONT>') 
                local txt3 = ('Wsnelheid >10.7: ' ..Ws_afgerond .. 'm/s.  Kn: '..Kn..'.  Bft :' ..Bft.. '<br>')
                local txt4 = ('Wvlaag >13.9: '..Wvlaag .. 'm/s. Kn_vl:' ..Kn_vl..'. Bftvl:' ..Bftvl.. '<br>')
                local txt5 = (''..Regen)
                domoticz.devices('Alarm Wind').updateAlertSensor(1,txt1 ..txt2 ..txt3 ..txt4 ..txt5)         -- 1= green
         end  
         
     if     (graden >= 60 and graden <= 247) and (Ws_afgerond >= 10.7) then
                --local txt =  ('<FONT COLOR=red> <u>2 WAARSCHUWING windrichting en snelheid.</u> <br><br>Wrichting tussen (>60..<247): ' ..graden.. 'gr.  ' ..richting..'<br>Wsnelheid >10.7m/s: ' ..Ws_afgerond .. 'm/s. Kn:'..Kn..'.  Bft :' ..Bft..'.<br><FONT COLOR=black> Wvlaag < 13.9:.....'..Wvlaag .. 'm/s. Kn_vl:' ..Kn_vl..'. Bftvl :' ..Bftvl.. '</FONT><br>' ..Regen)
                local txt1 = ('<FONT COLOR=red> <u>2 WAARSCHUWING windrichting en snelheid.</u>')
                local txt2 = ('<br><br><br><br><br>Wrichting tussen (>60..<247): ' ..graden.. 'gr.  ' ..richting..'<br>')
                local txt3 = ('Wsnelheid >10.7m/s: ' ..Ws_afgerond .. 'm/s. Kn:'..Kn..'.  Bft :' ..Bft..'.<br>')
                local txt4 = ('<FONT COLOR=black> Wvlaag < 13.9:.....'..Wvlaag .. 'm/s. Kn_vl:' ..Kn_vl..'. Bftvl :' ..Bftvl.. '</FONT>')
                local txt5 = ('<br>' ..Regen)
                domoticz.devices('Alarm Wind').updateAlertSensor(4,txt1 ..txt2 ..txt3 ..txt4 ..txt5)     --4-red
         end

        
    if     (graden >= 60 and graden <= 247) and (Wvlaag >= 13.9) then
                --local txt =  ('<FONT COLOR=red> <u>3 WAARSCHUWING Windrichting en -vlaag.</u><br> Windrichting tussen (>60..<247): ' ..graden.. 'gr. ' ..richting..'<br> </FONT> <FONT COLOR=black> Windsnelheid <10.7: ' ..Ws_afgerond .. 'm/s. Kn:'..Kn..'.  Bft :' ..Bft.. '.<br><FONT COLOR=red> Windvlaag > 13.9:.....'..Wvlaag .. 'm/s.  Kn_vl:' ..Kn_vl..'. Bftvl :' ..Bftvl.. '</FONT><br>' ..Regen)
                local txt1 = ('<FONT COLOR=red> <u>3 WAARSCHUWING Windrichting en -vlaag.</u><br><br><br><br><br> ')
                local txt2 = ('Windrichting tussen (>60..<247): ' ..graden.. 'gr. ' ..richting..'<br> </FONT>') 
                local txt3 = ('<FONT COLOR=black> Windsnelheid <10.7: '..Ws_afgerond .. 'm/s. Kn:'..Kn..'.  Bft :' ..Bft..'<br>')
                local txt4 = ('<FONT COLOR=red> Windvlaag ......>13.9: '..Wvlaag .. 'm/s. Kn_vl:' ..Kn_vl..'. Bftvl :' ..Bftvl..'</FONT>')
                local txt5 = ('<br>' ..Regen)     
                --local resultString = txt1 ..txt2 ..txt3 ..txt4 ..txt5     -- mogelijkheid om txtStrings te combineren
                domoticz.devices('Alarm Wind').updateAlertSensor(4,txt1 ..txt2 ..txt3 ..txt4 ..txt5)     --4-red
         end
         
        
    if     (graden >= 60 and graden <= 247) and (Ws_afgerond >= 10.7 and Wvlaag >13.9) then
                --local txt =  ('<FONT COLOR=red><b> 4 WAARSCHUWING.</u><br>Wrichting tussen (>60..<247): ' ..graden.. 'gr. = ' ..richting..'<br> Wsnelheid >10.7m/s: ' ..Ws_afgerond .. 'm/s. Kn:'..Kn..' Bft:' ..Bft.. '<br> Wvlaag >13.9m/s: .....'..Wvlaag .. 'm/s. Kn_vl:' ..Kn_vl..' Bftvl: ' ..Bftvl.. '<br></FONT>' ..Regen)
                --    domoticz.devices('Alarm Wind').updateAlertSensor(4,txt)     --4-red
                local txt1 = ('<FONT COLOR=red> <u>4 WAARSCHUWING.</u><br><br><br><br><br>')
                local txt2 = ('Windrichting tussen (>60..<247): ' ..graden.. 'gr. ' ..richting..'<br>') 
                local txt3 = ('Windsnelheid <10.7: '..Ws_afgerond .. 'm/s. Kn:'..Kn..'.  Bft :' ..Bft..'<br>')
                local txt4 = ('Windvlaag ......>13.9: '..Wvlaag .. 'm/s. Kn_vl:' ..Kn_vl..'. Bftvl :' ..Bftvl..'</FONT>')
                local txt5 = ('<br>' ..Regen)     
                --local resultString = txt1 ..txt2 ..txt3 ..txt4 ..txt5     -- mogelijkheid om txtStrings te combineren
                domoticz.devices('Alarm Wind').updateAlertSensor(4,txt1 ..txt2 ..txt3 ..txt4 ..txt5)     --4-red
        end
        
        

--[[
if (graden >= 60 and graden <= 247) and (Ws_afgerond >= 10.7 or Wvlaag >= 13.9) then
   domoticz.notify('Wind Alarm',' WindR, -snelh en -vlaag limieten overschreden.  Gr: ' ..graden .. ' , Windvlg: '..Wvlaag .. ',  Windsn : ' ..Ws_afgerond,domoticz.PRIORITY_NORMAL,domoticz.SOUND_SPACEALARM,nil,domoticz.NSS_PUSHOVER)
end
--]]

  
        
 -- activering van het script indien device 'Lux dummy gem 30min' eenmalig boven de grenswaardelux is geweest
        local Luxgrenswaarde = 3100 --3100
        local Lux_startmoment = domoticz.devices('Lux dummy gem 30min').lux
            
            domoticz.log('Startmoment average 30min: Lux '..Lux_startmoment, domoticz.LOG_DEBUG)
            
        if Lux_startmoment < Luxgrenswaarde and domoticz.devices('Lux_30min_switch').state == 'On' then                   -- switch wordt Off gezet nadat het 30min gemiddelde is overschreden waardoor script locals actief kunnen worden
            domoticz.log('Luxgrenswaarde '..Luxgrenswaarde .. 'Lux niet overschreden. Return', domoticz.LOG_DEBUG)
            return
        else    
            domoticz.devices('Lux_30min_switch').switchOff().checkFirst()
        end
        

      
        -- 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

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

                    domoticz.log(sensorType .. ' treshold exceeded', domoticz.LOG_DEBUG)
        
             -- toegevoegd om de reden van open en dicht vast te leggen   
                txtSBlog = (sensorType.. '  treshold exceeded')
                domoticz.log('==txtSBlog====:  ' .. txtSBlog, domoticz.LOG_DEBUG)
                domoticz.devices('Log open_dicht sunblinds').updateText(txtSBlog)     
             -- --   
             
             -- Return early when we exeed any tresholds
                    return
                end
            else
                domoticz.log('Sensor not active skipping: ' .. sensorType, domoticz.LOG_DEBUG)
            end
        end
        


        

        -- All tresholds OK, sunscreen may be lowered
        if (sunscreen.lastUpdate.minutesAgo > timeBetweenOpens) then
            switchOn(sunscreen, 'Sun is shining, all thresholds OK, lowering sunscreen')
        end
    end
}
User avatar
madpatrick
Posts: 636
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Sunscreen script

Post by madpatrick »

Hi Guys,

I've modified the script for my needs, but the problem is when it has triggered 1 treshold the script stops.
I like to check all sensors and switch those at the treshold until it has checked all sensors.

This is was i have made until now

Code: Select all

local scriptVar             = '-=# TEST sunscreens #=-'

return {
    active = true,
    on = {
        timer = {'every 5 minutes at 07:00 - 21:00'},
        devices = {727,728,290},
        },
    
    logging = {
                state = domoticz.LOG_ERROR,
                marker      = scriptVar 
                },
            
execute = function(domoticz)
	
-- Define all the sensors which needs to be considered for the sunscreen to close
local sensors = {

    Screen_Erker1 = {
        active = true,
        device = 607,
        closeRule = function(device)
            return device.level < 99
        end
    },
    Screen_Erker2 = {
        active = true,
        device = 610,
        closeRule = function(device)
            return  device.level < 99
        end
    },
    Screen_Erker3 = {
        active = true,
        device = 606,
        closeRule = function(device)
            return  device.level < 99
        end
    },
    Screen_Erker4 = {
        active = true,
        device = 609, 
        closeRule = function(device)
            return  device.level < 99
        end
    },
    Screen_Erker5 = {
        active = true,
        device = 611,
        closeRule = function(device)
            return device.level > 99
        end
    },
    Screen_Wnk_gr = {
        active = true,
        device = 493,
        closeRule = function(device)
            return device.level < 99
        end
    },
    Screen_Wnk_kl = {
        active = true,
        device = 612,
        closeRule = function(device)
            return device.level < 99
        end
    },
    Screen_Deur = {
        active = true,
        device = 605,
        closeRule = function(device)
            return device.level < 99
        end
    },
    Screen_Thomas_1 = {
        active = true,
        device = 604,
        closeRule = function(device)
            return device.level < 99
        end
    },
    Screen_Thomas_2 = {
        active = true,
        device = 608,
        closeRule = function(device)
            return device.level < 99
        end
    },
    Screen_Slaapkamer = {
        active = true,
        device = 613,
        closeRule = function(device)
            return device.level < 99
        end
    }
}

local RegenData     = domoticz.devices(445)
local MowerState            = domoticz.devices(727)
local MowerRun              = domoticz.devices(728)
local MowerAction           = domoticz.devices(730)

    if (RegenData.rainRate > 0) and (MowerRun.level == 'On') then
        MowerAction.setLevel(50)
        dz.log('Regen : ' .. dz.utils.round(RegenData.rainRate,2) .. ' mm', dz.LOG_FORCE)
        dz.log('Regen : Mower gaat naar huis', dz.LOG_FORCE)
    end

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

            if (sensor['active'] == true) and (RegenData.rainRate > 0) then
                local device = domoticz.devices(sensor['device'])
                local closeRule = sensor['closeRule']
                domoticz.log('Checking sensor: ' .. sensorType, domoticz.LOG_FORCE)

                if (closeRule(device)) then
                    device.switchOn()
                    domoticz.log(sensorType .. ' treshold exceeded', domoticz.LOG_FORCE)
                    -- Return early when we exeed any tresholds
                    return
                end
            else
                domoticz.log('Sensor not active skipping: ' .. sensorType, domoticz.LOG_FORCE)
            end
        end
    end
}
Thanks !!
-= HP server GEN8 Xeon(R) E3-1220L_V2 -=- OZW -=- Toon2 (rooted) -=- Domoticz v2024.7 -=- Dashticz v3.12b on Tab8" =-
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest