Help with an automation getting info from an api

Hi, i’m trying to make an automation and i think its beyond my capabilitties :frowning:

The goal is get the xbox game cover currently playing in my dasboard. The xbox and xbox live sensors show when i’m on a game and them the name of the game. With the name i get the id with the api of a web site, and with the id i want to get the cover from the same api and show it in the dasboard.

By now i get the id in a sensor but passing the id to the next step is not working. i have this in my configuration.yaml.

rest_command:
  trigger_idjuego_service:
    url: "https://api.mobygames.com/v1/games?api_key=APIKEY&title={{ states('sensor.d13g0_m0nt3s_status') }}&platform=289&format=brief"
    method: get
    headers:
      accept: 'application/json; charset=UTF-8'
      content_type: 'application/json; charset=utf-8'
    content_type: 'application/x-www-form-urlencoded'

  trigger_caratula_service:
    url: "https://api.mobygames.com/v1/games/{{ states('sensor.idjuego') }}/platforms/289/covers"
    method: get
    headers:
      accept: 'application/json; charset=UTF-8'
      content_type: 'application/json; charset=utf-8'
    content_type: 'application/x-www-form-urlencoded'

The fist rest_command works well, i have the id in a sensor, but not the second one. My automation at this moment is: (by now only trying to get the url in an input text)

alias: Caratula Juego Xbox
description: Muestra la caratula del juego de xbox que se esté jugando en este momento.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.d13g0_m0nt3s_in_game
    from: "off"
    to: "on"
condition: []
action:
  - service: rest_command.trigger_idjuego_service
    data: {}
    response_variable: idjuego
  - service: input_text.set_value
    target:
      entity_id: input_text.idjuego
    data:
      value: "{{idjuego['content']['games'][0].game_id}}"
  - service: rest_command.trigger_caratula_service
    data: {}
    response_variable: caratula
  - service: input_text.set_value
    target:
      entity_id: input_text.caratula
    data:
      value: "{{caratula['content']['cover_groups'][0].covers[0].image}}"  

sorry if i’m doing all wrong but i’m learning every day. Any help will be appreciated.

Post an example of the data contained in the response variable named caratula.

The sensor shows “unknown”. if I call the service in developers tools with the second rest_command loads the error page of the website. I believe that the template do not add the id to the api url in the second call. But the sensor shows the id correctly. And I test the template and it’s correct, I get the url of the cover.

The first REST command, the one you said that works, has api_key=.

The second REST command, the one you said that doesn’t work, doesn’t have api_key=. Shouldn’t it also include it in order to access api.mobygames.com?

Not needed according the documentation. If I try the url replacing the sensor with the number directly in a browser works. I suppose because I,m been authorized with the first one

When I click that link (the one that used to be displayed in your post), this is the response:

 {"code":401,"error":"Authorization required","message":"You must have an API key to use this resource."}

As an experiment, add apikey to the second URL in the same way it’s used as in the first URL.


EDIT

This seems to be more of an API access problem (composing the correct URL) than a Home Assistant problem. I’m unfamiliar with mobygames API but, for many APIs, it’s often required to include your API key in each individual request.

2 Likes

You are eight i need to add the authorization key too. Still same problem, “unknow”…

if a replace the second rest_command with a valid id (178788) in developers tools works ok but still “unknow” in the input_text and sensor…

  trigger_caratula_service:
    url: "https://api.mobygames.com/v1/games/178788/platforms/289/covers?api_key=AUTHKEY"
    method: get
    headers:
      accept: 'application/json; charset=UTF-8'
      content_type: 'application/json; charset=utf-8'
    content_type: 'application/x-www-form-urlencoded'
content:
  cover_groups:
    - comments: download release
      countries:
        - Worldwide
      covers:
        - comments: null
          description: null
          height: 800
          image: >-
            https://cdn.mobygames.com/covers/11255636-vampire-survivors-windows-apps-front-cover.jpg
          scan_of: Front Cover
          thumbnail_image: https://cdn.mobygames.com/04737a92-ac17-11ed-b950-02420a00012d.webp
          width: 533
status: 200

i have to problems to solve: replacing the 178788 with the value of the first sensor do not work and the unknow of the second input_text.

I keep trying. thanks for your help :slight_smile:

What is the data that is returned by the second URL? Specifically, what is the length of the string that is returned?

I’m asking because any entity’s state value (like input_text and sensor) is limited to storing 255 characters.

The url returns:

content:
  cover_groups:
    - comments: download release
      countries:
        - Worldwide
      covers:
        - comments: null
          description: null
          height: 800
          image: >-
            https://cdn.mobygames.com/covers/11255636-vampire-survivors-windows-apps-front-cover.jpg
          scan_of: Front Cover
          thumbnail_image: https://cdn.mobygames.com/04737a92-ac17-11ed-b950-02420a00012d.webp
          width: 533
status: 200

And the template should be the url of the cover,

https://cdn.mobygames.com/covers/11255636-vampire-survivors-windows-apps-front-cover.jpg

I replace the sensor.idjuego in the api url with the input_text.idjuego and in developers tools the RESTful Command works ok now. Still “unknow” in the automation. Do not know why with the sensor do not work and works with the input_text. both are the same value…

I suspected as much.

If you use the first URL with an API key in the browser, the second URL works in the browser without an API key probably because it’s still the same connection session that was established by the first URL. That’s not how REST commands work. Each one is a separate session.

Post the URL you are using that fails to work with the sensor.

In addition, copy-paste the following template into the Template Editor and post the result.

>>>{{ states('sensor.idjuego') }}<<<

Ideally, the result should look like this:

>>>178788<<<

Last question, is sensor.idjuego a Template Sensor?

The url that fails is

https://api.mobygames.com/v1/games/{{states(‘sensor.idjuego’)}}/platforms/289/covers?api_key=APIKEY

i post the api key feel free to use and i can get a different one later.

the result is: >>>state: “178788”<<<

the “state:” part should not be there?
EDIT: my mistake, in the sensor template i had: “sensor: {{ states(‘input_text.idjuego’) }}”

First part fixed now, the RESTful Command: trigger_caratula_service in developers tools works but keep receiving “unknow” in the sensor on the automation .

EDIT2: i remove the template from the las restful command and load in the input_text the contente of the variable: Caratula and shows:

{‘content’: {‘code’: 429, ‘error’: ‘Too many requests’, ‘message’: “Oy, you’re making requests too frequently for us to handle. Please wait at least 1.0 seconds between requests.”}, ‘status’: 429}

i suppose that HA is calling the api too fast. I need to make a delay between the two calls, right?

I asked that you test this:

>>>{{ states('sensor.idjuego') }}<<<

and you reported you tested this:

sensor: {{ states('input_text.idjuego') }}

That’s the Input Text, not the Sensor. I need to see the value of sensor.idjuego. Why? Because if the sensor’s value is not 178788 then it will fail to work correctly when used by the REST command’s template.

Also, if sensor.idjuego is a Template Sensor, please post its configuration.

It’s called as fast as you have designed your automation to call it.

input_text.idjuego and sensor.idjuego are working right, both displaying the id correctly.

The problem is fixed adding a delay of 5 seconds between request. I got the url in the sensor.caratula finally!!!

I need to polish the automation because the xbox live sensors are not very reliable but the base is working now

thanks a lot for your time and your help. :slight_smile:

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. Adding the apikey was a stepping stone to resolving the problem.

1 Like