python - 为什么我不能添加strptimes,只能减去?
问题描述
这工作正常:
{{ strptime(states('input_datetime.music_alarm'), "%H:%M:%S") - strptime("10", "%M") }}
但这会引发错误:
{{ strptime(states('input_datetime.music_alarm'), "%H:%M:%S") + strptime("10", "%M") }}
states('input_datetime.music_alarm') 等于一个时间,比如 08:00:00
我正在使用 jinja2 作为 homeassistant。这是错误。
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'
解决方案
您必须使用 timedelta 并且您的代码中有多个问题。
1/ 错误使用 datetime.datetime.strptime()
>>> import datetime
>>> print(datetime.datetime.strptime("10", "%M"))
1900-01-01 00:10:00
>>> print(datetime.datetime.strptime('08:00:00', "%H:%M:%S"))
1900-01-01 08:00:00
您必须解析完整的日期时间才能获得正确的行为。
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
您可以以相反的顺序看到这会给您错误的信息-1 day, 16:10:00
,因为这无法在没有错误的情况下处理。
3/ 您可以将 timedelta 注册到您的模板
默认情况下,strptime() 在 Jinja2 中不可用,因此请使用 timedelta()...
类似的东西:
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())
推荐阅读
- sql-server - 是否“使用
” 清除/重置声明的变量 - python - 单击单选按钮或更改 PyQt 中的旋转框时,组合框索引值返回默认值 0
- sql-server - 将数据从 CSV 插入 SQL server,插入问题
- python - 将命名元组有效插入熊猫数据帧
- node.js - 当组件没有 removeEventListener 时,如何清理 .on 事件侦听器?
- c++ - 在 Windows 中使用 unicode 文件名创建一个新文件(不读取现有文件)
- visual-studio-mac - Visual Studio Mac 不在资源管理器中显示文件,但文件在文件夹中
- python - 散景可视化不显示在 html 输出文件中
- mysql - 获取 MySQL 数据库中每月的条目数
- rest - HTTP:需要更改密码时返回什么响应?