dzvents and sensor

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

Moderator: leecollings

Post Reply
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

dzvents and sensor

Post by pvklink »

Case:
I bought a sensor, and when it catches a drop of water it sends about 21 on signals that my rfxcom receives and i can put a switch on when receiving a signal.
When the sensor is all wet, it does not sends signals, it has to dry first. So i place it not horizontal so that the water can drop down.
So my solution is to messure in an hour if the sensor sends x times before i put on a switch
When a switch is put on, it has to stay on for y minutes (it has rain enough)

When this switch is on, my irrigation program does not start (because it has rain enough)

I use the following script(mostly done by @waaren, and this one works perfect for my temperature functionality)
I want to use this script for reading this sensor and put a switch on,

Problem:
1) i think i dont need raintable because this sensor already has a logging table with time and on , how do i use the default data?
2) i like to add more then one weathersituations(weersituatie) for matching the current weather

Code: Select all

-- meet het aantal keer dat de watersensor in een uur een on afgeeft. ALs de sensor nog nat is geeft hij geen on af
-- 
local USAGE_SENSORS =     {
    ['tuinwatersensor'] = 'tuinwatersensor',  -- Adjust to your needs. Between every line you need to add a ",".
                        }

local USAGE_SWITCHES = {
    ['tuinwatersensor'] = 'lantaarn',                     -- Name of device that switch kw_1 On/ Off 
                        } 

local USAGE_Norm = {
    ['tuinwatersensor'] = 5,                  -- is de norm, boven deze waarde gaat het device aan als onoff aanstaat 5
                        }

local USAGE_DurTimer = {
    ['tuinwatersensor'] = 1,                   -- is de periode die verstreken moet zijn na de laatste update van de switch in minuten om uitgezet te worden
                        }

local USAGE_Weersituatie = {
    ['tuinwatersensor'] = 'bewolkt',
                    }

local USAGE_SwitchOnOff = {
    ['tuinwatersensor'] = 'Yes',                -- Adjust to your needs. Between every line you need to add a ",".
                        }

local USAGE_Notify = {
    ['tuinwatersensor'] = 'Yes',
                    }


local USAGE_Pushoverreceivers = {
    ['tuinwatersensor'] = 'pvkmobiel',
                    }

local USAGE_Pushoverprio = {
   ['tuinwatersensor'] = 'PRIORITY_NORMAL',           --domoticz.PRIORITY_LOW, PRIORITY_MODERATE, PRIORITY_NORMAL, PRIORITY_HIGH, PRIORITY_EMERGENCY
                    }

local USAGE_Pushoversound = {
    ['tuinwatersensor'] = 'siren',
                    }

