Jump to content

Pull Variable Value from ISY to Python Program on Pi


LarryCRetired

Recommended Posts

Posted

Wonder if someone can point me in the right direction to pull a variable from the ISY into a python program on the pi.

I am using this request command to retrieve the value of the variable from the ISY using this simple program.  When I print (r) i get a code of 200 which I assume means the request was successful.

What I want to do is populate a variable in python with the value of the state variable ID # 19

is this possible to do.  I messed around a little with Json but was not successful - most likely because I am not sure I know what I am doing.

Any help or direction would be appreciated.  Pretty new at this stuff.  I am running Version 5 release 16 and i have the Polyglot Node Server running on this Pi if that helps.

Thanks in advance for any help.

import requests
from requests.auth import HTTPBasicAuth
    
r=requests.get('http://192.xxx.x.xx/rest/vars/get/2/19', auth=('user', 'PWXXX'))
print (r)

 

Posted
2 hours ago, LarryCRetired said:

Wonder if someone can point me in the right direction to pull a variable from the ISY into a python program on the pi.

I am using this request command to retrieve the value of the variable from the ISY using this simple program.  When I print (r) i get a code of 200 which I assume means the request was successful.

What I want to do is populate a variable in python with the value of the state variable ID # 19

is this possible to do.  I messed around a little with Json but was not successful - most likely because I am not sure I know what I am doing.

Any help or direction would be appreciated.  Pretty new at this stuff.  I am running Version 5 release 16 and i have the Polyglot Node Server running on this Pi if that helps.

Thanks in advance for any help.


import requests
from requests.auth import HTTPBasicAuth
    
r=requests.get('http://192.xxx.x.xx/rest/vars/get/2/19', auth=('user', 'PWXXX'))
print (r)

 

In your python r is the requests object.  You can access the contents via literally the objects contents.

change the print statement like this.

print(r.content)

That will print the content you receive from the requests.get which is contained in the 'r' variable (object).

Posted
3 hours ago, LarryCRetired said:

Wonder if someone can point me in the right direction to pull a variable from the ISY into a python program on the pi.

I am using this request command to retrieve the value of the variable from the ISY using this simple program.  When I print (r) i get a code of 200 which I assume means the request was successful.

What I want to do is populate a variable in python with the value of the state variable ID # 19

is this possible to do.  I messed around a little with Json but was not successful - most likely because I am not sure I know what I am doing.

Any help or direction would be appreciated.  Pretty new at this stuff.  I am running Version 5 release 16 and i have the Polyglot Node Server running on this Pi if that helps.

Thanks in advance for any help.


import requests
from requests.auth import HTTPBasicAuth
    
r=requests.get('http://192.xxx.x.xx/rest/vars/get/2/19', auth=('user', 'PWXXX'))
print (r)

 

I do it the opposite way by ISY pushing the value of a variable to a python3 based server program. It can be done in about 10-15 lines of pythin3 code, maybe less if some of the later modules developed are used.

I control all of my RGBW lights and strips (about 35) using the method. The python3 program resends HTTP commands to the bulbs to control colour, brightness, effects,  and animations.

Posted

I am sorry that I have to come back to the forum for some additional help.  Thanks to simplextech, I am now able to see the stream that my request command is giving me and  I see the state value from the ISY.  I have tried json commands but everything I see is related to printing steams and not assigning a value to a variable.

Unfortunately, after an afternoon of searching on the internet, I am unable to find a way to select the value of the variable the ISY is sending and assign that value to variable in my python3 program. I typically like to see if I can find things out on my own, but after hours of research and trial and error, I am lost and would appreciate any help. ☹️

larryllix - My logic for turning on an appliance is based on results from the python program and sending a rest command to the ISY to start a program with a state variable.  I have successfully done that.  Whereas I see you are using python to do that for you.  Might get there at some point, but for now I have kept all that logic in the ISY.  But I think from your post you are getting a value from the ISY to trigger something happening in your python program.  That is basically where I am stuck.

For simplicity I am testing with this simple program.

import requests
from requests.auth import HTTPBasicAuth
 
Switch = 9

r=requests.get('http://192.xxx.x.xx/rest/vars/get/2/19', auth=('user', 'PWxxxx'))
print(r.content)

Switch =   # This value is what the ISY sends back for the value of the State variable        

Here are the results of my test

pi@raspberrypi:~ $ python3 time.py

b'<?xml version="1.0" encoding="UTF-8"?><var type="2" id="19"><init>0</init><prec>0</prec><val>41</val><ts>20191220 13:00:43</ts></var>'

41 is the value I am trying to extract and assign to the variable in the python3 program

Posted
37 minutes ago, LarryCRetired said:

I am sorry that I have to come back to the forum for some additional help.  Thanks to simplextech, I am now able to see the stream that my request command is giving me and  I see the state value from the ISY.  I have tried json commands but everything I see is related to printing steams and not assigning a value to a variable.

 

Unfortunately, after an afternoon of searching on the internet, I am unable to find a way to select the value of the variable the ISY is sending and assign that value to variable in my python3 program. I typically like to see if I can find things out on my own, but after hours of research and trial and error, I am lost and would appreciate any help. ☹️

 

 

larryllix - My logic for turning on an appliance is based on results from the python program and sending a rest command to the ISY to start a program with a state variable.  I have successfully done that.  Whereas I see you are using python to do that for you.  Might get there at some point, but for now I have kept all that logic in the ISY.  But I think from your post you are getting a value from the ISY to trigger something happening in your python program.  That is basically where I am stuck.

For simplicity I am testing with this simple program.


