-
Notifications
You must be signed in to change notification settings - Fork 6
Invalid json return when using get_device_reading #12
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
Comments
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. |
Hallo, Nevertheless i have another problem.
Adding a sleep 1 between the get does not help. |
Yes, there seems to be a problem with the new get API by @Andre0512, which uses non-blocking telnet-IO, causing the exception. I have to investigate further: either we change get() to use blocking IO, which should be safe (@Andre0512: why did you decide to set So for now:
I will look into a fix for that exception, this will require an update for python-fhem, |
Ok, I am preparing 0.6.1 which will use blocking IO, if protocol is telnet. That should solve the issue with those exceptions. |
|
Thanks again for your fantastic work!
|
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 |
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. |
You are right (concerning opening a new issue). |
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
The text was updated successfully, but these errors were encountered: