Clickable notification with templating

I’m trying to get the new Epic games integration calendar to trigger a notification to my phone that links directly to the website of the store. This is what i have so far:

service: notify.mobile_app_myphone
data_template:
  title: Epic free game!
  message: >
    {{ state_attr('calendar.epic_games_store_free_games', 'message') }} is currently free. <a href="{{ state_attr('calendar.epic_games_store_free_games', 'description') | regex_findall('https?://[^\s]+') | join }}">Click here to visit the website.</a>

It looks great, but when you click it it opens HA app only.

That won’t work, as the message should be formatted in markdown, not in HTML.

But that’s only one thing, you need to setup an “actionable notification”, see here on how to do that:

EDIT: here’s another example, that might be useful:

Awesome, I didn’t see that when I was looking. I was actually using Chatgpt to work through a few different options and I had tried clickable link, markdown, and actions uri. Nothing worked although as this actually formatted it like a link I thought it was close.

It was really useful to get the regex stuff templated right.

It’s not far from a link to an actionable notification, but if you don’t know what to look for… :laughing:

You’ll likely get even more ideas from the docs, actionable notifications are really great. You can trigger an automation, call links or even start an app. Everything’s possible! :slight_smile:

I’m trying to form my HA to a more “presenting and informing” way, rather than me “polling or asking” HA for informations. Actionable notifications are an important part of that. :wink:

Besides actionable notifications, you also have the option to set the entire message to an action. Meaning tapping anywhere in the text body or the notification itself will preform an action. For android use the clickAction command.

https://companion.home-assistant.io/docs/notifications/notifications-basic?_highlight=clicka#opening-a-url

1 Like

I was going through the official doco too. I tried clickAction but either it doesn’t like Jinja queries or I’m not entirely sure why it didn’t work.

One of the other ideas was to call a script, then I could pull the name of the new free app from the message attribute, as well as using that regex above to pull the url out of the description.

Ideally clicking anywhere on the notification would be best.

what did the yaml look like?

then we should fix whatever was there for clickAction it should accept templates. Make sure to test template output in the editor.

Is there a way to do it via a script that will allow 2 separate Jinja queries though? Still calling clickAction?

I’ll post the other attempt here In a bit, once I’m on the PC.

not sure what you are asking but you can use jinja template with a condition in the field

assuming the template you have in your post provides a URL you should be able to use it directly:

clickAction: "{{ state_attr('calendar.epic_games_store_free_games', 'description') | regex_findall('https?://[^\s]+') | join }}"

i dont use the integration so unsure of the output

Ok, so as soon as I put in the regex the yaml is invalid.

service: notify.mobile_app_myphone
data:
  title: "A new Epic free game has arrived!"
  data:
    # iOS - not used url: "{{ state_attr("calendar.epic_games_store_free_games", "description") | regex_findall("https?://[^\s]+") | join }}"
    clickAction: "{{ state_attr("calendar.epic_games_store_free_games", "description") | regex_findall("https?://[^\s]+") | join }}"

For reference as you dont have the integration:

{{ state_attr('calendar.epic_games_store_free_games', 'description')
  | regex_findall('https?://[^\s]+')
  | join(', ') }}

Returns this in developer console

CLEAR

Result type: string

https://store.epicgames.com/en-US/p/circus-electrique

This template listens for the following state changed events:

  • Entity: calendar.epic_games_store_free_games

One of my concerns is that Jinja2 will apparently only parse regex on one feild in an automation. If this wasnt a restriction I would also use it to set the title to say what game was actually free. When this is working it would need both the URI for iOS and the clickAction for Android to run the regex.

Is there a way of running an action before to set variables to be used in the notification?

variables:
  game: "New free game: {{ state_attr('calendar.epic_games_store_free_games', 'message') }}"
  url: "{{ state_attr('calendar.epic_games_store_free_games', 'description') | regex_findall('https?://[^\s]+') | join(', ') }}"

Same issue - looks like maybe it doesn’t like the pipes, which means I cant trim the long description down to just the url. The whole entity looks like this for reference:

message: Circus Electrique
all_day: false
start_time: 2024-05-10 01:00:00
end_time: 2024-05-17 01:00:00
location:
description: Circus Electrique is part story-driven RPG, part tactics, part circus management, and completely enthralling. When everyday Londoners mysteriously turn into vicious killers, only the show’s talented performers possess the skills necessary to save the city.
https://store.epicgames.com/en-US/p/circus-electrique
friendly_name: Epic Games Store Free games

Ok, making progress however the actions still dont play out on android. I dont have an iOS device to test. This is the whole automation:

alias: Epic Free Games Notification
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.epic_games_store_free_games
condition: []
action:
  - service: script.epic_free_games_url_parser
    metadata: {}
    data: {}
  - variables:
      game: " {{ state_attr('calendar.epic_games_store_free_games','message') }}"
      url: " {{ states('input_text.epic_free_url') }} "
  - service: notify.mobile_app_myphone
    data:
      title: New free game on Epic
      message: Click to get {{ game }}
      data:
        actions:
          - action: OPEN
            title: Open Store
            uri: "{{ url }}"
mode: single

and this is the script that is triggered:

alias: Epic Free Games URL Parser
sequence:
  - service: input_text.set_value
    metadata: {}
    data:
      value: >-
        "{{ state_attr('calendar.epic_games_store_free_games', 'description') |
        regex_findall('https?://[^\s]+') | join(', ') }}"
    target:
      entity_id: input_text.epic_free_url
mode: single

So it all saves and seems valid but the uri doesnt work, ive tried open instead of uri, and ive tried clickAction too.

alias: Epic Free Games Notification
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.epic_games_store_free_games
condition: []
action:
  - service: script.epic_free_games_url_parser
    metadata: {}
    data: {}
  - variables:
      game: " {{ state_attr('calendar.epic_games_store_free_games','message') }}"
      url: " {{ states('input_text.epic_free_url') }} "
  - service: notify.mobile_app_myphone
    data:
      title: New free game on Epic
      message: Click to get {{ game }}
      data:
        clickAction: "{{ url }}"
mode: single

this doesn’t work?

1 Like

this is invalid

action: URI is required if you are supplying a uri

https://companion.home-assistant.io/docs/notifications/actionable-notifications#uri-values

edit: I see looks like iOS requires OPEN there interesting, yea for android it needs to be URI as its mentioned in a few places there

yes your issue is the misuse of double quotes, you changed what I had and your double quotes are conflicting. Make sure double and single quotes are used appropriately. Use my example directly there.

1 Like

No, it still goes to the HA app.

Yeah sorry, I missed that you had changed it.

There is something up with that template for the site. I have this integration, the template does return the address in dev tools but if I use it in an automation it fails.

I rarely use jinja but I can recreate this in nodered, extract the address from the string using jsonata and send a notification where clickAction works.

This still isn’t valid btw. As soon as you put that quoted state_attr & regex after clickAction the yaml editor stays red.

Thats awesome, but it would be great for everyone if we could get this working 100% in HA to remove the requirement for nodeRED. I’ve got a mate waiting on a solution so he can use it on his apple gear.

Could it be that the uri is expecting an object not a string?

Executed: May 17, 2024 at 2:45:35 PM
Result:
params:
  domain: notify
  service: mobile_app_myphone
  service_data:
    title: New free game on Epic
    message: 'Click to get Dragon Age: Inquisition – Game of the Year Edition'
    data:
      actions:
        - action: URI
          title: Open Store
          uri: '"https://store.epicgames.com/en-US/p/mystery-game-01"'
          clickAction: '"https://store.epicgames.com/en-US/p/mystery-game-01"'
  target: {}
running_script: false

Looks like its sending the double quotes as well as single quote marks.

OK - solution, and now it works! The script needed the double quotation marks removed.

service: input_text.set_value
metadata: {}
data:
  value: >-
    {{ state_attr('calendar.epic_games_store_free_games', 'description') |
    regex_findall('https?://[^\s]+') | join(', ') }}
target:
  entity_id: input_text.epic_free_url