More python parsing assistance

Do you have questions about writing plugins or scripts in Python? Meet the coders here.

More python parsing assistance

Postby dlondon » Sat Jan 07, 2017 1:43 pm

Hi guys,

Pardon my ignorance as I am relatively new to python; I have managed to connect EG to MySQL, so far mainly for displaying info on the web interface from the media center as well as storing program/device statuses, however I have what I hope is a relatively simple question for you smart people.

Similar but slightly different to the one in this thread:
http://www.eventghost.net/forum/viewtopic.php?f=10&t=9118

Using the Philips Hue plugin I'm trying to retrieve the current temp from the Hue motion sensor so that I can display that on the web interface as well as to control an electric heater in the bedroom due to a temperature differential with the living room (where the thermostat is located). There is an action within the plugin to get this however it is returned with a lot of other information that I am not interested in, like this:-

Code: Select all
{u'name': u'Hue temperature sensor 1', u'uniqueid': u'00:17:88:01:02:02:b8:70-02-0402', u'config': {u'on': True, u'battery': 100, u'ledindication': False, u'alert': u'none', u'reachable': True, u'usertest': False, u'pending': []}, u'swversion': u'6.1.0.18912', u'state': {u'temperature': 2110, u'lastupdated': u'2017-01-07T12:26:23'}, u'manufacturername': u'Philips', u'type': u'ZLLTemperature', u'modelid': u'SML001'}


I'm only interested in the number after '{u'temperature':' in this case '2110'

I've tried a few things in order to get only this number but am met by various errors. When I use the following script I get the output from above printed in the log:
Code: Select all
eg.globals.temp = eg.plugins.PhilipsHue.getSensorStatus(u'03 - Hue temperature sensor 1')
print eg.globals.temp


When I try to cut only certain characters from the output as an example:
Code: Select all
eg.globals.temp = eg.plugins.PhilipsHue.getSensorStatus(u'03 - Hue temperature sensor 1')
print eg.globals.temp[110:200]

results in 'TypeError: unhashable type'

when I try to use the .split() function as mentioned here:
http://gis.stackexchange.com/questions/58578/use-python-to-parse-string

using this code:
Code: Select all
eg.globals.temp = eg.plugins.PhilipsHue.getSensorStatus(u'03 - Hue temperature sensor 1').split(':')
x = eg.globals.temp [0]
print x

results in AttributeError: 'dict' object has no attribute 'split'

I realise that I have probably embarrased myself with myself with my poor coding but I did say that I am a newbie, there seems to be a bewildering number of ways to achieve what I'm trying to do, I really want to do this in the simplest way possible.

Can anyone help?
dlondon
 
Posts: 2
Joined: Sat Jan 07, 2017 11:45 am

Re: More python parsing assistance

Postby pearbear » Sat Jan 07, 2017 11:38 pm

Try:
Code: Select all
eg.globals.temp['state']['temperature']

Information on Python dictionaries:
https://docs.python.org/2.7/tutorial/datastructures.html#dictionaries
pearbear
Experienced User
 
Posts: 150
Joined: Mon Apr 02, 2012 10:28 pm

Re: More python parsing assistance

Postby dlondon » Sun Jan 08, 2017 2:36 pm

That works perfectly, I will have a read of the link and educate myself.

Thanks for your assistance.
dlondon
 
Posts: 2
Joined: Sat Jan 07, 2017 11:45 am


Return to Coding Corner

Who is online

Users browsing this forum: No registered users and 3 guests