Syntax For Using Service Template Variables

I am trying to use a template variable when calling a service. See my configuration below. If I set the topic to a string it works fine, I just can’t seem to figure out the syntax when I set it to the below template.

configuration.yaml

notify:
  - name: ntfy
    platform: rest
    resource: https://ntfy.domain.name/
    method: POST_JSON
    authentication: basic
    username: !secret ntfy_username
    password: !secret ntfy_password
    title_param_name: title
    message_param_name: message
    data:
      topic: "{{ ntfy_topic }}"

Testing service call

service: notify.ntfy
data:
  message: "Test #1"
  data:
    variables: 
      ntfy_topic: test

This doesn’t work. Anyone have any hints? I do see the following error in the logs, which suggest this should work if I can figure out the syntax.

Logger: homeassistant.helpers.template
Source: helpers/template.py:419
First occurred: 8:33:19 PM (13 occurrences)
Last logged: 9:39:16 PM

Template variable warning: 'ntfy_topic' is undefined when rendering '{{ ntfy_topic }}'

How do I define ntfy_topic when I call the service?

See: https://www.home-assistant.io/integrations/script/#passing-variables-to-scripts

You only need to use variables: when using the script.turn_on service. When calling a service directly just use:

  data:
    ntfy_topic: test

I tried both this:

service: notify.ntfy
data:
  message: "Test #1"
  data:
    ntfy_topic: test

And this:

service: notify.ntfy
data:
  message: "Test #1"
  ntfy_topic: test

The second one complains about extra keys that are not allowed. I thought this should be simple but it’s not working.

I don’t have the means to test the following; it’s based on other examples I have seen.

notify:
  - name: ntfy
    platform: rest
    resource: https://ntfy.domain.name/
    method: POST_JSON
    authentication: basic
    username: !secret ntfy_username
    password: !secret ntfy_password
    title_param_name: title
    message_param_name: message
    data:
      topic: "topic"
service: notify.ntfy
data:
  message: "Test #1"
  data:
    topic: test

I appreciate your reply. Filling in a topic with a value works as expected but that means to send a notification I will need to duplicate the configuration for each topic I want post to. Instead I was hoping I could set the topic when I call the service and have a single configuration entry. Your configuration works but setting the topic when calling the service won’t use that topic, it will use whatever is in the configuration.

Maybe I’ve run into something that’s not implemented or perhaps a bug? This really seems like it should be simple.

According to other examples, it should send the value specified in the service call. :man_shrugging:

https://community.home-assistant.io/search?q=Rest%20notification