Page 1 of 2

How to send notifications with a delay

Posted: Tuesday 07 April 2020 11:45
by gschmidt
Hi,

I am trying to create a dzVents script which sends notifications to telegram/Domoticz Log after a Switch is turned ON/OFF.
There must be 4 posibilities:

1. Switch ON -->Notification send (this works)
2. Switch ON -->Notification send 15 minutes after switch is turned ON (not working yet)
2. Switch ON -->Notification send 60 minutes after switch is turned ON (not working yet)
4. Switch OFF -->Notification send (this works)

Here is what I have sofar:

Code: Select all

-- Frietpan dzVents script

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

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Test Schakelaar',
    },

    execute = function(dz, item)
        local token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        local chatid = XXXXXXXXXXXXXXX
        local msgAan = 'De frietpan is aangezet!'
        local msgKlaar = 'De frietpan is klaar om te frituren!'
        local msgWordt = 'De frietpan staat nog aan en wordt nu uitgezet!'
        local msgUit = 'De frietpan is uitgezet!'
        
        if (item.state == 'On') then
            os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..msgAan..'"  "https://api.telegram.org/bot'..token..'/sendMessage" ')
            dz.log(msgAan,dz.LOG_DEBUG)
        
        elseif (item.state == 'On' and "time is 15 minutes later after switch is turned ON") then
            os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..msgKlaar..'"  "https://api.telegram.org/bot'..token..'/sendMessage" ')
            dz.log(msgKlaar,dz.LOG_DEBUG)
        
        elseif (item.state == 'On' and "time is 60 minutes later  after switch is turned ON") then
            item.switchOff();
            os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..msgWordt..'"  "https://api.telegram.org/bot'..token..'/sendMessage" ')
            dz.log(msgWordt,dz.LOG_DEBUG)

        elseif (item.state == 'Off') then
            os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..msgUit..'"  "https://api.telegram.org/bot'..token..'/sendMessage" ')
            dz.log(msgUit,dz.LOG_DEBUG)
        end
    end
}
For the 1st and 2nd ELSEIF I need a value which is a certain amount of time after the Switch is turned ON.
How does this work in dzVents?

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 13:06
by waaren
gschmidt wrote: Tuesday 07 April 2020 11:45
I am trying to create a dzVents script which sends notifications to telegram/Domoticz Log after a Switch is turned ON/OFF.
There must be 4 possibilities:

1. Switch ON -->Notification send (this works)
2. Switch ON -->Notification send 15 minutes after switch is turned ON (not working yet)
2. Switch ON -->Notification send 60 minutes after switch is turned ON (not working yet)
4. Switch OFF -->Notification send (this works)
You could use the newly introduced customEvent for this.
Any reason why you don't use the native dz.notify ?

Code: Select all

-- Frietpan dzVents script

local myDevice =  'Test'
local myEvent = 'Friet'

return 
{
    on = 
    { 
        devices = 
        {
            myDevice,            
        },
        customEvent =
        {
            myEvent,
        },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Test Schakelaar',
    },

    execute = function(dz, item)
        
        myDevice = dz.devices(myDevice)
        
        local msgAan = 'De frietpan is aangezet!'
        local msgKlaar = 'De frietpan is klaar om te frituren!'
        local msgWordt = 'De frietpan staat nog aan en wordt nu uitgezet!'
        local msgUit = 'De frietpan is uitgezet!'
        
        local klaarDelay = 15
        local OffDelay = 60 - klaarDelay
        
        local function sendMessage(message, emitMinutes)
            -- Native dzVents / domoticz method
            dz.notify(dz.moduleLabel, message, dz.PRIORITY_MODERATE, nil, nil, dz.NSS_TELEGRAM)

            -- OS approach
            local token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            local chatid = XXXXXXXXXXXXXXX
            -- os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..msgAan..'"  "https://api.telegram.org/bot'..token..'/sendMessage"  &')

            dz.log(messsage,dz.LOG_DEBUG)
            if emitMinutes ~= nil then dz.emitEvent(myEvent, emitMinutes).afterMin(emitMinutes) end
            --  if emitMinutes ~= nil then dz.emitEvent(myEvent, emitMinutes).afterSec(emitMinutes) end -- for test use seconds
        end

        if item.isDevice and item.state == 'On' then
            sendMessage(msgAan, klaarDelay)
        elseif item.isCustomEvent and myDevice.state == 'On' and tonumber(item.data) == klaarDelay then
            sendMessage(msgKlaar, OffDelay)
        elseif item.isCustomEvent and myDevice.state == 'On' and tonumber(item.data) == OffDelay then 
            myDevice.switchOff().silent()
            sendMessage(msgWordt)
        elseif (item.state == 'Off') then
            sendMessage(msgUit)
        end

    end
}

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 15:20
by gschmidt
waaren wrote: Tuesday 07 April 2020 13:06 You could use the newly introduced customEvent for this.
Thanx man, I will study and test the script.
Where can I find the documentation for dzVents customEvent?
waaren wrote: Tuesday 07 April 2020 13:06 Any reason why you don't use the native dz.notify ?
I didn't know that dz.notify existed...just looked for an example and the first one I got was the os.execute example
I guess dz.notify is using the credentials I set for Telegram at the domoticz settings page?

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 15:26
by gschmidt
waaren wrote: Tuesday 07 April 2020 13:06
gschmidt wrote: Tuesday 07 April 2020 11:45
I am trying to create a dzVents script which sends notifications to telegram/Domoticz Log after a Switch is turned ON/OFF.
There must be 4 possibilities:

1. Switch ON -->Notification send (this works)
2. Switch ON -->Notification send 15 minutes after switch is turned ON (not working yet)
2. Switch ON -->Notification send 60 minutes after switch is turned ON (not working yet)
4. Switch OFF -->Notification send (this works)
You could use the newly introduced customEvent for this.
Any reason why you don't use the native dz.notify ?

Code: Select all

-- Frietpan dzVents script

local myDevice =  'Test'
local myEvent = 'Friet'

return 
{
    on = 
    { 
        devices = 
        {
            myDevice,            
        },
        customEvent =
        {
            myEvent,
        },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Test Schakelaar',
    },

    execute = function(dz, item)
        
        myDevice = dz.devices(myDevice)
        
        local msgAan = 'De frietpan is aangezet!'
        local msgKlaar = 'De frietpan is klaar om te frituren!'
        local msgWordt = 'De frietpan staat nog aan en wordt nu uitgezet!'
        local msgUit = 'De frietpan is uitgezet!'
        
        local klaarDelay = 15
        local OffDelay = 60 - klaarDelay
        
        local function sendMessage(message, emitMinutes)
            -- Native dzVents / domoticz method
            dz.notify(dz.moduleLabel, message, dz.PRIORITY_MODERATE, nil, nil, dz.NSS_TELEGRAM)

            -- OS approach
            local token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            local chatid = XXXXXXXXXXXXXXX
            -- os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..msgAan..'"  "https://api.telegram.org/bot'..token..'/sendMessage"  &')

            dz.log(messsage,dz.LOG_DEBUG)
            if emitMinutes ~= nil then dz.emitEvent(myEvent, emitMinutes).afterMin(emitMinutes) end
            --  if emitMinutes ~= nil then dz.emitEvent(myEvent, emitMinutes).afterSec(emitMinutes) end -- for test use seconds
        end

        if item.isDevice and item.state == 'On' then
            sendMessage(msgAan, klaarDelay)
        elseif item.isCustomEvent and myDevice.state == 'On' and tonumber(item.data) == klaarDelay then
            sendMessage(msgKlaar, OffDelay)
        elseif item.isCustomEvent and myDevice.state == 'On' and tonumber(item.data) == OffDelay then 
            myDevice.switchOff().silent()
            sendMessage(msgWordt)
        elseif (item.state == 'Off') then
            sendMessage(msgUit)
        end

    end
}
I get the following error:

Code: Select all

2020-04-07 15:24:27.463 Error: dzVents: Error: (3.0.1) error loading module 'Script #2' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Script #2.lua':
2020-04-07 15:24:27.463 ...domoticz/scripts/dzVents/generated_scripts/Script #2.lua:20: '}' expected (to close '{' at line 7) near 'logging'

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 15:33
by gschmidt
I think it was the "on =" closing brace },

After fixing this I got the next error:

Code: Select all

2020-04-07 15:29:22.866 Error: dzVents: Error: (3.0.1) Test Schakelaar: An error occurred when calling event handler Script #2
2020-04-07 15:29:22.866 Error: dzVents: Error: (3.0.1) Test Schakelaar: /home/pi/domoticz/dzVents/runtime/Domoticz.lua:115: attempt to concatenate a nil value (local 'subject')

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 15:46
by waaren
gschmidt wrote: Tuesday 07 April 2020 15:33 I think it was the "on =" closing brace },

