目前我正在使用python代码,根据一些要求生成yaml文件。我有以下代码来完成一项任务:
import yaml
script = {}
room = 'kitchen'
dev_num = '_2'
script.update({'turn_off_all':
{
'sequence':[
{'service': 'light.turn_off',
'entity_id': 'group.turn_off_wired_lights'},
{'delay':
{'milliseconds': '>n'+ "{{% if is_state('light.{}{}', 'off') %}}n".format(room, dev_num) + ' 0n'+' {% else %}n'+' 500n'+' {% endif %}n' }
}
]}})
with open("script.yaml", 'w', encoding='utf-8') as f:
yaml.dump(script,f,allow_unicode=True,encoding='utf-8',sort_keys=False, width=float("inf"))
我在脚本中得到以下输出。yaml
turn_off_all:
sequence:
- service: light.turn_off
entity_id: group.turn_off_wired_lights
- delay:
milliseconds: ">n{% if is_state('light.kitchen_2', 'off') %}n 0n {% else %}n 500n {% endif %}n"
但我正在寻找不同的输出。我想要的输出是:
turn_off_all:
sequence:
- service: light.turn_off
entity_id: group.turn_off_wired_lights
- delay:
milliseconds: >
{% if is_state('light.kitchen_2', 'off') %}
0
{% else %}
500
{% endif %}
那么,如果有什么办法的话,我怎样才能得到想要的输出呢?我已经尝试过使用\n、\n、双引号和单引号的各种方法。。。
添加default_style='|'之后
我得到了以下输出。。。
"turn_off_all":
"sequence":
- "service": |-
light.turn_off
"entity_id": |-
group.turn_off_wired_lights
- "delay":
"milliseconds": |
>
{% if is_state('light.kitchen_2', 'off') %}
0
{% else %}
500
{% endif %}
default_style='>'的结果大致相同在转储函数内部
,提前谢谢。
您可以添加参数default_style='|'
或default_style='>'