Page 1 of 1

DzVents and PushSafer - need help getting it to work

Posted: Tuesday 11 December 2018 11:54
by erem
Friends,

I am trying to get PushSafer to send a push message to my iPhone.
The http syntax i use is OK, as i can end that message from a browser from the RPi and the notification shows up at my phone.
trying to send that message from DzVents does not yied an error from DzVents, but the message never shows up at my phone

DzVents code i "wrote/cobbled together"

Code: Select all


-- Test event to see if pushsafer can be done from DzEvents
return {
   on = {
      timer = { 'every 5 minutes' },
      httpResponses = { 'MsgSent' }
   },
   execute = function(domoticz, device)
      if (device.isTimer) then
         domoticz.openURL(
            {
            url = 'https://www.pushsafer.com/api?k=####################&d=#####&i=173&v=3&pr=0&a=1&t=Message from DzEvents&m=Zet je fiets aan de lader',
            method = 'POST',
            callback = 'MsgSent'
            }
         )
      end
      if (device.isHTTPResponse) then
         if (device.ok) then
            domoticz.log('HTTP POST message TBC was sent', domoticz.LOG_INFO)
         else
            domoticz.log('device.ok was not on', domoticz.LOG_INFO)
         end
      end  
   end
}

The Domoticz log with DzVents at most verbose level yields

Code: Select all

2018-12-11 11:30:00.467 Status: dzVents: Info: ------ Start internal script: SndNotTest:, trigger: every 5 minutes
2018-12-11 11:30:00.469 Status: dzVents: Debug: OpenURL: url = https://www.pushsafer.com/api?k=####################&d=####&i=173&v=3&pr=0&a=1&t=Message from DzEvents&m=Zet je fiets aan de lader
2018-12-11 11:30:00.469 Status: dzVents: Debug: OpenURL: method = POST
2018-12-11 11:30:00.469 Status: dzVents: Debug: OpenURL: post data =
2018-12-11 11:30:00.469 Status: dzVents: Debug: OpenURL: headers = nil
2018-12-11 11:30:00.469 Status: dzVents: Debug: OpenURL: callback = MsgSent
2018-12-11 11:30:00.469 Status: dzVents: Info: ------ Finished SndNotTest
2018-12-11 11:30:00.469 Status: dzVents: Debug: Commands sent to Domoticz:
2018-12-11 11:30:00.470 Status: dzVents: Debug: - OpenURL = {["method"]="POST", ["URL"]="https://www.pushsafer.com/api?k=####################&d=####&i=173&v=3&pr=0&a=1&t=Message from DzEvents&m=Zet je fiets aan de lader", ["_trigger"]="MsgSent", ["postdata"]=""}
2018-12-11 11:30:00.470 Status: dzVents: Debug: =====================================================

I do not see any obvious errors, but since there is no notification received, something must be wrong.
I have tried sending the same http string from the RPi browser, and that yields a notification on my phone, so technically it works from the RPi.
I suspect the issue to be in my DzVents code, but as a LUA newbie i i am not to familiar with the syntax.

Did anybody try this already and got it to work? I am grateful for any advice that may help me get this working.

thanks for reading,

Rob

Re: DzVents and PushSafer - need help getting it to work  [Solved]

Posted: Tuesday 11 December 2018 13:42
by elmortero
I think you should use urlencode (info on the dzvents wiki page) to avoid the spaces in the URL.
You can easily check if that is the cause by replacing the spaces in your URL with %20

Re: DzVents and PushSafer - need help getting it to work

Posted: Tuesday 11 December 2018 16:40
by erem
@elmotero, you hit the nail on the head. domoticz.urlEncode was the solution

But, urlencode also encoded the http:// to http%3A%2F%2F, and that did not work for me

so i ended up doing: PS_URL = PS_httpHeader .. domoticz.urlEncode(PS_Parms)

Re: DzVents and PushSafer - need help getting it to work

Posted: Thursday 24 January 2019 21:58
by Robby1969
Hello Rob,

Is it possible to place the final code in this subject. You wrote "so i ended up doing: PS_URL = PS_httpHeader .. domoticz.urlEncode(PS_Parms)"
But where in the code do I have to place this?

Regards Robert

Re: DzVents and PushSafer - need help getting it to work

Posted: Friday 25 January 2019 1:31
by waaren
Robby1969 wrote: Thursday 24 January 2019 21:58 Is it possible to place the final code in this subject. You wrote "so i ended up doing: PS_URL = PS_httpHeader .. domoticz.urlEncode(PS_Parms)"
But where in the code do I have to place this?
There is no real need to use the http code for a pushsafer notification from dzVents. Pushsafer can be used with the notify command.

Code: Select all

domoticz.notify("Subject", "Message", domoticz.PRIORITY_NORMAL,,,domoticz.NSS_PUSHSAFER) 
If for one reason or another you still need to use the operURL approach the code would be something like

Code: Select all