After fixing this I got the next error:

Code: Select all

2020-04-07 15:29:22.866 Error: dzVents: Error: (3.0.1) Test Schakelaar: An error occurred when calling event handler Script #2
2020-04-07 15:29:22.866 Error: dzVents: Error: (3.0.1) Test Schakelaar: /home/pi/domoticz/dzVents/runtime/Domoticz.lua:115: attempt to concatenate a nil value (local 'subject')
I missed an 's' in customEvents :oops:

Code: Select all

-- Frietpan dzVents script

local myDevice =  'Test'
local myEvent = 'Friet'

return 
{
    on = 
    { 
        devices = 
        {
            myDevice,            
        },
        customEvents =
        {
            myEvent,
        },
    },
    
    logging = 
    {
        level = domoticz.LOG_DEBUG,  -- change to LOG_ERROR when ok
        marker = 'Test Schakelaar',
    },

    execute = function(dz, item)
        
        myDevice = dz.devices(myDevice)
        
        local msgAan = 'De frietpan is aangezet!'
        local msgKlaar = 'De frietpan is klaar om te frituren!'
        local msgWordt = 'De frietpan staat nog aan en wordt nu uitgezet!'
        local msgUit = 'De frietpan is uitgezet!'
        
        local klaarDelay = 15
        local OffDelay = 60 - klaarDelay
        
        local function sendMessage(message, emitMinutes)
            
            -- Native dzVents / domoticz method
            dz.notify(dz.moduleLabel, message, dz.PRIORITY_MODERATE, nil, nil, dz.NSS_TELEGRAM)
            
            -- OS approach
            local token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            local chatid = XXXXXXXXXXXXXXX
            -- os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..msgAan..'"  "https://api.telegram.org/bot'..token..'/sendMessage"  &')
            
            dz.log(message,dz.LOG_DEBUG)
            if emitMinutes ~= nil then dz.emitEvent(myEvent, emitMinutes).afterMin(emitMinutes) end
            -- if emitMinutes ~= nil then dz.emitEvent(myEvent, emitMinutes).afterSec(emitMinutes) end -- for test only
        end
        
        if item.isDevice and item.state == 'On' then
            sendMessage(msgAan, klaarDelay)
                        
        elseif item.isCustomEvent and myDevice.state == 'On' and tonumber(item.data) == klaarDelay then
            sendMessage(msgKlaar, OffDelay)
            
        elseif item.isCustomEvent and myDevice.state == 'On' and tonumber(item.data) == OffDelay then 
            myDevice.switchOff().silent()
            sendMessage(msgWordt)
            
        elseif (item.state == 'Off') then
            sendMessage(msgUit)
            
        end
    end
}

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 16:02
by gschmidt
But also dzNotify has an error...

Code: Select all

dz.notify(dz.moduleLabel, message, dz.PRIORITY_MODERATE, nil, nil, dz.NSS_TELEGRAM)
when I used the os.execute instead, I got a telegram notification

Code: Select all

local token = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
local chatid = xxxxxxxxxxxxxxx
os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..message..'"  "https://api.telegram.org/bot'..token..'/sendMessage"  &')

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 16:09
by gschmidt
Your last script has the following error:

Code: Select all

2020-04-07 16:03:38.701 Error: dzVents: Error: (3.0.1) Test Schakelaar: An error occurred when calling event handler Script #2
2020-04-07 16:03:38.701 Error: dzVents: Error: (3.0.1) Test Schakelaar: /home/pi/domoticz/dzVents/runtime/Domoticz.lua:115: attempt to concatenate a nil value (local 'subject')
I think it has to do with dz.notify, because when I switched back to the os.execute method, the error was gone and I received a telegram notification

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 16:25
by gschmidt
Script is working, I tested it with 1 and 3 minutes, and this works!
Only dz.notify gives an error and no notifications but with the os.execute it all works fine

What happens if I have turned on the Switch by accident and then turn it OFF, is the "sendMessage(msgAan, klaarDelay)" still send to telegram?

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 18:24
by waaren
gschmidt wrote: Tuesday 07 April 2020 16:25 Script is working, I tested it with 1 and 3 minutes, and this works!
Only dz.notify gives an error and no notifications but with the os.execute it all works fine

