Error rendering data template: UndefinedError:'value' is undefined

I’m getting an error:
Failed to call service script/autodiscovery. Error rendering data template: UndefinedError: 'value' is undefined

I’m attempting to publish a mqtt autodiscovery for one of my sensors:

alias: autodiscovery
sequence:
  - service: mqtt.publish
    data:
      topic: homeassistant/sensor/Acurite-986-1R-51778/Acurite-986-1R-51778-B-1/config
      payload: >-
        {"device_class": "battery", "name": "Acurite-986-1R-51778-B",
        "unit_of_measurement": "%", "value_template": "{{ float(value|int) * 99
        + 1 }}", "state_topic":
        "rtl_433/rtl433/devices/Acurite-986/1R/51778/battery_ok", "unique_id":
        "Acurite-986-1R-51778-B", "device": {"identifiers":
        "Acurite-986-1R-51778", "name": "Acurite-986-1R-51778", "model":
        "Acurite-986", "manufacturer": "rtl_433"}}
mode: single

I’m assuming it’s trying to parse the value_template, but I don’t want it to. How do I get Home Assistant to ignore the payload I am sending and not try to parse it as part of the script?

Before HA changed it my payload was:

payload: '{"device_class": "battery", "name": "Acurite-986-1R-51778-B", "unit_of_measurement": "%", "value_template": "{{ float(value|int) * 99 + 1 }}", "state_topic": "rtl_433/rtl433/devices/Acurite-986/1R/51778/battery_ok", "unique_id": "Acurite-986-1R-51778-B", "device": {"identifiers": "Acurite-986-1R-51778", "name": "Acurite-986-1R-51778", "model": "Acurite-986", "manufacturer": "rtl_433"}}'

Okay, after more research and somehow missing it the first time I read the documentation it seems the payload must be formatted/escaped.

Still the same error. What else do I need to escape?

  - service: mqtt.publish
    data:
      topic: >-
        :
        homeassistant/sensor/Acurite-986-1R-51778/Acurite-986-1R-51778-T-1/config
      payload: >-
        {\"device_class\": \"temperature\", \"name\":
        \"Acurite-986-1R-51778-T\", \"unit_of_measurement\": \"\\u00b0C\",
        \"value_template\": \"{{ value|float }}\", \"state_topic\":
        \"rtl_433/rtl433/devices/Acurite-986/1R/51778/temperature_C\",
        \"unique_id\": \"Acurite-986-1R-51778-T\", \"device\": {\"identifiers\":
        \"Acurite-986-1R-51778\", \"name\": \"Acurite-986-1R-51778\", \"model\":
        \"Acurite-986\", \"manufacturer\": \"rtl_433\"}}

I finally got this working. The documentation really needs to be updated.

This is the code that works. Note no escaping is required. If you escape the string it shows up in the MQTT server as escaped. Also note that the value_template is wrapped in an extra set of "{{ }}". I hope this helps someone else and prevents them from spending hours trying to resolve the issue.

autodiscovery:
  alias: autodiscovery
  sequence:
    - service: mqtt.publish
      data:
        topic: "homeassistant/sensor/Acurite-986-1R-51778/Acurite-986-1R-51778-B-1/config"
        payload: >-
          {
            "device_class": "battery",
            "name": "Acurite-986-1R-51778-B",
            "unit_of_measurement": "%",
            "value_template": "{{ "{{ float(value|int) * 99 + 1 }}" }}",
            "state_topic": "rtl_433/rtl433/devices/Acurite-986/1R/51778/battery_ok",
            "unique_id": "Acurite-986-1R-51778-B",
            "device": {
              "identifiers": "Acurite-986-1R-51778",
              "name": "Acurite-986-1R-51778",
              "model": "Acurite-986",
              "manufacturer": "rtl_433"
              }
          }
  mode: single