-- Test event to see if pushsafer can be done from DzEvents
return {
   on = {
      timer = { 'every 5 minutes' },
      httpResponses = { 'MsgSent' }
   },
   execute = function(domoticz, device)
      if (device.isTimer) then
         PS_httpHeader  = 'https://www.pushsafer.com/api?k='
         PS_Parms       = '####################&d=#####&i=173&v=3&pr=0&a=1&t=Message from DzEvents&m=Zet je fiets aan de lader'
         PS_URL         = PS_httpHeader .. domoticz.urlEncode(PS_Parms)
         domoticz.openURL(
            {
            url = PS_URL,
            method = 'POST',
            callback = 'MsgSent'
            }
         )
      end
      if (device.isHTTPResponse) then
         if (device.ok) then
            domoticz.log('HTTP POST message TBC was sent', domoticz.LOG_INFO)
         else
            domoticz.log('device.ok was not on', domoticz.LOG_INFO)
         end
      end  
   end
}

Re: DzVents and PushSafer - need help getting it to work

Posted: Monday 28 January 2019 14:07
by Robby1969
In the log I get the message : HTTP POST message TBC was sent, but I do not get the message on my phone.
If I paste the URL in the browser I do get the message on my phone.
With domoticz.notify I also get a message on my phone.
I have Domoticz version 4.9700

What could be the problem?

Re: DzVents and PushSafer - need help getting it to work

Posted: Monday 28 January 2019 15:15
by waaren
Robby1969 wrote: Monday 28 January 2019 14:07 In the log I get the message : HTTP POST message TBC was sent, but I do not get the message on my phone.
If I paste the URL in the browser I do get the message on my phone.
With domoticz.notify I also get a message on my phone.
I have Domoticz version 4.9700

What could be the problem?
A problem could be that the api key and device are also urlEncoded.
Can you try this ? It works on my system.
[EDIT] removed logWrite and changed domoticz.urlEncode domoticz.utils.urleEncode (thanks Robby1969)

Code: Select all

-- Test event to see if pushsafer can be done from DzEvents
return {
   on = {
      timer = { 'every 5 minutes' },
      httpResponses = { 'MsgSent' }
   },
   execute = function(domoticz, device)
      if (device.isTimer) then
         PS_httpHeader  = 'https://www.pushsafer.com/api?k='
         local subject = 'Message from DzEvents'
               subject = domoticz.utils.urlEncode(subject)
         local message = 'Zet je fiets aan de lader nu'
               message = domoticz.utils.urlEncode(message)
         PS_Parms       = 'XXXXXXXX&d=XXXX&i=173&v=3&pr=0&a=1&t'.. subject .. '&m=' .. message
         PS_URL         = PS_httpHeader .. PS_Parms
         domoticz.openURL(
            {
            url = PS_URL,
            method = 'POST',
            callback = 'MsgSentX'
            }
         )
          
      end
      if (device.isHTTPResponse) then
         if (device.ok) then
            domoticz.log('HTTP POST message TBC was sent', domoticz.LOG_INFO)
         else
            domoticz.log('device.ok was not on', domoticz.LOG_INFO)
         end
      end  
   end
}

Re: DzVents and PushSafer - need help getting it to work

Posted: Monday 28 January 2019 22:31
by Robby1969
Thanks! now it works. A few minor changes where necessary:
Log: Info: domoticz.urlEncode deprecated. Please use domoticz.utils.urlEncode.
--domoticz.urlEncode needed to be domoticz.utils.urlEncode
and
Log: Error (2.4.6): ...oticz/scripts/dzVents/generated_scripts/testmessage3.lua:21: attempt to call global 'logWrite' (a nil value)
--logWrite(PS_URL) needed to be domoticz.log(PS_URL)

So the final code is:

Code: Select all

return {
   on = {
      timer = { 'every 2 minutes' },
      httpResponses = { 'MsgSent' }
   },
   execute = function(domoticz, device)
      if (device.isTimer) then
         PS_httpHeader  = 'https://www.pushsafer.com/api?k='
         local subject = 'Message from DzEvents'
               subject = domoticz.utils.urlEncode(subject)
         local message = 'Zet je motorfiets aan de lader nu'
               message = domoticz.utils.urlEncode(message)
         PS_Parms       = XXXXXXXXXXXXXXXX'&d=XXXXXXX&i=50&s=20&v=3&pr=0&a=1&t'.. subject .. '&m=' .. message
         PS_URL         = PS_httpHeader .. PS_Parms
         --logWrite(PS_URL)
         domoticz.log(PS_URL)
         
         domoticz.openURL(
            {
            url = PS_URL,
            method = 'POST',
            callback = 'MsgSentX'
            }
         )
          
      end
      if (device.isHTTPResponse) then
         if (device.ok) then
            domoticz.log('HTTP POST message TBC was sent', domoticz.LOG_INFO)
         else
            domoticz.log('device.ok was not on', domoticz.LOG_INFO)
         end
      end  
   end
}