What happens if I have turned on the Switch by accident and then turn it OFF, is the "sendMessage(msgAan, klaarDelay)" still send to telegram?
the dz.notify error is probably related to the use of dz.moduleLabel
This is available in latest Beta's (>= build 11899) If you are on lower build or Stable , you should change that to 'frietpan' (with the quotes)

If you switch it on and within the 15 minutes to off the message klaarDelay will not be send.

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 22:02
by gschmidt
waaren wrote: Tuesday 07 April 2020 18:24
gschmidt wrote: Tuesday 07 April 2020 16:25 Script is working, I tested it with 1 and 3 minutes, and this works!
Only dz.notify gives an error and no notifications but with the os.execute it all works fine

What happens if I have turned on the Switch by accident and then turn it OFF, is the "sendMessage(msgAan, klaarDelay)" still send to telegram?
the dz.notify error is probably related to the use of dz.moduleLabel
This is available in latest Beta's (>= build 11899) If you are on lower build or Stable , you should change that to 'frietpan' (with the quotes)

If you switch it on and within the 15 minutes to off the message klaarDelay will not be send.
Updated to the latest beta version.
dz.notify is working.....however the message in my telegrambot is “2”, also when switch is turned off

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 22:08
by gschmidt
Changing dz.moduleLabel with ‘frietpan’ is working...
But this way there is no notification sound in telegram...with os.execute there is! Strange :shock:

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 22:30
by waaren
gschmidt wrote: Tuesday 07 April 2020 22:02 Updated to the latest beta version.
dz.notify is working.....however the message in my telegrambot is “2”, also when switch is turned off
:D Funny bug this is !

What happens is that your script was probably named something like 'script #2'
The domoticz notify function use the # character as separator for the various fields (subject, message, priority, etc.)
internally it is converted to "subject#message#prio#etc"

so you messages was translated into
"script #2#frietpan klaar#2#" with 2 being the message..

I will modify the dzVents function to prevent this behavior.

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 22:46
by gschmidt
waaren wrote: Tuesday 07 April 2020 22:30
gschmidt wrote: Tuesday 07 April 2020 22:02 Updated to the latest beta version.
dz.notify is working.....however the message in my telegrambot is “2”, also when switch is turned off
:D Funny bug this is !

What happens is that your script was probably named something like 'script #2'
The domoticz notify function use the # character as separator for the various fields (subject, message, priority, etc.)
internally it is converted to "subject#message#prio#etc"

so you messages was translated into
"script #2#frietpan klaar#2#" with 2 being the message..

I will modify the dzVents function to prevent this behavior.
Yes, it is called script #2....because it is in testing phase :D :D :D
And are you familiar with the “no sound” notification in telegram, when using dz.notify?

Re: How to send notifications with a delay

Posted: Tuesday 07 April 2020 23:15
by waaren
gschmidt wrote: Tuesday 07 April 2020 22:46 And are you familiar with the “no sound” notification in telegram, when using dz.notify?
Some small changes..

I don't know how to force telegram to produce sound. I use other notification subsystems and always without sound.
Do you use the same chatid and token in domoticz settings for telegram as in the os execute ?

Code: Select all

-- Frietpan dzVents script

local myDevicename = 'Test'
local myEvent = 'Friet'

