How to make the following structure inside the yaml file using python?

Viewed 17

Currently I am working with python code that generating yaml file according some requirements. And I have the following code to do one task:

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) + '  0\n'+' {% else %}\n'+' 500\n'+' {% 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"))

And I get the following output inside the script.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  0\n {% else %}\n 500\n {% endif %}\n"

But I am looking for different output. Mine desired output is:

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 %}

So, If there is any way How I can get desired output? I've already tried various way with \n, \n, and double and single quotes...

, thanks in advance.

0 Answers
Related