Ntfy notification via CLI/CURL does not work

Hey there.

I’ve tried to add ntfy as a notification service to my Home Assistant instance. I wrote a command-line script, that takes either STDIN or CLI arguments as input and then uses curl to my self-hosted notification server. For debugging purposes it even sends out a notification even if the message is missing. Everything works fine when I ssh into my HA and run the script from command line (execution rights are fine). But whatever I try, I seem unable to use this as a notification service in HA.

I’ve tried multiple variations of this configuration:

notify:
  - name: ntfy
    platform: command_line
    command: /root/ntfy.sh

It does show up in the dev section, and I can trigger it without errors – but no notification is sent. Does anybody have an idea what I am doing wrong? Did I miss something?

I have still to try shell_command as suggest in some other threads – if I understand it correctly, I would lose the ability to notify with a custom message. But I might be wrong here.

Thanks!

Home Assistant 2022.7.6
Supervisor 2022.07.0
Operating System 8.2
Frontend 20220707.1 - latest

Did you manage to integrate ntfy in the end?

No, sadly not. I switched to Pushover.

I’m not sure if this meets your requirements, but here’s what it looks like to me.

notify:
  - name: ntfy
    platform: rest
    method: POST_JSON
    data:
      topic: secret-topic
    title_param_name: title
    message_param_name: message
    resource: https://ntfy.sh

In the node-red addon I configured node-red-contrib-sse-client for incoming ntfy messages. But the ‘websocket in’ node works for that too.

2 Likes

I am trying to send ntfy.sh via HA. It works fine with the title and body, but I can’t send the priority and tag. Here is my configuration:
notify.yaml

- name: ntfy
  platform: rest
  method: POST_JSON
  data:
    topic: test 
  title_param_name: title
  message_param_name: message
  resource: https://ntfy.sh 

script:

alias: Test ntfy
sequence:
  - service: notify.ntfy
    data:
      message: Nachricht
      title: Titel
mode: single

Tag and priority work in Tasker with this command:

But not in HA. I have tried the following:

alias: Test ntfy
sequence:
  - service: notify.ntfy
    data:
      message: Nachricht
      title: Titel
      data:
        priority: 2
        tags: test, warning
mode: single
icon: mdi:message-alert

and things like:

      message: Nachricht
      title: Titel
      data:
        data:
          priority: 2
          tags: test, warning

The messages are sent, but without the properties. Does anyone have any ideas?

There are a few examples, which may help you:

https://ntfy.sh/docs/examples/

Thanks! If I see it correctly, priority and tag can only be defined in notify.yalm. I would like to include it in the script or automation.

I didn’t tried it myself. There is a Matrix group for ntfy. I guess, there you have the biggest chance to get an answer, because ntfy isn’t widely known yet.

I’ll try it, thank You.

It is possible to send the service with all its properties via the terminal using curl, which works like this:

curl -d "Hi" ntfy.sh/topic -H "tags: test" -H "priority: 4" -H "Title: Titel"

But now I don’t always want to send the same message via a ‘shell_command’ in the configuration.yaml. How could I fill the command with variables? I can create the command line by:

{% set cmd = "curl -d " + now().strftime('%Y%m%d') + " ntfy.sh/topic -H 'tags: test' -H 'priority: 4' -H 'Title: " + states('sensor.value') + "'"  %}
{{ cmd }}

But: how do I execute it in a script? :man_shrugging:

1 Like

I have solved the priority issue by creating two entries in configuration.yaml

notify:
- name: ntfy_urgent
  platform: rest
  method: POST_JSON
  data:
    topic: test 
    priority: 5
  title_param_name: title
  message_param_name: message
  resource: https://ntfy.sh 

- name: ntfy
    platform: rest
    method: POST_JSON
    data:
      topic: secret-topic
    title_param_name: title
    message_param_name: message
    resource: https://ntfy.sh

the first for urgent notifications and the other for normal ones.
it works very well it skips in DND mode on my android phone

3 Likes

If you configure your ntfy rest_command with templates, you can override the values when the service is called. So start by adding this to your configuration:

rest_command:
  notify_phone:
    url: https://ntfy.sh/<put your topic here>
    method: POST
    headers:
      title: "{{ title }}"
      priority: "{{ priority | default('default') }}"
      tags: "{{ tags | join(',') }}"
    payload: "{{ message }}"

Then, and automation like this will send a default priority notification:

service: rest_command.notify_phone
data:
  title: Heater pump has been running long
  message: is it clogged?
  tags:
    - warning

And this will send an urgent one:

service: rest_command.notify_phone
data:
  title: Water sensor detected water
  message: {{ trigger.to_state.name }}
  priority: urgent
  tags:
    - droplet

(I haven’t actually seen the water sensor one work - but I have the same templated message for an HA notification that does work.)

1 Like

I just published a HACS integration that I wrote for myself. Please feel free to use it and submit PR requests for any enhancements.

1 Like