Announcement

Collapse
No announcement yet.

Problem updating HS device from python script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Problem updating HS device from python script

    Hi,

    Sorry if this is not the correct forum to post, not sure where to put this.

    I have a script that pulls data from my solarpanel production and has up until a week ago working great.
    Do to some issues with upgrading to python, the script started to fail.
    Now I have downgraded and the script is again able to fetch data from my solarpanel inverter.

    The problem is that the script won't update the HS3 devices any more.
    Is there anyone that could take a look at this code and see if there is any obvious problems?
    I'm out of ideas.
    I get no error when I run the script.

    PHP Code:
    #!/usr/bin/python
    import requests
    import urllib
    urllib2

    #config
    username             'XXX'    #your username
    password             'XXX'                #your password
    domain                 'm.ginlong.com'             #domain
    plantId             'XXX'                     #plant id
    lan                 '2'                         #lanuage (2 = English)

    #Homeseer settings
    hs_host          'localhost'
    hs_port          '80'
    hs_url           'JSON'
    hs_ActualPower   '183' #refid of new device
    hs_etoday       '190' #refid of new device
    hs_etotal       '192' 

    # Create session for requests
    session requests.session()

    #building url
    url 'http://'+domain+'/cpro/login/validateLogin.json'
    params = {
        
    "userName"username,
        
    "password"password,
        
    "lan"lan,        
        
    "domain"domain,
        
    "userType""C"
    }

    #login call
    resultData session.post(urlparams=params)
    resultJson resultData.json()
    if 
    resultJson['result'].get('isAccept') == 1:
        print(
    "Login Succesfull!")
    else:
        print(
    "Login Failed!!")
        Exit()

    # Get plant details
    url 'http://'+domain+'/cpro/epc/plantDetail/showPlantDetailAjax.json'
    params = {
        
    'plantId'int(plantId)
    }

    cookies = {'language'lan}
    resultData session.get(urlparams=paramscookies=cookies)
    resultJson resultData.json()

    TodayIncome resultJson['result']['plantAllWapper']['plantData'].get('incomeTotal')
    ActualPower resultJson['result']['plantAllWapper']['plantData'].get('power')
    etoday resultJson['result']['plantAllWapper']['plantData'].get('energyToday')
    etotal resultJson['result']['plantAllWapper']['plantData'].get('energyTotal')

    multiply='1000.0'
    etotal1000 float(etotal) * float(multiply)
    etotalstr=str(etotal1000)

    #logging values
    print('TodayIncome: ' str(TodayIncome))
    print(
    'ActualPower: ' str(ActualPower))
    print(
    'etoday: ' str(etoday))
    print(
    'etotal: ' str(etotal))
    print(
    'etotal 1000: ' str(etotalstr))

    #uploading values to Homeseer
    url = ("http://" hs_host ":" hs_port "/" hs_url"?request=controldevicebyvalue&ref=" hs_ActualPower"&value=" str(ActualPower))
    urllib.urlopen(url)

    url = ("http://" hs_host ":" hs_port "/" hs_url"?request=controldevicebyvalue&ref=" hs_etoday"&value=" str(etoday))
    urllib.urlopen(url)

    url = ("http://" hs_host ":" hs_port "/" hs_url"?request=controldevicebyvalue&ref=" hs_etotal"&value=" str(etotal))
    urllib.urlopen(url

    #2
    It would be helpful if you posted the errors from running the script.

    Comment


      #3
      If I try using this: http://localhost:80/JSON?request=setdevicestatus&ref=183&value=65&string= in the browser, then it works.
      But I'm having trouble to make the script do the same.
      url = ("http://" + hs_host + ":" + hs_port + "/" + hs_url+ "?request=setdevicestatus&ref=" + hs_ActualPower+ "&value=" + str(ActualPower) + "&string=")

      What si wrong here?

      Comment


        #4
        Originally posted by simplextech View Post
        It would be helpful if you posted the errors from running the script.
        That's the "problem" I get no errors..
        Click image for larger version

Name:	Capture.JPG
Views:	35
Size:	16.5 KB
ID:	1372392
        Attached Files

        Comment


          #5
          This is what I get in the browser:
          Attached Files

          Comment


            #6
            What is the error?

            Comment


              #7
              Then print out your last statements to see what is being sent. Add some debugging to your code to see what it's doing.

              Comment


                #8
                Originally posted by simplextech View Post
                Then print out your last statements to see what is being sent. Add some debugging to your code to see what it's doing.
                How do I do that?
                I'm on really deep waters here. I have very little/zero experience with scripting.
                Is there a way to run the scripts other than CMD to get more information?

                Comment


                  #9
                  I suggest printing what is in the url variable for each of the 3 homeseer calls. Then paste those in your browser to test if the url is correctly formated.

                  PHP Code:
                  url = ("http://" hs_host ":" hs_port "/" hs_url"?request=controldevicebyvalue&ref=" hs_ActualPower"&value=" str(ActualPower))

                  url = ("http://" hs_host ":" hs_port "/" hs_url"?request=controldevicebyvalue&ref=" hs_etoday"&value=" str(etoday))

                  url = ("http://" hs_host ":" hs_port "/" hs_url"?request=controldevicebyvalue&ref=" hs_etotal"&value=" str(etotal)) 

                  Comment


                    #10
                    Also worth looking to see if what you're putting into the URL is actually what you think you're putting into the URL. Add something real simple like:

                    Code:
                    print url
                    urllib.urlopen(url)
                    So that it prints out what it's about to send to HS. Then you can see if you're actually sending what you think you're sending.....

                    Comment


                      #11
                      I would also say, sending 3 Json commands virtually simultaneously is asking for trouble so I would place a delay between each urllib.urlopen(url)
                      Jon

                      Comment


                        #12
                        That piece of code did the trick!
                        Was looking for something like that.
                        Seams that the script was sending everything correct, but the problem is with comma and dot.
                        The script sends dot, but HS3 needs comma to change the device value.
                        So I'm one step closer.

                        Comment


                          #13
                          Originally posted by jon00 View Post
                          I would also say, sending 3 Json commands virtually simultaneously is asking for trouble so I would place a delay between each urllib.urlopen(url)
                          How do I do that?

                          Comment


                            #14
                            Add:

                            import time

                            to the top of the script and then add:

                            time.sleep(1)

                            between the JSON calls.
                            Jon

                            Comment


                              #15
                              Changing my region settings did the trick. And also added a delay just to be on the safe side.

                              Thanks everybody. I slowly learn something new every day.

                              Comment

                              Working...
                              X