domschl / python-fhem

Python FHEM (home automation server) API
MIT License
15 stars 6 forks source link

Invalid json return when using get_device_reading #12

Closed d1nd141 closed 5 years ago

d1nd141 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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:

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 commented 5 years ago

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 commented 5 years ago

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

d1nd141 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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