Help with automation to send discord embedded message with the attributes of free epic games

I’m trying to use the epic games integration along with notify.discord to send a notification to everyone in my server when a new free game is available on epic.

alias: Epic Games New Games Notification
trigger:
  - platform: state
    entity_id: sensor.epic_games
action:
  - service: notify.disc_assistant
    data:
      message: ""
      target: '87285139647683840'
      data_template:
        embed:
          title: 'Free Epic Games'
          url: 'https://store.epicgames.com/en-US/free-games'
          color: 199363
          image:
            url: 'https://www.macobserver.com/wp-content/uploads/2020/06/Epic-games-logo.jpg'
          fields:

Im not quite sure what i need to put into the fields section though. Before bothering anyone i decided to see if gpt could give me the answer i was looking for but that didn’t work. This was its response.

          fields:
            {% set games = trigger.to_state.attributes.games %}
            {% for game in games %}
              - name: '{{ game.title }}'
                value: >
                  Rating: {{ game.rating }}
                  Release Date: {{ game.release }}
                  Runtime: {{ game.runtime }}
                  Studio: {{ game.studio }}
                inline: false
            {% endfor %}

Would really appreciate help getting this one going and thanks in advance.

Edit: Here is the attribute data I’m getting for the sensor.

data: 
- title_default: $title
  line1_default: $rating
  line2_default: $release
  line3_default: $runtime
  line4_default: $studio
  icon: mdi:arrow-down-bold
- title: Idle Champions of the Forgotten Realms
  poster: >-
    https://cdn1.epicgames.com/offer/7e508f543b05465abe3a935960eb70ac/IdleChampions_FreeGamesPromo_1200x16001_1200x1600-764258f78394b9f79815bbd9010e8454
  rating: N/A
  synopsis: >-
    Idle Champions is a licensed Dungeons & Dragons strategy management video
    game uniting iconic characters from novels, campaigns, and shows into one
    epic adventure.
  studio: Codename Entertainment
  runtime: '2021-02-16'
  release: $date
  airdate: '2021-02-16'
- title: 'theHunter: Call of the Wild™'
  poster: >-
    https://cdn1.epicgames.com/salesEvent/salesEvent/EGS_theHunterCalloftheWild_ExpansiveWorlds_S2_1200x1600-1e9b46aaabc33fe0a08cf5b418e76ba2
  rating: N/A
  synopsis: >-
    Experience an atmospheric hunting game like no other in this realistic and
    visually breathtaking open world. Immerse yourself in the atmospheric single
    player campaign, or share the ultimate hunting experience with friends.
  studio: Expansive Worlds AB
  runtime: '2021-11-18'
  release: $date
  airdate: '2021-11-18'
- title: Guacamelee! Super Turbo Championship Edition
  poster: >-
    https://cdn1.epicgames.com/spt-assets/7041a1ca4ed543459b95d78887047bd9/guacamelee-stce-ztldk.jpg
  rating: '80'
  synopsis: >-
    Guacamelee! is a Metroidvania-style action-platformer set in a magical
    Mexican-inspired world. The game draws its inspiration from traditional
    Mexican culture and folklore, and features many interesting and unique
    characters.
  studio: Drinkbox Studios Inc.
  runtime: '2023-06-08'
  release: $date
  airdate: '2023-06-08'
- title: Guacamelee! 2
  poster: >-
    https://cdn1.epicgames.com/spt-assets/b05acd7d28d34f6d93a961b0e53ddf0b/guacamelee-2-1lym2.jpg
  rating: '84'
  synopsis: >-
    Head back to the Mexiverse in this long-awaited sequel to the smash hit
    Guacamelee! Uppercut your way to victory across stunning new hand-crafted
    levels. Featuring a dense and colorful world, new luchador moves, sassy new
    bosses, twice the enemies, and 300% more chickens!
  studio: Drinkbox Studios Inc.
  runtime: '2023-06-08'
  release: $date
  airdate: '2023-06-08'
