How to use sensor output in notification data?

I want to have multiple notifications about important stuff, each adding 1 to the app’s badge, so I made a sensor that counts them:

- sensor:
  - unique_id: app_badge_counter
    name: App badge counter
    state: >
      {% set counter = 0 %}
      {% if is_state("binary_sensor.sensor_1", "on") %}
        {% set counter = counter + 1 %}
      {%endif%}
      {% if is_state("binary_sensor.sensor_2", "on") %}
        {% set counter = counter + 1 %}
      {%endif%}
      {{ counter }}

Which seems to be working properly so far.
But when I try to edit my send/clear notification automations like this:

action:
  - service: notify.me
    data:
      message: clear_notification
      data:
        tag: sensor_1
        push:
          badge: {{ sensor.app_badge_counter }}

after I save and open it again, it gets rewritten to this:

action:
  - service: notify.me
    data:
      message: clear_notification
      data:
        tag: sensor_1
        push:
          badge:
            "[object Object]": null

Is there a way I can achieve what I want? Or am I trying to do it correctly but it’s a known/unknown bug? Or if I directly edit the yaml and never edit it from the web interface, it should work?

Try:

badge: "{{ states('sensor.app_badge_counter') }}"

One-line templates must be wrapped in quotes and you must use the states() function to get the value.

Or:

badge: >
  {{ states('sensor.app_badge_counter') }}

What happened in your case is that it got interpreted as a set in a set containing a string.