Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid json return when using get_device_reading #12

Closed
d1nd141 opened this issue Dec 25, 2018 · 9 comments
Closed

Invalid json return when using get_device_reading #12

d1nd141 opened this issue Dec 25, 2018 · 9 comments

Comments

@d1nd141
Copy link

d1nd141 commented Dec 25, 2018

Hi,
Using the following code:
date_1 = str(fh.get_device_reading("myWeather", "fc1_date"))
print ("Date:"+date_1)

I always get a json return with "u", and the datetime not in '':
Date:{u'Value': u'25 Dec 2018', u'Time': datetime.datetime(2018, 12, 25, 16, 46, 8)}

I used str() to convert the dict return as a string.
Thanks

@domschl
Copy link
Owner

domschl commented Dec 26, 2018

Assuming that you are using Python 2.x and talking to the 'Weather' module.

I am not clear what you are intending.

The returned dictionary in this case has two entries: 'Value' and 'Time'. Below is an example how to work with each of those entries:

import logging
import fhem

logging.basicConfig()
fh=fhem.Fhem('your-server')

ans_dict=fh.get_device_reading("myWeather", "fc1_date")
# ans_dict is now {u'Value': u'26 Dec 2018', u'Time': datetime.datetime(2018, 12, 26, 7, 42, 3)}
myDate=ans_dict['Value']
print("Date:"+myDate)
# Output: print("Date:"+myDate)
myTime=ans_dict['Time']
# myTime is now a datetime.datetime object. Use strftime to format a string
print(myTime.strftime("%Y-%m-%d %H:%M:%S"))
# Output: 2018-12-26 07:42:03
# or, using a different formatting string:
print(myTime.strftime("Time:  %H:%M:%S"))
# Output: Time: 07:42:03

See here for more info on strftime's formatting options.

@d1nd141
Copy link
Author

d1nd141 commented Dec 26, 2018

Hallo,
thanks for your fast help!
I changed the code, and now it works fine. My fault, sorry!

Nevertheless i have another problem.
I'm fetching about 20 Values from fhem.
The first one or two are working fine, then i get:

DEBUG:Fhem:Sending: jsonlist2 NAME~myWeather
DEBUG:Fhem:Connected, sending...
INFO:Fhem:Sent msg, len=25
DEBUG:Fhem:Exception in non-blocking. Error: [Errno 11] Resource temporarily unavailable
INFO:Fhem:JSON answer received.
DEBUG:Fhem:Sending: jsonlist2 NAME~myWeather
DEBUG:Fhem:Connected, sending...
INFO:Fhem:Sent msg, len=25
DEBUG:Fhem:Exception in non-blocking. Error: [Errno 11] Resource temporarily unavailable
INFO:Fhem:JSON answer received.
DEBUG:Fhem:Sending: jsonlist2 NAME~myWeather
DEBUG:Fhem:Connected, sending...
INFO:Fhem:Sent msg, len=25
DEBUG:Fhem:Exception in non-blocking. Error: [Errno 11] Resource temporarily unavailable
INFO:Fhem:JSON answer received.

Adding a sleep 1 between the get does not help.
Pasting all the get's together in a telnet session works fine.

@domschl
Copy link
Owner

domschl commented Dec 26, 2018

Yes, there seems to be a problem with the new get API by @Andre0512, which uses non-blocking telnet-IO, causing the exception.
In my tests I still get correct results, even so the exception is raised. Does that work for you?

I have to investigate further: either we change get() to use blocking IO, which should be safe (@Andre0512: why did you decide to set blocking=False in get()?)

So for now:

  • if you receive correct replies, just ignore the exception.
  • Alternatively, use HTTP(S) protocol when connecting to FHEM

I will look into a fix for that exception, this will require an update for python-fhem,
probably either forcing blocking IO for telnet, or fixing the current exception on non-blocking.

domschl added a commit that referenced this issue Dec 26, 2018
@domschl
Copy link
Owner

domschl commented Dec 26, 2018

Ok, I am preparing 0.6.1 which will use blocking IO, if protocol is telnet. That should solve the issue with those exceptions.

@domschl
Copy link
Owner

domschl commented Dec 26, 2018

pip install -U fhem should update to 0.6.1 and this problem should be resolved. Thanks for the input!

@d1nd141
Copy link
Author

d1nd141 commented Dec 26, 2018

Thanks again for your fantastic work!
Getting values works fine now!
Unfortunately another error. Not sure if it's python-fhem related.
If you get two "Value" fields as return:

temp_1_wday:{u'fc1_day_of_week': {u'Value': u'Wed', u'Time': datetime.datetime(2018, 12, 26, 11, 46, 32)}, u'day_of_week': {u'Value': u'Wed', u'Time': datetime.datetime(2018, 12, 26, 11, 46, 32)}}
Traceback (most recent call last):
  File "own.py", line 27, in <module>
    temp_1_wday = temp_1_wday['Value']
KeyError: 'Value'


temp_1_wday = fh.get_device_reading("myWeather", "fc1_day_of_week")
print("temp_1_wday:"+str(temp_1_wday))
temp_1_wday = temp_1_wday['Value']

@domschl
Copy link
Owner

domschl commented Dec 26, 2018

You need to look at the dict that's returned. In this case, it's more complex:

Fhem has a bit of history, and datatypes tend to grow ;-)

temp_1_wday = fh.get_device_reading("myWeather", "fc1_day_of_week")
print(temp_1_wday)         
# Output:                                             
#{'day_of_week': {'Value': 'Wed', 'Time': datetime.datetime(2018, 12, 26, 12, 22, 4)}, 'fc1_day_of_week': {'Value': 'Wed', 'Time': datetime.datetime(2018, 12, 26, 12, 22, 4)}}
# So there are two dictionaries nested, one entry is 'day_of_week' and one is 'fc1_day_of_week'.
# This gets the first nested dict:
dict1=temp_1_wday['day_of_week']
# then:
print(dict1)                                                            
# Output: {'Value': 'Wed', 'Time': datetime.datetime(2018, 12, 26, 12, 22, 4)}
# So finally:
print(dict1['Value'])                                                  
# Output: Wed

@domschl
Copy link
Owner

domschl commented Dec 26, 2018

Generally, it is a good idea to open new issues for new problems. Then it's easier for others to see what it's all about.

@d1nd141
Copy link
Author

d1nd141 commented Dec 26, 2018

You are right (concerning opening a new issue).
I dod not want to "spam" the issue place too much, so i appended here ;-)
Tks!

@domschl domschl closed this as completed Dec 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants