| 
    
     | 
       
        | Author |  |  
        | GCollins Groupie
 
  
  
 Joined: March 27 2011
 Location: United States
 Online Status: Offline
 Posts: 54
 | 
          I've been a user of PH2 for many years, currently running
           | Posted: March 08 2023 at 16:12 | IP Logged |   |  
           | 
 |  v2.1.5c, but I've never tried using the CGI interface
 before now and I'm having some trouble.
 
 What I want to do is send commands to the PH web server
 to execute macros, but I can't seem to get the syntax
 correct, nor can I find complete or up to date
 documentation.
 
 For instance, a post elsewhere on this forum suggested a
 command like the following would work, but I get a syntax
 error:
 
 http://192.168.1.100/ph-cgi/eval?playmacro("GARAGE-ON")
 
 Can someone show me a correctly formatted command for
 this?
 
 
 |  
        | Back to Top |     |  
        |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          Hi,
           | Posted: March 08 2023 at 16:29 | IP Logged |   |  
           | 
 |  
 I use it all the time.
 
 Here's an example of calling a simple macro.  Where :91 is your port number set in PH.
 Yours is probably the default :80
 
 http://192.168.1.100:91/ph-cgi/eval?formula=ph_macro( "NAME_OF_YOUR_PH_MACRO"), auth=(PH_USER_NAME, PH_PASSWORD )
 
 
 Here's an example of calling a macro with parameters:
 
 http://192.168.1.100:91/ph-cgi/eval?formula=ph_macroparm( "NAME_OF_YOUR_PH_MACRO",
 "GOES_INTO_LOCAL1",
 "GOES_INTO_LOCAL2",
 "GOES_INTO_LOCAL3",
 "GOES_INTO_LOCAL4",
 "GOES_INTO_LOCAL5"),
 auth=(PH_USER_NAME, PH_PASSWORD )
 
 Whatever your macro puts into [LOCAL1] will be returned to the caller.
 
 Works nicely!!
 
 
 
 
 
 
 
 
 
 
 
 |  
        | Back to Top |     |  
        |  |  
        | GCollins Groupie
 
  
  
 Joined: March 27 2011
 Location: United States
 Online Status: Offline
 Posts: 54
 | 
          Hi gg102,
           | Posted: March 08 2023 at 21:13 | IP Logged |   |  
           | 
 |  
 Thank you, that works flawlessly.
 
 I had actually tried that syntax before, but was getting
 a return of -1.
 
 After posting my question here, I found that PH
 communications was impaired in general (eg: X10 commands
 not being executed from Device Status).  I restarted PH
 and now the CGI macro call also works.
 
 
 
 
 |  
        | Back to Top |     |  
        |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          I'm glad that I was able to help.
           | Posted: March 08 2023 at 21:25 | IP Logged |   |  
           | 
 |  
 
   |  
        | Back to Top |     |  
        |  |  
        | GCollins Groupie
 
  
  
 Joined: March 27 2011
 Location: United States
 Online Status: Offline
 Posts: 54
 | 
          Well, I'm having trouble again, I think related to
           | Posted: March 09 2023 at 10:58 | IP Logged |   |  
           | 
 |  authentication.
 
 If I have an active session with the PH web server, a
 manually submitted URL containing only the macro name
 works.  If the session is inactive, the web server
 prompts me for credentials and then executes the macro.
 
 If I add the auth= parameter as shown in gg102's example,
 I get "syntax error" as the web response and PH logs a
 formula error.  Indeed, the help file for my system does
 not show "auth" as being a valid parameter.
 
 When I send the CGI command from a Java process running
 on the same Win10 PC, the PH status bar shows Last Event
 is "Web Access" (in red) with details "Connection: Keep-
 AliveUser-Agent: Appache-HttpClient/4.5.13
 (Java/1.8.0_361)Accept-Encoding: gzip,deflate".  The Java
 process indicates successful command submission, but the
 macro doesn't get executed.
 
 What am I doing wrong?
 |  
        | Back to Top |     |  
        |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          Try this from a browser:
           | Posted: March 09 2023 at 11:18 | IP Logged |   |  
           | 
 |  
 http://192.168.1.100/ph-cgi/eval?formula=ph_macro("GARAGE-ON ")
 Should return: 0
 
 
 edit: There wasn't supposed to be a [SPACE] at the end of the macro name.
 
 Edited by gg102 - March 09 2023 at 11:27
 |  
        | Back to Top |     |  
        |  |  
        | GCollins Groupie
 
  
  
 Joined: March 27 2011
 Location: United States
 Online Status: Offline
 Posts: 54
 | 
          Yes it does, works perfectly without the auth parameter,
           | Posted: March 09 2023 at 11:31 | IP Logged |   |  
           | 
 |  whether from a browser on the PH PC or another PC on the
 same subnet.
 
 But, the first time I submit that after booting the PH
 PC, I am prompted for credentials.  On subsequent
 attempts it works without further authentication.
 
 I have a suspicion that the Java process (BWS Systems HA-
 Bridge / Hue Emulator) is unauthenticated.  I was hoping
 to get the correct syntax to include the auth parameter
 in the command string, as in your original example above.
 
 Please note, in PH Web Server Setup I initially had the
 Authentication Method=Cookie with the Persist Days=30.
 Then I tried checking the "Allow Guest on Internal Pages"
 box, and then leaving that box checked while changing
 Method=Basic.  All to no avail.  The Trusted IP list
 appears correct: 127.0.0.1; 192.168.0.*
 
 |  
        | Back to Top |     |  
        |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          
           | Posted: March 09 2023 at 12:12 | IP Logged |   |  
           | 
 |  I have the following settings:
 
 "Allow Guest on Internal Pages" [UNCHECKED]
 "Enable Smart NextURL" [UNCHECKED]
 
 Authentication Method=Cookie
 Persist Days=30.
 Enable WAP [CHECKED]
 The Trusted IP list: 127.0.0.1; 192.168.x.*  (Seems your network is 0, so x=0)
 
 Here's my Python code from my garage_pi:
 
 PH_USER = admin       // Not really.
 PH_PASSWORD = 12345   //Not really.
 ...
 ...
 header_string =  "http://192.168.x.y:91/ph-cgi/eval?formula=ph_macroparmret("
 data_string   = '"GARAGE_DATA_UPDATE"'      + "," + \
 '"' + passed_string1        + '"' + "," + \
 '"' + passed_server_function+ '"' + "," + \
 '"' + passed_string3        + '"' + "," + \
 '"' + passed_string4        + '"' + "," + \
 '"' + passed_string5        + '"' + ")"
 full_string = header_string + data_string
 write_local_log_file( "Sent to Server: " + data_string + "\r\n")
 ...
 ...
 try:
 response = -1
 response = requests.get( full_string, auth=(PH_USER, PH_PASSWORD) )
 write_local_log_fi le("Server Response: " + response.text + "\r\n")
 except:
 write_local_log_file("Error. Line: " + get_program_line_number() + \
 " Can't write to server data file." + \
 " Response is: "+ str(response) + "\r\n")
 return(response)
 
 
 The beauty of this method is that you can test these commands from a browser to find out which side the problem is on.
 
 You seem to understand your system very well.  I can't speak to that, I can only show you what I use.
 Perhaps the libraries for the raspberry pi "do stuff" underneath that I don't know.
 Maybe the libraries use auth=() if they get an error, then retry.
 I only know this works for the raspberry pi.
 
 for my Shelly devices, I only need:
 http://192.168.x.y:91/ph-cgi/eval?formula=ph_macro(%22MOTION _SENSOR11%22)
 No auth=()
 
 
 
 Maybe Dave can jump in here and offer better insight.
 
 
 
 |  
        | Back to Top |     |  
        |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          
           | Posted: March 09 2023 at 12:42 | IP Logged |   |  
           | 
 |  There's one more thing I should advise.
 (You probably already know this)
 
 If you're using ph_macroparm()  you MUST have all the parms: 1,2,3,4,5.  You can't skip any.
 
 so:
 ph_marcoparm("MACRO_NAME","1","2","3","4","5") works
 ph_marcoparm("MACRO_NAME","1") fails
 To do that you must use:
 ph_marcoparm("MACRO_NAME","1","","","","") works
 
 
 if you're using ph_macro() you don't use any parms.
 
 
 
 |  
        | Back to Top |     |  
        |  |  
        | GCollins Groupie
 
  
  
 Joined: March 27 2011
 Location: United States
 Online Status: Offline
 Posts: 54
 | 
          gg102, thanks for your detailed responses.
           | Posted: March 09 2023 at 13:52 | IP Logged |   |  
           | 
 |  
 Interesting that your script tests for a -1 response as
 an authentication issue.  I didn't know what -1 meant,
 but I have seen that response in my testing, so it
 supports my suspicion.
 
 I thought about running the HA-Bridge on a Pi, but am
 trying to reduce the size of my Pi farm.  Since my
 PowerHome PC is otherwise dedicated to that function, I
 thought I'd run the bridge alongside.
 
 To be clear on my setup, the PH PC currently lives at
 192.168.10.60 in subnet 192.168.10.0/24 with these
 listeners:
 PH web server - port 81
 HA-Bridge web server - port 80 (default to support
 discovery)
 
 And at this point I am just using the single parm
 ph_macro call.  Trying to keep things very simple,
 without a lot of logic on the HA-Bridge side of things.
 
 |  
        | Back to Top |     |  
        |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          
           | Posted: March 09 2023 at 14:52 | IP Logged |   |  
           | 
 |  Let me explain the -1.  It's a "FEATURE" of Python.
 
 
 in this code:
 try:
 response = -1
 response = requests.get(.....)
 more code....
 except:
 more code....
 return(response)
 
 The -1 is to set into the "response" variable before I call the requests.get() function to initialize it.
 The reason is because if requests.get() function times out or gets an error, then the 'response=' never gets executed, and
 "response" unless it's a global variable, remains uninitialized.  This is the way the Python "TRY/EXCEPT" construct works.
 
 If you're not familiar with Python, the "TRY/EXCEPT" construct is like a "WHILE-ELSE-LOOP" construct.  If something inside the
 "TRY" fails, it moves to execute the "EXCEPT" section.  So, if the requests.get() function fails, the code in the "except"
 section executes and the "response=" part never sets anything into "response".  Response would remain uninitialized.  If I
 didn't set it, then in case of an error, the return(response) would have a problem returning a variable that never was
 initialized.  (I learned that one the hard way.)
 
 
 As to my using port 91:
 My ISP blocks port 80 so that we, paying customers, can't set up a WEB server at home. How nice of them.  So, I use a
 different port so I can access 'stuff' from outside my internal net.
 
 As to using Pi's.  I love these little things.  They are great!  Since I can communicate from PH to the Pi's using this
 method, it's just wonderful to use them as satellite intelligent devices/sensors.  They can pass data into PH and PH can pass
 stuff back to them.  I have tried to use sockets, but I could never get that going.  This method is fast and reliable. I use
 RDP to access the Pi's so mine are all "headless".
 
 I also recently moved away from x10 to Shelly devices.  I have them set to use this method to command and alert my PH or
 devices.
 
 Hope this all helps.
 
 
 
 
 |  
        | Back to Top |     |  
        |  |  
        | GCollins Groupie
 
  
  
 Joined: March 27 2011
 Location: United States
 Online Status: Offline
 Posts: 54
 | 
          gg102,
           | Posted: March 09 2023 at 21:28 | IP Logged |   |  
           | 
 |  
 Absolutely, all that helps!
 
 I'm smiling. I was a coder 45 years ago, and initializing
 variables was one of my earliest lessons.  I've never
 done Python, but should!
 
 This thread has helped me along, and to better understand
 what I'm seeing in another post that tipped me off to
 using HA-Bridge alongside PH2.  Like in this one, my
 ultimate goal is Alexa voice control of PH macros:
 http://power-home.com/forum/forum_posts.asp?TID=10699
 
 I haven't even tried the Alexa part yet, focusing first
 on manual CGI and now manual testing from HA-Bridge.
 
 Regarding devices, my use of X10 goes back as far as my
 coding days (and was frustrating). Then, about 20 years
 ago, I built a house using superset Lightolier Compose
 devices (predecessor to Philips Hue), which have proven
 very reliable, capable on their own including scenes, and
 greatly enhanced by PH2 schedules, triggers, and macros.
 
 
 |  
        | Back to Top |     |  
        |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          
           | Posted: March 09 2023 at 22:17 | IP Logged |   |  
           | 
 |  Seems you and I are of the same vintage.
 
 As to Alexa, I would never have that or any other spy device in my home.  That's a whole different conversation.
 
 I have tried off-line voice recognition on the Pi's and it's just not ready for prime time yet. I experimented with voice recognition back on the
 S-100 buss devices.  Then I moved to RS-232 Threshold Technologies boxes and more from there.  Yeah, I've been trying voice recognition since
 before disco.
 
 Over the years, I have discovered that just having the house do its thing when its supposed to works just fine for me.  PH "knows" if I'm home
 and does things appropriately, and if I'm not home, PH knows how to operate things until I become home.  PH sends me text messages if something
 happens or if I need to handle something.  I use motion sensors to turn lights on/off if there's motion and the lumen level is at some level.
 Depending on the lumen level changes the amount of brightness the light gets set to.  The same motion sensors become alarm sensors if I'm not
 home.  Programming PH allows me to do things like that.  Using my weather sensors determine whether or not to activate the sprinklers and if so,
 it monitors how much water gets used.  If there's a sprinkler leak (used more water than it should), it knows and alerts me.  If necessary, it
 will stop using the sprinklers until I override it or fix the leak.
 
 Accessing PH from my phone (WEB service) either locally or remotely is sufficient for me to immediately command any device or alter a condition.
 
 I have a very advanced Remote Control Center (RCC) in PH and I use that to control things, set variables/conditions that the house then uses,
 graphs HVAC cycles, Refrigerator power use, temperatures and so much more.
 
 
 
 
   |  
        | Back to Top |     |  
        |  |  
        | GCollins Groupie
 
  
  
 Joined: March 27 2011
 Location: United States
 Online Status: Offline
 Posts: 54
 | 
          Roger voice rec.  Alexa has it nailed, especially with the
           | Posted: March 09 2023 at 23:14 | IP Logged |   |  
           | 
 |  Hue integration.  I acknowledge the intrusion and give it
 an earful of training every chance I get.
 
 Agree on local house logic.  Once it's dialed in, hard to
 beat.
 
 Edited by GCollins - March 10 2023 at 09:57
 
 __________________
 PH2 Integr: JV Digital, Compose, Harmony, Insteon, Leviton, X10-Pro, DSC, Radio Shack, HA-Bridge/Alexa, UniFi Protect
 Alexa Integr: Ecobee, Philips Hue, UniFi Protect, Sengled Zigbee, Kasa WiFi
 
 
 |  
        | Back to Top |     |  
        |  |  
        | GCollins Groupie
 
  
  
 Joined: March 27 2011
 Location: United States
 Online Status: Offline
 Posts: 54
 | 
          Just closing the loop here, my problem is solved.
           | Posted: March 10 2023 at 19:44 | IP Logged |   |  
           | 
 |  
 Log levels and syntax on the Trusted IP's listed were both out of whack.
 
 Macro calls from HA-Bridge are now sailing through. Details posted over here:
 http://power-home.com/forum/forum_posts.asp?TID=10699&PN=1&T PN=1
 
 Thanks again to gg102 for getting me started.
 
 __________________
 PH2 Integr: JV Digital, Compose, Harmony, Insteon, Leviton, X10-Pro, DSC, Radio Shack, HA-Bridge/Alexa, UniFi Protect
 Alexa Integr: Ecobee, Philips Hue, UniFi Protect, Sengled Zigbee, Kasa WiFi
 
 
 |  
        | Back to Top |     |  
        |  |  
        | dhoward Admin Group
 
  
  
 Joined: June 29 2001
 Location: United States
 Online Status: Offline
 Posts: 4447
 | 
          GCollins,
           | Posted: March 10 2023 at 21:42 | IP Logged |   |  
           | 
 |  
 Sorry to take so long responding but glad that Walt (gg102) was able to get you going and that ultimately it ended up not being too difficult.
 I know Walt is making extensive use of the PH functionality (probably more than even me
  ) so has a ton of knowledge. 
 Dave.
 
 |  
        | Back to Top |       |  
        |  |  
        | GCollins Groupie
 
  
  
 Joined: March 27 2011
 Location: United States
 Online Status: Offline
 Posts: 54
 | 
          Dave,  No worries, and thank you.
           | Posted: March 11 2023 at 07:32 | IP Logged |   |  
           | 
 |  
 Just one more question regarding this example Walt gave:
 
 http://192.168.1.100:91/ph-cgi/eval?formula=ph_macro( "NAME_OF_YOUR_PH_MACRO"), auth=(PH_USER_NAME, PH_PASSWORD )
 
 Should the "auth=" parameter work in v2.1.5c?  I don't need it now since I've whitelisted the source IP, but just curious.
 
 __________________
 PH2 Integr: JV Digital, Compose, Harmony, Insteon, Leviton, X10-Pro, DSC, Radio Shack, HA-Bridge/Alexa, UniFi Protect
 Alexa Integr: Ecobee, Philips Hue, UniFi Protect, Sengled Zigbee, Kasa WiFi
 
 
 |  
        | Back to Top |     |  
        |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          
           | Posted: March 11 2023 at 08:31 | IP Logged |   |  
           | 
 |  Sorry guys, my bad.....
 
 Checking the requests,get() function I discovered that the auth=() is one of many optional parms of the Python function and not PH.
 
 It seems that the Python library DOES handle the auth if the site requests it.  Thus I don't have the "login" problem that you're seeing on
 the first packet because the Python library handled it.
 
 I must have had the same login issue you're seeing and handled it with this optional auth parm.
 
 Sorry,
 
 
 
 
 |  
        | Back to Top |     |  
        |  |  
        | GCollins Groupie
 
  
  
 Joined: March 27 2011
 Location: United States
 Online Status: Offline
 Posts: 54
 | 
          Walt, that's cool.  Thanks for the explanation.  Good
           | Posted: March 11 2023 at 09:51 | IP Logged |   |  
           | 
 |  learning experience.
 
 __________________
 PH2 Integr: JV Digital, Compose, Harmony, Insteon, Leviton, X10-Pro, DSC, Radio Shack, HA-Bridge/Alexa, UniFi Protect
 Alexa Integr: Ecobee, Philips Hue, UniFi Protect, Sengled Zigbee, Kasa WiFi
 
 
 |  
        | Back to Top |     |  
        |  |  |  |