Page 1 of 1

notification script in case of rain problem

Posted: Monday 10 May 2021 18:58
by djdevil
Hi everyone I have this script that warns me in case of rain by sending a telegram notification, but I want this notification to arrive only once every 12 hours even if the sensor returns to the on phase after 10 minutes! I don't know why but it continually sends me the notification, where am I wrong thanks

Code: Select all

local scriptVar = 'Pioggia'

return
{
    on =
    {
        devices =
        {
            'Sensore Pioggia',
            -- change to name of device to monitor
        },
        customEvents =
        {
            scriptVar,
        },
    },

    data =
    {
       
        notificationsSend =
        {
                history = true,
                maxHours = 6,         -- Only 1 notification every 4 hours change to how many you want
        },
    logging =
    {
        level = domoticz.LOG_ERROR, -- change to doomticz.LOG_DEBUG when script works without issues
    }
    },

  

    execute = function(dz, item)
        
        local Presenza = dz.devices('Presenza')
        local Sensore = dz.devices('Sensore Pioggia')
        local buonanotte = dz.devices('Buonanotte')
      
       
        if dz.time.matchesRule('at 09:00-22:00') then
       
            if (Sensore.state == "On"  ) and ( dz.data.notificationsSend.sum() < 1 ) and (Presenza.state == "On") and (buonanotte.state == "Off")then
             
               	dz.notify("Meteo", 'Fuori sta piovendo', dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , dz.NSS_TELEGRAM)
                
               dz.executeShellCommand('/home/pi/domoticz/alexa_remote_control.sh -d "Zona Giorno" -e speak:"Attenzione fuori sta piovendo"')
               
              
             end
               
             
       
          end
                
            
        end
    
}

Re: notification script in case of rain problem  [Solved]

Posted: Monday 10 May 2021 19:35
by waaren
djdevil wrote: Monday 10 May 2021 18:58 Hi everyone I have this script that warns me in case of rain by sending a telegram notification, but I want this notification to arrive only once every 12 hours even if the sensor returns to the on phase after 10 minutes! I don't know why but it continually sends me the notification, where am I wrong thanks
Can you try below version

Code: Select all

local scriptVar = 'Pioggia'

return
{
    on =
    {
        devices =
        {
            'Sensore Pioggia', -- change to name of device to monitor
        },
    },

    data =
    {

        notificationsSend =
        {
                history = true,
                maxHours = 12,         -- Only 1 notification every 12 hours change to how many you want
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to doomticz.LOG_ERROR when script works without issues
        marker = scriptVar,
    },

    execute = function(dz, item)
        local Presenza = dz.devices('Presenza')
        local Sensore = dz.devices('Sensore Pioggia')
        local buonanotte = dz.devices('Buonanotte')


        if dz.time.matchesRule('at 09:00-22:00') then
            if (Sensore.state == "On"  ) and ( dz.data.notificationsSend.sum() < 1 ) and (Presenza.state == "On") and (buonanotte.state == "Off")then
               dz.data.notificationsSend.add(1)
               dz.notify("Meteo", 'Fuori sta piovendo', dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , dz.NSS_TELEGRAM)
               dz.executeShellCommand('/home/pi/domoticz/alexa_remote_control.sh -d "Zona Giorno" -e speak:"Attenzione fuori sta piovendo"')
            end
        end
    end
}

Re: notification script in case of rain problem

Posted: Tuesday 11 May 2021 18:04
by djdevil
waaren wrote: Monday 10 May 2021 19:35
djdevil wrote: Monday 10 May 2021 18:58 Hi everyone I have this script that warns me in case of rain by sending a telegram notification, but I want this notification to arrive only once every 12 hours even if the sensor returns to the on phase after 10 minutes! I don't know why but it continually sends me the notification, where am I wrong thanks
Can you try below version

Code: Select all

local scriptVar = 'Pioggia'

return
{
    on =
    {
        devices =
        {
            'Sensore Pioggia', -- change to name of device to monitor
        },
    },

    data =
    {

        notificationsSend =
        {
                history = true,
                maxHours = 12,         -- Only 1 notification every 12 hours change to how many you want
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to doomticz.LOG_ERROR when script works without issues
        marker = scriptVar,
    },

    execute = function(dz, item)
        local Presenza = dz.devices('Presenza')
        local Sensore = dz.devices('Sensore Pioggia')
        local buonanotte = dz.devices('Buonanotte')


        if dz.time.matchesRule('at 09:00-22:00') then
            if (Sensore.state == "On"  ) and ( dz.data.notificationsSend.sum() < 1 ) and (Presenza.state == "On") and (buonanotte.state == "Off")then
               dz.data.notificationsSend.add(1)
               dz.notify("Meteo", 'Fuori sta piovendo', dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , dz.NSS_TELEGRAM)
               dz.executeShellCommand('/home/pi/domoticz/alexa_remote_control.sh -d "Zona Giorno" -e speak:"Attenzione fuori sta piovendo"')
            end
        end
    end
}
Waaren Now it works perfectly was that the problem? Thx

Code: Select all

 dz.data.notificationsSend.add(1)

Re: notification script in case of rain problem

Posted: Tuesday 11 May 2021 19:35
by waaren
djdevil wrote: Tuesday 11 May 2021 18:04 was that the problem? Thx

Code: Select all

 dz.data.notificationsSend.add(1)
Yes, your original script logic checked the amount of entries in this historical persistent variable but it was never populated.

Re: notification script in case of rain problem

Posted: Tuesday 11 May 2021 22:23
by djdevil
waaren wrote: Tuesday 11 May 2021 19:35
djdevil wrote: Tuesday 11 May 2021 18:04 was that the problem? Thx

Code: Select all

 dz.data.notificationsSend.add(1)
Yes, your original script logic checked the amount of entries in this historical persistent variable but it was never populated.
Thanks again waaren 8-)