return {
    on      =   {   devices = USAGE_SENSORS },
                
    logging =   { 
                    level   = domoticz.LOG_DEBUG ,                  -- Uncomment to override the dzVents global logging setting
                    marker  = "Watermeting" 
                },
              
    data    =   {
                    raintable = { history = true,  maxHours = 1 },
                    sendnotification = {initial = {}}, 
                },
        
    execute = function(dz, device)
        
        local function secondsToClock(sec)
          local seconds = tonumber(sec)

          if seconds <= 0 then
            return "00:00:00"
          else
            hours = string.format("%02.f", math.floor(seconds/3600))
            mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)))
            secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60))
            return hours..":"..mins..":"..secs
          end
        end
 
        --if dz.data.raintable.getOldest() == nil then  -- Check if history is already filled (initial does not work with history persistent data
          --dz.data.raintable.add(4)                    -- ook deze kun je niet parametriseren
        --end

        local name          = device.name
        local name2         = USAGE_SWITCHES[name]
        local switch        = dz.devices(USAGE_SWITCHES[name])
        local switch2       = dz.devices('Weersituatie')
 
        local onoff         = USAGE_SwitchOnOff[USAGE_SENSORS[name]]
        local norm          = USAGE_Norm[USAGE_SENSORS[name]]
        local Weersituatie  = USAGE_Weersituatie[USAGE_SENSORS[name]]
        local notify        = USAGE_Notify[USAGE_SENSORS[name]]
        local receivers     = USAGE_Pushoverreceivers[USAGE_SENSORS[name]]
        local prio          = USAGE_Pushoverprio[USAGE_SENSORS[name]]
        local sound         = USAGE_Pushoversound[USAGE_SENSORS[name]]

        local DurTimer      = USAGE_DurTimer[USAGE_SENSORS[name]]
        local huidwaarde    = device.state 
        local now           = os.time(os.date('*t'))      

        if huidwaarde == nil then               -- onderstaande checks moeten na de locals omdat ze deze ook gebruiken!
            dz.log("01 Sensor info         = Geen waarde opgehaald")
        else
            dz.data.raintable.add(huidwaarde)
        end

        if dz.data.sendnotification[name] == nil then 
            dz.data.sendnotification[name] = false -- eerste waarde teller op false gezet  -- geen notify gedaan
        end

        local aantal = 0
        for i = dz.data.raintable.size,1,-1 do 
            aantal = aantal +1
            item = dz.data.raintable.get(i)
            dz.log("00 History data van " .. item.time.rawDate .. " " .. item.time.rawTime .. 
                                    " = " .. item.data)
        end
        dz.log("Deviceinfo---------------------------------------")
        dz.log("00 Kwh-device.......................= " .. name)
        dz.log("00 Switch-device....................= " .. name2)
        dz.log("00 Switch.onoff.....................= " .. onoff)
        dz.log("00 Uitzetten.na(in.min).............= " .. DurTimer)
        dz.log("Sensor info-------------------------------------")
        dz.log("00 Aantal.norm......................= " .. norm)
        dz.log("00 Aantal gemeten...................= " .. aantal)
        dz.log("00 Aantal..huidige waarde...........= " .. huidwaarde)
        dz.log("00 Weersituatie.....................= " .. switch2.text)
        dz.log("00 Weersituatie filter..............= " .. Weersituatie)

        dz.log("Notificatie--------------------------------------")

        dz.log("00 Notificatie.switch.laatste update= " .. tostring(switch.lastUpdate.rawTime)) -- deze is goed

        dz.log("00 Notificatie.verzonden............= " .. tostring(dz.data.sendnotification[name]))
        dz.log("00 Notificatie.aan..................= " .. notify)
        dz.log("00 Notificatie.receivers............= " .. receivers)
        dz.log("00 Notificatie.prio.................= " .. prio)
        dz.log("00 Notificatie.sound................= " .. sound)
        dz.log("-------------------------------------------------")

    if  aantal >= norm and string.find(string.lower(tostring(switch2.text)),tostring(Weersituatie) ) ~= nil then 

        if switch.state == 'Off' and onoff == 'Yes' then
            device.cancelQueuedCommands()
            switch.switchOn().checkFirst().forMin(DurTimer)
            msg3=' Switch: ' .. name2 .. ' is aangezet'
        else
            msg3=' '
        end

        if (dz.data.sendnotification[name] == false) then
            if (notify == 'Yes') then 
                msg1 = 'PVK Domoticz device:' .. name2  
                msg2 = 'Gemiddelde temperatuur (' .. aantal .. ') >= (' .. norm .. '),' .. msg3
                dz.notify(msg1,msg2,prio,sound,receivers,dz.NSS_PUSHOVER)
                dz.data.sendnotification[name] = true
            end
        end
        dz.log("05 Sensor info         = Huidig gemiddelde water signalen  (".. aantal ..") is gelijk of boven de norm(".. norm .. "), device: " .. name ..  "")
    end

end
}

Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents and sensor

Post by waaren »

pvklink wrote: Saturday 13 April 2019 11:13 Problem:
1) i think i dont need raintable because this sensor already has a logging table with time and on , how do i use the default data?
2) i like to add more then one weathersituations(weersituatie) for matching the current weather
I don't understand what your question is in Problem 1

If you want to check for a match in a table (requirement in Problem 2) you can use amended script.

Code: Select all

-- meet het aantal keer dat de watersensor in een uur een on afgeeft. ALs de sensor nog nat is geeft hij geen on af
-- 
local USAGE_SENSORS =     {
    ['tuinwatersensor'] = 'tuinwatersensor',  -- Adjust to your needs. Between every line you need to add a ",".
                        }

local USAGE_SWITCHES = {
    ['tuinwatersensor'] = 'lantaarn',                     -- Name of device that switch kw_1 On/ Off 
                        } 

local USAGE_Norm = {
    ['tuinwatersensor'] = 5,                  -- is de norm, boven deze waarde gaat het device aan als onoff aanstaat 5
                        }

local USAGE_DurTimer = {
    ['tuinwatersensor'] = 1,                   -- is de periode die verstreken moet zijn na de laatste update van de switch in minuten om uitgezet te worden
                        }

local USAGE_Weersituatie = { 'bewolkt', 'regen', 'sneeuw', 'hagel' }

local USAGE_SwitchOnOff = {
    ['tuinwatersensor'] = 'Yes',                -- Adjust to your needs. Between every line you need to add a ",".
                        }

local USAGE_Notify = {
    ['tuinwatersensor'] = 'Yes',
                    }

local USAGE_Pushoverreceivers = {
    ['tuinwatersensor'] = 'pvkmobiel',
                    }

local USAGE_Pushoverprio = {
   ['tuinwatersensor'] = 'PRIORITY_NORMAL',           --domoticz.PRIORITY_LOW, PRIORITY_MODERATE, PRIORITY_NORMAL, PRIORITY_HIGH, PRIORITY_EMERGENCY
                    }

local USAGE_Pushoversound = {
    ['tuinwatersensor'] = 'siren',
                    }