- title: PAYDAY 2
  poster: >-
    https://cdn1.epicgames.com/offer/d5241c76f178492ea1540fce45616757/mammoth-h1nvv_2560x1440-ac346d6ece5ec356561e112fbddb2dc1
  rating: '79'
  synopsis: >-
    PAYDAY 2 is an action-packed, four-player co-op shooter that once again lets
    gamers don the masks of the original PAYDAY crew - Dallas, Hoxton, Wolf and
    Chains - as they descend on Washington DC for an epic crime spree.
  studio: Epic Dev Test Account
  runtime: '2023-06-01'
  release: $date
  airdate: '2023-06-01'

If you plan to use Jinja, you need to add the multiline indicator thing, and return data in JSON format (so [1, 2] instead of - 1\n- 2).

Thanks for replying but would you mind elaborating on that? I’m really bad with the templates.

Something like this:

fields: |
  {% set games = trigger.to_state.attributes.games %}
  {% set game_namespace = namespace(game_list=[]) %}
  {% for game in games %}
    {% set game_data = {
      "name": game.title,
      "value": "Rating: " + game.rating + "\n" +
               "Release Date: " + game.release + "\n" +
               "Runtime: " + game.runtime + "\n" +
               "Studio: " + game.studio
    } %}
    {% set game_namespace.game_list = game_namespace.game_list + [game_data] %}
  {% endfor %}
  {{ game_namespace.game_list | tojson }}

Got an error in the logs.

Logger: homeassistant.components.automation.epic_games_free_notification
Source: components/automation/__init__.py:268
Integration: Automation (documentation, issues)
First occurred: 11:34:55 PM (7 occurrences)
Last logged: 11:40:16 PM

Error while executing automation automation.epic_games_free_notification: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'
Error while executing automation automation.epic_games_free_notification: extra keys not allowed @ data['data_template']

I’ll blame the original GPT code. Try replacing trigger.to_state with states("sensor.epic_games").

alias: Epic Games New Games Notification
trigger:
  - platform: state
    entity_id: sensor.epic_games
action:
  - service: notify.disc_assistant
    data:
      message: ""
      target: '87285139947683840'
      data_template:
        embed:
          title: 'Free Epic Games'
          url: 'https://store.epicgames.com/en-US/free-games'
          color: 199363
          image:
            url: 'https://www.macobserver.com/wp-content/uploads/2020/06/Epic-games-logo.jpg'
          fields: |
            {% set games = states("sensor.epic_games") %}
            {% set game_namespace = namespace(game_list=[]) %}
            {% for game in games %}
              {% set game_data = {
                "name": game.title,
                "value": "Rating: " + game.rating + "\n" +
                         "Release Date: " + game.release + "\n" +
                         "Runtime: " + game.runtime + "\n" +
                         "Studio: " + game.studio
              } %}
              {% set game_namespace.game_list = game_namespace.game_list + [game_data] %}
            {% endfor %}
            {{ game_namespace.game_list | tojson }}

I think this is what you meant. Still not working though. I get this error

Logger: homeassistant.components.automation.epic_games_free_notification
Source: components/automation/__init__.py:268
Integration: Automation (documentation, issues)
First occurred: 11:34:55 PM (9 occurrences)
Last logged: 11:49:32 PM

Error while executing automation automation.epic_games_free_notification: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'
Error while executing automation automation.epic_games_free_notification: extra keys not allowed @ data['data_template']
Error while executing automation automation.epic_games_free_notification: Error rendering data template: UndefinedError: 'str object' has no attribute 'rating'

I think you should change in games to in games.attributes.data?

Thank you for helping me with this. I’ll try this change when I get out of work tonight. Again thank you.

That unfortunately also didn’t work. I don’t wanna keep eating away into too much of your time for something so frivolous though. Thanks for making the attempt. Cheers!

Can you send the current version you have? What exactly doesn’t work? Send any errors.

This is what i have right now