return 
{
    on = 
    { 
        devices = 
        {
            myDevicename,            
        },
        customEvents =
        {
            myEvent,
        },
    },
    
    logging = 
    {
        level = domoticz.LOG_DEBUG,  -- change to LOG_ERROR when ok
        marker = 'Test Schakelaar',
    },

    execute = function(dz, item)
        
        local myDevice = dz.devices(myDevicename)
        
        local msgAan = 'De frietpan is aangezet!'
        local msgKlaar = 'De frietpan is klaar om te frituren!'
        local msgWordt = 'De frietpan staat nog aan en wordt nu uitgezet!'
        local msgUit = 'De frietpan is uitgezet!'
        
        local klaarDelay = 15
        local OffDelay = 60 - klaarDelay
        
        local function sendMessage(message, emitMinutes)
            
            -- Native dzVents / domoticz method
            local subject = (dz.moduleLabel or 'frietpan'):gsub('#','')
            dz.notify(subject, message, dz.PRIORITY_MODERATE, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM)
            
            -- OS approach
            local token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            local chatid = XXXXXXXXXXXXXXX
            -- os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..msgAan..'"  "https://api.telegram.org/bot'..token..'/sendMessage"  &')
            
            dz.log(message,dz.LOG_DEBUG)
            if emitMinutes ~= nil then dz.emitEvent(myEvent, emitMinutes).afterMin(emitMinutes) end
            -- if emitMinutes ~= nil then dz.emitEvent(myEvent, emitMinutes).afterSec(emitMinutes) end -- for test only
        end
        
        if item.isDevice and item.state == 'On' then
            sendMessage(msgAan, klaarDelay)
                        
        elseif item.isCustomEvent and myDevice.state == 'On' and tonumber(item.data) == klaarDelay then
            sendMessage(msgKlaar, OffDelay)
            
        elseif item.isCustomEvent and myDevice.state == 'On' and tonumber(item.data) == OffDelay then 
            myDevice.switchOff().silent()
            sendMessage(msgWordt)
            
        elseif item.isDevice and item.state == 'Off' then
            sendMessage(msgUit)
            
        end
    end
}

Re: How to send notifications with a delay

Posted: Wednesday 08 April 2020 7:54
by gschmidt
I don't know how to force telegram to produce sound. I use other notification subsystems and always without sound.
Do you use the same chatid and token in domoticz settings for telegram as in the os execute ?
I think it is the other way around, by default most communication apps (also Telegram) produce notification sounds. You can modify them to their own needs. But in this case it looks like dz.notify forces Telegram not to produce a notification sound.

Yes I use the same credentials.

Re: How to send notifications with a delay

Posted: Wednesday 08 April 2020 9:18
by waaren
gschmidt wrote: Wednesday 08 April 2020 7:54 I think it is the other way around, by default most communication apps (also Telegram) produce notification sounds.
The absence of sound is because of the notification priority. In the script it is set to moderate.

If you change in the script to a higher level you will get sound.

possible levels are
dz.PRIORITY_EMERGENCY
dz.PRIORITY_HIGH
dz.PRIORITY_NORMAL
dz.PRIORITY_MODERATE -- no sound
dz.PRIORITY_LOW -- no sound

Re: How to send notifications with a delay

Posted: Wednesday 08 April 2020 20:35
by gschmidt
waaren wrote: Wednesday 08 April 2020 9:18
gschmidt wrote: Wednesday 08 April 2020 7:54 I think it is the other way around, by default most communication apps (also Telegram) produce notification sounds.
The absence of sound is because of the notification priority. In the script it is set to moderate.

If you change in the script to a higher level you will get sound.

possible levels are
dz.PRIORITY_EMERGENCY
dz.PRIORITY_HIGH
dz.PRIORITY_NORMAL
dz.PRIORITY_MODERATE -- no sound
dz.PRIORITY_LOW -- no sound
Your last script is working properly now.
I have played with the levels, and this is working.
So now I can set the level of a notification, based on priority.
Thanx!

Re: How to send notifications with a delay

Posted: Wednesday 08 April 2020 21:19
by gschmidt
I have another question, maybe you have an answer.
I also use node-red-contrib-cast nodes to send audio notifications for the "Frietpan" Switch to my google home speakers.
Now this is working when I turn the "Frietpan" switch ON or OFF, but the 2 delay notifications I have a problem with:

the Switch ON/OFF message is published through the MQTT node: domoticz/out into the node-red flow, but is it also possible to publish a "domoticz" log message to the MQTT node? Then I could use the domoticz "frietpan" delay messages to send the audio messages parallel

Re: How to send notifications with a delay

Posted: Wednesday 08 April 2020 22:52
by waaren
gschmidt wrote: Wednesday 08 April 2020 21:19 I have another question, maybe you have an answer.
I also use node-red-contrib-cast nodes to send audio notifications for the "Frietpan" Switch to my google home speakers.
Now this is working when I turn the "Frietpan" switch ON or OFF, but the 2 delay notifications I have a problem with:

the Switch ON/OFF message is published through the MQTT node: domoticz/out into the node-red flow, but is it also possible to publish a "domoticz" log message to the MQTT node? Then I could use the domoticz "frietpan" delay messages to send the audio messages parallel
using os.execute dzVents can send any MQTT message to any topic. Only thing needed is installed mosquitto on the domoticz system.

Just let me know what you want to send to which topic and when and I will have a look.