Python 为什么可以';我不能加时间,只能减吗?

Python 为什么可以';我不能加时间,只能减吗?,python,jinja2,Python,Jinja2,这很好: {{ strptime(states('input_datetime.music_alarm'), "%H:%M:%S") - strptime("10", "%M") }} 但这带来了一个错误: {{ strptime(states('input_datetime.music_alarm'), "%H:%M:%S") + strptime("10", "%M") }} 状态('input_datetime.music_alarm')等于一个时间,如08:00:00 我用jin

这很好:

{{ strptime(states('input_datetime.music_alarm'), "%H:%M:%S") - strptime("10", "%M") }} 
但这带来了一个错误:

{{ strptime(states('input_datetime.music_alarm'), "%H:%M:%S") + strptime("10", "%M") }} 
状态('input_datetime.music_alarm')等于一个时间,如08:00:00

我用jinja2做家庭助理。这里是错误

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/automation/__init__.py", line 336, in async_trigger
    yield from self._async_action(self.entity_id, variables)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/automation/__init__.py", line 425, in action
    yield from script_obj.async_run(variables)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/script.py", line 158, in async_run
    await self._async_call_service(action, variables)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/script.py", line 187, in _async_call_service
    self.hass, action, True, variables, validate_config=False)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/service.py", line 72, in async_call_from_config
    config[CONF_SERVICE_DATA_TEMPLATE], variables))
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py", line 56, in render_complex
    for key, item in value.items()}
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py", line 56, in <dictcomp>
    for key, item in value.items()}
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py", line 57, in render_complex
    return value.async_render(variables)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py", line 132, in async_render
    return self._compiled.render(kwargs).strip()
  File "/srv/homeassistant/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/srv/homeassistant/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: bad operand type for unary -: 'datetime.datetime'
执行作业时出错:从未检索到任务异常 回溯(最近一次呼叫最后一次): 文件“/usr/lib/python3.5/asyncio/tasks.py”,第239行,步骤 结果=coro.send(无) 文件“/srv/homesAssistant/lib/python3.5/site packages/homesAssistant/components/automation/__init__;.py”,第336行,在异步触发器中 self.\u异步\u操作的收益(self.entity\u id,变量) 文件“/srv/homesAssistant/lib/python3.5/site packages/homesAssistant/components/automation/_init__.py”,第425行,正在运行 脚本异步运行的收益(变量) 文件“/srv/homeassistant/lib/python3.5/site packages/homeassistant/helpers/script.py”,第158行,异步运行 等待self.\u异步\u调用\u服务(操作、变量) 文件“/srv/homeassistant/lib/python3.5/site packages/homeassistant/helpers/script.py”,第187行,在异步调用服务中 self.hass,action,True,变量,validate\u config=False) 文件“/srv/homesAssistant/lib/python3.5/site packages/homesAssistant/helpers/service.py”,第72行,来自配置的异步调用 配置[配置服务数据模板],变量) 文件“/srv/homeassistant/lib/python3.5/site packages/homeassistant/helpers/template.py”,第56行,在render_complex中 对于键,value.items()中的项 文件“/srv/homeassistant/lib/python3.5/site packages/homeassistant/helpers/template.py”,第56行,在 对于键,value.items()中的项 文件“/srv/homeassistant/lib/python3.5/site packages/homeassistant/helpers/template.py”,第57行,在render_complex中 返回值.async_render(变量) 文件“/srv/homeassistant/lib/python3.5/site packages/homeassistant/helpers/template.py”,第132行,异步呈现 返回self._compiled.render(kwargs.strip()) 文件“/srv/homeassistant/lib/python3.5/site packages/jinja2/environment.py”,第1008行,在渲染中 返回self.environment.handle\u异常(exc\u info,True) 文件“/srv/homeassistant/lib/python3.5/site-packages/jinja2/environment.py”,第780行,在handle\u异常中 重放(exc_类型、exc_值、tb) 文件“/srv/homeassistant/lib/python3.5/site-packages/jinja2/_-compat.py”,第37行,请重新登录 通过_回溯(tb)提升值 文件“”,第1行,顶层模板代码 TypeError:一元数的操作数类型错误-:'datetime.datetime'
您必须使用timedelta,并且代码中存在多个问题

1/错误使用datetime.datetime.strtime()

您必须解析FullDateTime才能有正确的行为

2/不能对两个日期时间求和

基本上,
+
是禁止的,以避免错误,您只需要
-
,因为您只需反转表达式中的变量即可获得负的时间增量

>>> print(type(datetime.datetime.strptime("10", "%M") - datetime.datetime.strptime('08:00:00', "%H:%M:%S")))
<class 'datetime.timedelta'>
>>> print(datetime.datetime.strptime("10", "%M") - datetime.datetime.strptime('08:00:00', "%H:%M:%S"))
-1 day, 16:10:00
>>> print(datetime.datetime.strptime('08:00:00', "%H:%M:%S") - datetime.datetime.strptime("10", "%M"))
7:50:00

两个时刻之间的时间差是一个持续时间。两个时刻的总和意味着什么?您可能需要添加一个持续时间,它由
datetime.timedelta
表示。timedelta在jinja2中似乎不可用
>>> print(type(datetime.datetime.strptime("10", "%M") - datetime.datetime.strptime('08:00:00', "%H:%M:%S")))
<class 'datetime.timedelta'>
>>> print(datetime.datetime.strptime("10", "%M") - datetime.datetime.strptime('08:00:00', "%H:%M:%S"))
-1 day, 16:10:00
>>> print(datetime.datetime.strptime('08:00:00', "%H:%M:%S") - datetime.datetime.strptime("10", "%M"))
7:50:00
import datetime
from jinja2 import Template

jinga = Template('{{ strptime(states("input_datetime.music_alarm"), "%H:%M:%S") - timedelta(minutes=10) }} ')
jinga.globals['timedelta'] = datetime.timedelta
print(jinga.render())