alias: Epic Games New Games Notification
trigger:
  - platform: state
    entity_id: sensor.epic_games
action:
  - service: notify.disc_assistant
    data:
      message: ""
      target: "87285139947683840"
      data_template:
        embed:
          title: Free Epic Games
          url: https://store.epicgames.com/en-US/free-games
          color: 199363
          image:
            url: >-
              https://www.macobserver.com/wp-content/uploads/2020/06/Epic-games-logo.jpg
          fields: |
            {% set games = states("sensor.epic_games") %}
            {% set game_namespace = namespace(game_list=[]) %}
            {% for game in games.attributes.data %}
              {% set game_data = {
                "name": game.title,
                "value": "Rating: " + game.rating + "\n" +
                         "Release Date: " + game.release + "\n" +
                         "Runtime: " + game.runtime + "\n" +
                         "Studio: " + game.studio
              } %}
              {% set game_namespace.game_list = game_namespace.game_list + [game_data] %}
            {% endfor %}
            {{ game_namespace.game_list | tojson }}

i get 3 errors in the logs

Logger: homeassistant.helpers.template
Source: helpers/template.py:233
First occurred: 2:04:45 AM (3 occurrences)
Last logged: 4:50:33 AM

`Template variable error: 'str object' has no attribute 'attributes' when rendering '{% set games = states("sensor.epic_games") %} {% set game_namespace = namespace(game_list=[]) %} {% for game in games.attributes.data %} {% set game_data = { "name": game.title, "value": "Rating: " + game.rating + "\n" + "Release Date: " + game.release + "\n" + "Runtime: " + game.runtime + "\n" + "Studio: " + game.studio } %} {% set game_namespace.game_list = game_namespace.game_list + [game_data] %} {% endfor %} {{ game_namespace.game_list | tojson }}'`
Logger: homeassistant.components.automation.epic_games_free_notification
Source: helpers/script.py:410
Integration: Automation (documentation, issues)
First occurred: 2:04:45 AM (3 occurrences)
Last logged: 4:50:33 AM

Epic Games New Games Notification: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'str object' has no attribute 'attributes'
Logger: homeassistant.components.automation.epic_games_free_notification
Source: components/automation/__init__.py:268
Integration: Automation (documentation, issues)
First occurred: 4:50:33 AM (1 occurrences)
Last logged: 4:50:33 AM

Error while executing automation automation.epic_games_free_notification: Error rendering data template: UndefinedError: 'str object' has no attribute 'attributes'

EDIT: Heres what the attributes look like

data: 
- title_default: $title
  line1_default: $rating
  line2_default: $release
  line3_default: $runtime
  line4_default: $studio
  icon: mdi:arrow-down-bold
- title: Idle Champions of the Forgotten Realms
  poster: >-
    https://cdn1.epicgames.com/offer/7e508f543b05465abe3a935960eb70ac/IdleChampions_FreeGamesPromo_1200x16001_1200x1600-764258f78394b9f79815bbd9010e8454
  rating: N/A
  synopsis: >-
    Idle Champions is a licensed Dungeons & Dragons strategy management video
    game uniting iconic characters from novels, campaigns, and shows into one
    epic adventure.
  studio: Codename Entertainment
  runtime: '2021-02-16'
  release: $date
  airdate: '2021-02-16'
- title: 'theHunter: Call of the Wild™'
  poster: >-
    https://cdn1.epicgames.com/salesEvent/salesEvent/EGS_theHunterCalloftheWild_ExpansiveWorlds_S2_1200x1600-1e9b46aaabc33fe0a08cf5b418e76ba2
  rating: N/A
  synopsis: >-
    Experience an atmospheric hunting game like no other in this realistic and
    visually breathtaking open world. Immerse yourself in the atmospheric single
    player campaign, or share the ultimate hunting experience with friends.
  studio: Expansive Worlds AB
  runtime: '2021-11-18'
  release: $date
  airdate: '2021-11-18'
