I’m getting the following TypeError when trying to set the datetime of an input_datetime component from appdaemon. Anyone able to assist?
Error:
“TypeError: Object of type ‘datetime’ is not JSON serializable”
Trying to call:
self.set_state(“input_datetime.both_date_and_time”, state = new_time)
Code Except:
# Get Time
current_time_str = self.get_state("input_datetime.both_date_and_time", attribute="state")
print("Current Time String: %s" % current_time_str)
current_time = datetime.datetime.strptime(current_time_str, "%Y-%m-%d %H:%M:%S")
print("Current Time: %s" % current_time)
# Determine New time
new_time = current_time + datetime.timedelta(minutes = 10)
print("New Time: %s" % new_time)
# Set New Time
self.set_state("input_datetime.both_date_and_time", state = new_time)
Error:
Current Time String: 2018-03-01 10:00:00
Current Time: 2018-03-01 10:00:00
New Time: 2018-03-01 10:10:00
2018-03-27 15:28:31.977776 WARNING AppDaemon: Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 543, in worker
funcref(args["event"], data, args["kwargs"])
File "./apps/energyOptimisation.py", line 45, in increase_time_event_listener
self.set_state("input_datetime.both_date_and_time", state = new_time)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/appdaemon/plugins/hass/hassapi.py", line 134, in set_state
apiurl, headers=headers, json=new_state, verify=certpath
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 494, in request
prep = self.prepare_request(req)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/models.py", line 308, in prepare
self.prepare_body(data, files, json)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/models.py", line 458, in prepare_body
body = complexjson.dumps(json)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'datetime' is not JSON serializable
Section of configuration.yaml
script:
test_appdaemon_event:
sequence:
# This is Home Assistant Script Syntax
- event: appdaemon_event
event_data:
name: optimise
message: event to trigger optimise manually
test_appdaemon_event_increase_time:
sequence:
# This is Home Assistant Script Syntax
- event: appdaemon_event_increase_time
event_data:
name: increase_time
message: event to trigger optimise manually