import requests
from requests.auth import HTTPBasicAuth
 
Switch = 9

r=requests.get('http://192.xxx.x.xx/rest/vars/get/2/19', auth=('user', 'PWxxxx'))
print(r.content)

Switch =   # This value is what the ISY sends back for the value of the State variable        

Here are the results of my test

 

pi@raspberrypi:~ $ python3 time.py

 

b'<?xml version="1.0" encoding="UTF-8"?><var type="2" id="19"><init>0</init><prec>0</prec><val>41</val><ts>20191220 13:00:43</ts></var>'

41 is the value I am trying to extract and assign to the variable in the python3 program

 

Give this a try.  I left comments inline and print statements for debug

#!/usr/bin/env python3

import xml.etree.ElementTree as ET
import requests
from requests.auth import HTTPBasicAuth


def get_request(url, user, password):
    try:
        r = requests.get(url, auth=HTTPBasicAuth(user, password))
        if r.status_code == requests.codes.ok:
            return r.content
        else:
            print("get_request Error:  " + r.content)
            return None

    except requests.exceptions.RequestException as e:
        print("Error: " + str(e))


my_switch = 0

isy_ip = "192.168.1.69"
user = "admin"
password = "admin"

svars_url = "http://" + isy_ip + "/rest/vars/get/2/1"

svars_resp = get_request(svars_url, user, password)
# print(svars_resp)
if svars_resp is not None:
    svars_root = ET.fromstring(svars_resp)
    # print(svars_root.tag, svars_root.attrib)
    for svar in svars_root.iter('var'):
        # print(svar.find('val').text)
        my_switch = svar.find('val').text


print("My Switch Value is: " + my_switch)

Change the (isy_ip, user, password) to match yours.

 

  • Like 1
Posted

FYI -- One thing to keep in mind about using REST to get the value of variables: if you're using the 5.x.x series of ISY firmware and you have a precision value set for your variables to anything other than 0, you'll get a return value that you'll have to additionally parse.  If the value of the variable in the ISY is '45.1' (precision 1), then REST will return a value of '451'.

  • Like 2
Posted

simplextech.  Thank you so much for taking the time to lay out the code for me.  I am not a current day programmer and my programming experience is FORTRAN, Cobol (I am aging myself) and recently VBA for Excel and writing sketches for the Arduino.  Never in a hundred years would I have ever been able to duplicate the code you prepared.  Especially grateful since you were also working on the Ambient Weather Polyglot at about the same time.  With just a minor tweak I was able to get the code working for me. 

Also, thanks Bumbershoot for reminding me about the precision logic.  Something I previously read about but had forgotten.  Also thanks to larryllix for the reference website.  With every challenge I am able to build a little more foundation and these documents help.

Hopefully not offending anyone but hope you all have a Merry Christmas and a Happy New Year.

 

Posted
1 minute ago, LarryCRetired said:

simplextech.  Thank you so much for taking the time to lay out the code for me.  I am not a current day programmer and my programming experience is FORTRAN, Cobol (I am aging myself) and recently VBA for Excel and writing sketches for the Arduino.  Never in a hundred years would I have ever been able to duplicate the code you prepared.  Especially grateful since you were also working on the Ambient Weather Polyglot at about the same time.  With just a minor tweak I was able to get the code working for me. 

Also, thanks Bumbershoot for reminding me about the precision logic.  Something I previously read about but had forgotten.  Also thanks to larryllix for the reference website.  With every challenge I am able to build a little more foundation and these documents help.

Hopefully not offending anyone but hope you all have a Merry Christmas and a Happy New Year.

 

You're very welcome.  I grabbed pieces from the ISY-Inventory polyglot and edited to work outside of the nodeserver.  Here's the link to my public repositories.  Might find some ideas or useful tidbits.

https://github.com/simplextech?tab=repositories

 

Posted

Simplextech.  Thanks for the info.  Once I complete my current projects I will be looking for something else to add.  This will be a good starting point.

Thanks again

Posted
15 minutes ago, LarryCRetired said:

simplextech.  Thank you so much for taking the time to lay out the code for me.  I am not a current day programmer and my programming experience is FORTRAN, Cobol (I am aging myself) and recently VBA for Excel and writing sketches for the Arduino.  Never in a hundred years would I have ever been able to duplicate the code you prepared.  Especially grateful since you were also working on the Ambient Weather Polyglot at about the same time.  With just a minor tweak I was able to get the code working for me. 

Also, thanks Bumbershoot for reminding me about the precision logic.  Something I previously read about but had forgotten.  Also thanks to larryllix for the reference website.  With every challenge I am able to build a little more foundation and these documents help.

Hopefully not offending anyone but hope you all have a Merry Christmas and a Happy New Year.

 

Many of old farts are in a similar boat here.

I did a lot of programming back in the day also, but got out just as methods became a thing. Now with Python some people live on them and causes me real stress now. LOL I use them but don't find them handy enough to warrant some of the wild usages people put them to, due to being somewhat cryptic at times.

All the best and have a great Christmas and happy NY also.

  • 4 weeks later...
Posted

larryllix

I found a couple of courses on Udemy.com to teach me Python.  If you are unaware of this site, it has some great courses for prices as low as $10 or$11 dollars (US), if you watch for the sales.  (I will never be an expert, but at least I am getting some understanding of the formats and structure).  I have subscribed to a number of courses for the Arduino and Pi, since I was starting with no knowledge of either one. 

I found this a great place for learning especially during the winter months in Nebraska.

  • Like 1
Guest
This topic is now closed to further replies.

×
×
  • Create New...