- title: Guacamelee! Super Turbo Championship Edition
  poster: >-
    https://cdn1.epicgames.com/spt-assets/7041a1ca4ed543459b95d78887047bd9/guacamelee-stce-ztldk.jpg
  rating: '80'
  synopsis: >-
    Guacamelee! is a Metroidvania-style action-platformer set in a magical
    Mexican-inspired world. The game draws its inspiration from traditional
    Mexican culture and folklore, and features many interesting and unique
    characters.
  studio: Drinkbox Studios Inc.
  runtime: '2023-06-08'
  release: $date
  airdate: '2023-06-08'
- title: Guacamelee! 2
  poster: >-
    https://cdn1.epicgames.com/spt-assets/b05acd7d28d34f6d93a961b0e53ddf0b/guacamelee-2-1lym2.jpg
  rating: '84'
  synopsis: >-
    Head back to the Mexiverse in this long-awaited sequel to the smash hit
    Guacamelee! Uppercut your way to victory across stunning new hand-crafted
    levels. Featuring a dense and colorful world, new luchador moves, sassy new
    bosses, twice the enemies, and 300% more chickens!
  studio: Drinkbox Studios Inc.
  runtime: '2023-06-08'
  release: $date
  airdate: '2023-06-08'
- title: PAYDAY 2
  poster: >-
    https://cdn1.epicgames.com/offer/d5241c76f178492ea1540fce45616757/mammoth-h1nvv_2560x1440-ac346d6ece5ec356561e112fbddb2dc1
  rating: '79'
  synopsis: >-
    PAYDAY 2 is an action-packed, four-player co-op shooter that once again lets
    gamers don the masks of the original PAYDAY crew - Dallas, Hoxton, Wolf and
    Chains - as they descend on Washington DC for an epic crime spree.
  studio: Epic Dev Test Account
  runtime: '2023-06-01'
  release: $date
  airdate: '2023-06-01'

Remove {% set games = states("sensor.epic_games") %}. Change {% for game in games.attributes.data %} to {% for game in state_attr("sensor.epic_games", "data") %}.

So heres what i have after making the changes

alias: Epic Games New Games Notification
trigger:
  - platform: state
    entity_id: sensor.epic_games
action:
  - service: notify.disc_assistant
    data:
      message: ""
      target: "87285139947683840"
      data_template:
        embed:
          title: Free Epic Games
          url: https://store.epicgames.com/en-US/free-games
          color: 199363
          image:
            url: >-
              https://www.macobserver.com/wp-content/uploads/2020/06/Epic-games-logo.jpg
          fields: |
            {% set game_namespace = namespace(game_list=[]) %}
            {% for game in state_attr("sensor.epic_games", "data") %}
              {% set game_data = {
                "name": game.title,
                "value": "Rating: " + game.rating + "\n" +
                         "Release Date: " + game.release + "\n" +
                         "Runtime: " + game.runtime + "\n" +
                         "Studio: " + game.studio
              } %}
              {% set game_namespace.game_list = game_namespace.game_list + [game_data] %}
            {% endfor %}
            {{ game_namespace.game_list | tojson }}

Here are the new errors

Logger: homeassistant.components.automation.epic_games_free_notification
Source: components/automation/__init__.py:268
Integration: Automation (documentation, issues)
First occurred: June 18, 2023 at 4:50:33 AM (3 occurrences)
Last logged: 5:05:43 AM

Error while executing automation automation.epic_games_free_notification: Error rendering data template: UndefinedError: 'str object' has no attribute 'attributes'
Error while executing automation automation.epic_games_free_notification: Error rendering data template: UndefinedError: 'dict object' has no attribute 'rating'

and

Logger: homeassistant.components.automation.epic_games_free_notification
Source: helpers/script.py:410
Integration: Automation (documentation, issues)
First occurred: June 18, 2023 at 2:04:45 AM (29 occurrences)
Last logged: 5:05:43 AM