return {
    on      =   {   devices = USAGE_SENSORS },
                
    logging =   { 
                    level   = domoticz.LOG_DEBUG ,                  -- Uncomment to override the dzVents global logging setting
                    marker  = "Watermeting" 
                },
              
    data    =   {
                    raintable = { history = true,  maxHours = 1 },
                    sendnotification = {initial = {}}, 
                },
        
    execute = function(dz, device)

        local function inTable(t, searchValue, caseInSensitive)
            for _,v in pairs(t) do
                if (( v == searchValue ) or ( caseInSensitive and ( v:lower() == searchValue:lower()))) then
                    return true
                end
            end
            return false
        end
        
        local function secondsToClock(sec)
          local seconds = tonumber(sec)

          if seconds <= 0 then
            return "00:00:00"
          else
            hours = string.format("%02.f", math.floor(seconds/3600))
            mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)))
            secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60))
            return hours..":"..mins..":"..secs
          end
        end
 
        --if dz.data.raintable.getOldest() == nil then  -- Check if history is already filled (initial does not work with history persistent data
          --dz.data.raintable.add(4)                    -- ook deze kun je niet parametriseren
        --end

        local name          = device.name
        local name2         = USAGE_SWITCHES[name]
        local switch        = dz.devices(USAGE_SWITCHES[name])
        local switch2       = dz.devices('Weersituatie')
 
        local onoff         = USAGE_SwitchOnOff[USAGE_SENSORS[name]]
        local norm          = USAGE_Norm[USAGE_SENSORS[name]]
        local Weersituatie  = USAGE_Weersituatie
        local notify        = USAGE_Notify[USAGE_SENSORS[name]]
        local receivers     = USAGE_Pushoverreceivers[USAGE_SENSORS[name]]
        local prio          = USAGE_Pushoverprio[USAGE_SENSORS[name]]
        local sound         = USAGE_Pushoversound[USAGE_SENSORS[name]]

        local DurTimer      = USAGE_DurTimer[USAGE_SENSORS[name]]
        local huidwaarde    = device.state 
        local now           = os.time(os.date('*t'))      

        if huidwaarde == nil then               -- onderstaande checks moeten na de locals omdat ze deze ook gebruiken!
            dz.log("01 Sensor info         = Geen waarde opgehaald")
        else
            dz.data.raintable.add(huidwaarde)
        end

        if dz.data.sendnotification[name] == nil then 
            dz.data.sendnotification[name] = false -- eerste waarde teller op false gezet  -- geen notify gedaan
        end

        local aantal = 0
        for i = dz.data.raintable.size,1,-1 do 
            aantal = aantal +1
            item = dz.data.raintable.get(i)
            dz.log("00 History data van " .. item.time.rawDate .. " " .. item.time.rawTime .. 
                                    " = " .. item.data)
        end
        dz.log("Deviceinfo---------------------------------------")
        dz.log("00 Kwh-device.......................= " .. name)
        dz.log("00 Switch-device....................= " .. name2)
        dz.log("00 Switch.onoff.....................= " .. onoff)
        dz.log("00 Uitzetten.na(in.min).............= " .. DurTimer)
        dz.log("Sensor info-------------------------------------")
        dz.log("00 Aantal.norm......................= " .. norm)
        dz.log("00 Aantal gemeten...................= " .. aantal)
        dz.log("00 Aantal..huidige waarde...........= " .. huidwaarde)
        dz.log("00 Weersituatie.....................= " .. switch2.text)
        for i=1,#Weersituatie do
            dz.log("00 Weersituatie filter[" .. i .. "]........= " .. Weersituatie[i])
        end
        
        dz.log("Notificatie--------------------------------------")

        dz.log("00 Notificatie.switch.laatste update= " .. tostring(switch.lastUpdate.rawTime)) -- deze is goed

        dz.log("00 Notificatie.verzonden............= " .. tostring(dz.data.sendnotification[name]))
        dz.log("00 Notificatie.aan..................= " .. notify)
        dz.log("00 Notificatie.receivers............= " .. receivers)
        dz.log("00 Notificatie.prio.................= " .. prio)
        dz.log("00 Notificatie.sound................= " .. sound)
        dz.log("-------------------------------------------------")

        if aantal >= norm and inTable(Weersituatie, switch2.text, true) then

        if switch.state == 'Off' and onoff == 'Yes' then
            device.cancelQueuedCommands()
            switch.switchOn().checkFirst().forMin(DurTimer)
            msg3=' Switch: ' .. name2 .. ' is aangezet'
        else
            msg3=' '
        end

        if (dz.data.sendnotification[name] == false) then
            if (notify == 'Yes') then 
                msg1 = 'PVK Domoticz device:' .. name2  
                msg2 = 'Gemiddelde temperatuur (' .. aantal .. ') >= (' .. norm .. '),' .. msg3
                dz.notify(msg1,msg2,prio,sound,receivers,dz.NSS_PUSHOVER)
                dz.data.sendnotification[name] = true
            end
        end
        dz.log("05 Sensor info         = Huidig gemiddelde water signalen  (".. aantal ..") is gelijk of boven de norm(".. norm .. "), device: " .. name ..  "")
    end

end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest