Ntfy.sh with actions

Hello,

Just a small post to share my configuration to use ntfy.sh with home assistant and action on notification.

In shell_command :

ntfy: >
    curl
    -X POST
    --url 'https://USERNAME:PASSWORDM@URL_NTFY/{{ topic | default("maison") }}'
    --data "{{ message }}"
    --header 'X-Title: {{ title }}'
    --header 'X-Tags: {{ tags  | default("") }}'
    --header 'X-Priority: {{ priority | default("default") }}'
    --header 'X-Actions: {{ actions | default("") | replace("%ha%","URL_HA/api/events/ntfy,method=POST,headers.Authorization=Bearer LONG_LIFE_TOKEN") }}'
    --header 'X-Click: {{ click  | default("") }}'
    --header 'X-Icon: {{ icon  | default("") }}'

Don’t forget to replace :

  • USERNAME : username for ntfy.sh
  • PASSWORD : password for ntfy.sh
  • URL_NTFY : url of your ntfy.sh
  • URL_HA : url of your HA
  • LONG_LIFE_TOKEN : HA long token (on your profils page)

After that you can use action in automation like that :

alias: ask test
description: ""
trigger: []
condition: []
action:
  - variables:
      action_open: "{{ 'OPEN_' ~ context.id }}"
      action_close: "{{ 'CLOSE_' ~ context.id }}"
  - service: shell_command.ntfy
    data:
      topic: loic
      title: coucou
      message: The blinds are half-open. Do you want to adjust this?
      actions: >-
        http,Open,%ha%,body={"action":"{{action_open}}"};
        http,Close,%ha%,body={"action":"{{action_close}}"};
  - wait_for_trigger:
      - platform: event
        event_type: ntfy
        event_data:
          action: "{{ action_open }}"
      - platform: event
        event_type: ntfy
        event_data:
          action: "{{ action_close }}"
  - choose:
      - conditions: "{{ wait.trigger.event.data.action == action_open }}"
        sequence:
          - service: cover.open_cover
            target:
              entity_id: cover.some_cover
            data: {}
      - conditions: "{{ wait.trigger.event.data.action == action_close }}"
        sequence:
          - service: cover.close_cover
            target:
              entity_id: cover.some_cover
            data: {}
mode: single

I hope it’s can help user to use action with ntfy.sh.

4 Likes

Good day, thank you for sharing this. I’m using a rest command I found in another post. Please see below:

ntfy_notify:
  url: https://ntfy_url/homeassistant
  method: POST
  headers:
    authorization: "Bearer token"  
    username: "admin"
    password: !secret network_password
    title: "{{ title }}"
    icon: "{{ icon }}"
    priority: "{{ priority | default('default') }}"
    tags: "{{ tags | join(',') }}"
    click: "{{ click }}"
    attach: "{{ attach }}"
    actions: "{{ actions | join(',') }}"
  payload: "{{ message }}"

I was wondering if you could guide me on how to use the actions if using the above rest command.

I’ve tried to use your example:

      actions: >-
        http,Open,%ha%,body={"action":"{{action_open}}"};

by defining ha as a variable in the automation, but im failing. Any guidance is much appreciated.

Edit: never mind. got it. All i had to do is modify my actions definition in the rest command with yours.
Thank you.

Stumbled on this but I seem to be getting a 401 error when i click the action in ntfy is there something specific you did to get this working outside your home network? As I’m looking at the automation action I’m wondering if it’s a limitation of the action since it shows HTTP and not HTTPS?

I’m just not sure why the notification comes through and the click action doesn’t work (I did include a long-lived token).

Your ntfy instance needs to have a public domain.