Epic Games New Games Notification: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'str object' has no attribute 'attributes'
Epic Games New Games Notification: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'dict object' has no attribute 'rating'

Go to the template editor in devtools, and set the template to {{ state_attr("sensor.epic_games", "data") }}. Reply with a screenshot of the whole right panel.

[
  {
    "title_default": "$title",
    "line1_default": "$rating",
    "line2_default": "$release",
    "line3_default": "$runtime",
    "line4_default": "$studio",
    "icon": "mdi:arrow-down-bold"
  },
  {
    "title": "Idle Champions of the Forgotten Realms",
    "poster": "https://cdn1.epicgames.com/offer/7e508f543b05465abe3a935960eb70ac/IdleChampions_FreeGamesPromo_1200x16001_1200x1600-764258f78394b9f79815bbd9010e8454",
    "rating": "N/A",
    "synopsis": "Idle Champions is a licensed Dungeons & Dragons strategy management video game uniting iconic characters from novels, campaigns, and shows into one epic adventure.",
    "studio": "Codename Entertainment",
    "runtime": "2021-02-16",
    "release": "$date",
    "airdate": "2021-02-16"
  },
  {
    "title": "theHunter: Call of the Wild™",
    "poster": "https://cdn1.epicgames.com/salesEvent/salesEvent/EGS_theHunterCalloftheWild_ExpansiveWorlds_S2_1200x1600-1e9b46aaabc33fe0a08cf5b418e76ba2",
    "rating": "N/A",
    "synopsis": "Experience an atmospheric hunting game like no other in this realistic and visually breathtaking open world. Immerse yourself in the atmospheric single player campaign, or share the ultimate hunting experience with friends.",
    "studio": "Expansive Worlds AB",
    "runtime": "2021-11-18",
    "release": "$date",
    "airdate": "2021-11-18"
  },
  {
    "title": "Guacamelee! Super Turbo Championship Edition",
    "poster": "https://cdn1.epicgames.com/spt-assets/7041a1ca4ed543459b95d78887047bd9/guacamelee-stce-ztldk.jpg",
    "rating": "80",
    "synopsis": "Guacamelee! is a Metroidvania-style action-platformer set in a magical Mexican-inspired world. The game draws its inspiration from traditional Mexican culture and folklore, and features many interesting and unique characters.",
    "studio": "Drinkbox Studios Inc.",
    "runtime": "2023-06-08",
    "release": "$date",
    "airdate": "2023-06-08"
  },
  {
    "title": "Guacamelee! 2",
    "poster": "https://cdn1.epicgames.com/spt-assets/b05acd7d28d34f6d93a961b0e53ddf0b/guacamelee-2-1lym2.jpg",
    "rating": "84",
    "synopsis": "Head back to the Mexiverse in this long-awaited sequel to the smash hit Guacamelee! Uppercut your way to victory across stunning new hand-crafted levels. Featuring a dense and colorful world, new luchador moves, sassy new bosses, twice the enemies, and 300% more chickens!",
    "studio": "Drinkbox Studios Inc.",
    "runtime": "2023-06-08",
    "release": "$date",
    "airdate": "2023-06-08"
  },
  {
    "title": "PAYDAY 2",
    "poster": "https://cdn1.epicgames.com/offer/d5241c76f178492ea1540fce45616757/mammoth-h1nvv_2560x1440-ac346d6ece5ec356561e112fbddb2dc1",
    "rating": "79",
    "synopsis": "PAYDAY 2 is an action-packed, four-player co-op shooter that once again lets gamers don the masks of the original PAYDAY crew - Dallas, Hoxton, Wolf and Chains - as they descend on Washington DC for an epic crime spree.",
    "studio": "Epic Dev Test Account",
    "runtime": "2023-06-01",
    "release": "$date",
    "airdate": "2023-06-01"
  }
]

You’ll have to somehow exclude the first item. Maybe you could do something like {% for game in state_attr("sensor.epic_games", "data")[1:] %}? Not sure.