Automation is malformed when saved

I have an automation that works, but it is malformed when I save it.

I enter it like this:

service: input_number.set_value
data_template:
  entity_id: input_number.nextdns_delta
  value: "{{ (states('sensor.nextdns_dns_queries') | float - states('input_number.nextdns_previous_value') | float) | string }}"

and it runs fine, but after I save my automation, it turn into this:

service: input_number.set_value
data_template:
  entity_id: input_number.nextdns_delta
  value: >-
    "{{ (states('sensor.nextdns_dns_queries') | float -
    states('input_number.nextdns_previous_value') | float) | string }}"

and it won’t work anymore.

This is the complete automation:

alias: NextDNS update monthly limit
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.nextdns_dns_queries
action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.nextdns_delta
      value: >-
        "{{ (states('sensor.nextdns_dns_queries') | float -
        states('input_number.nextdns_previous_value') | float) | string }}"
  - service: input_number.set_value
    data_template:
      entity_id: input_number.nextdns_previous_value
      value: "{{ states('sensor.nextdns_dns_queries') | float }}"
  - service: input_number.set_value
    data_template:
      entity_id: input_number.nextdns_monthly_limit
      value: >-
        "{{ (states('input_number.nextdns_monthly_limit') | float +
        states('input_number.nextdns_delta') | float) | string }}"
  - service: input_number.set_value
    data:
      entity_id: input_number.nextdns_delta
      value: 0

The last step isn’t really necessary, but that way I can see that it’s been triggered

What editor are you writing this in that is changing your YAML?

You can try writing it like this so the editor is less likely to change it:

alias: NextDNS update monthly limit
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.nextdns_dns_queries
action:
  - service: input_number.set_value
    target:
      entity_id: input_number.nextdns_delta
    data:
      value: >
        {{ (states('sensor.nextdns_dns_queries') | float -
        states('input_number.nextdns_previous_value') | float) | string }}
  - service: input_number.set_value
    target:
      entity_id: input_number.nextdns_previous_value
    data:
      value: >
        {{ states('sensor.nextdns_dns_queries') | float }}
  - service: input_number.set_value
    target:
      entity_id: input_number.nextdns_monthly_limit
    data:
      value: >
        {{ (states('input_number.nextdns_monthly_limit') | float +
        states('input_number.nextdns_delta') | float) | string }}
  - service: input_number.set_value
    target:
      entity_id: input_number.nextdns_delta
    data:
      value: 0
1 Like

I wrote it with the built in editor, but also tried notepad++. Your alterations seem to work, thanks! I’ll know for sure when the automation is triggered.