Daily random pokemon

I am playing around with a sensor that gets a random pokemon from the PokeAPI.
The url is: https://pokeapi.co/api/v2/pokemon/ but requires either a name or an id at the end
eg https://pokeapi.co/api/v2/pokemon/84

My idea is to have it load daily, and use this url with a random number for the id:
https://pokeapi.co/api/v2/pokemon/{random_number}

I currently have two issues.

  1. can’t think of the best way to generate a random number
    • Was going to use the random integration but I cannot seem to set the frequency it generates a number.
  2. how to insert it into the url above

Any help would be appreciated

Not knowing the API, it there a numeric limit on the number ? Like the random-number is between 1 and 5000 …

The random is the easy part, what info do you want from the JSON?

OK. Here is a random pokemon extrator that grabs a pokemon every hour with all the data

##
## pokemon
## 
- platform: rest
  name: Random Pokemon
  scan_interval: 3600
  resource_template: https://pokeapi.co/api/v2/pokemon/{{ range(1, 1000) | random }}
  value_template: "{{ now() }}"
  json_attributes:
    - abilities
    - base_experience
    - forms
    - game_indicies
    - height
    - held_items
    - id
    - is_default
    - location_area_encounters
    - moves
    - name
    - order
    - past_types
    - species
    - sprites
    - stats
    - types
    - weight

The range is set from 1 to 1000, I have no idea what the range should be

2 Likes

This is amazing. I’m going to try to make this into a sensor for my Pokémon obsessed husbands dashboard!

Keep this going. My grandbabies would like something like this. Just FYI, I was playing earlier with getting a really cool image …

I found this:

https://unpkg.com/[email protected]/sprites/pokemon/other/dream-world/60.svg

Turns out that the number is the same ID as that sensor, so I can grab that image now.
Just need to put it together in some card.

1 Like

Yes please! My husbands gonna flip!

I’m having trouble getting the rest sensor to work though right now, so I’m not quite there yet, but how would you create a sensor to pull the picture?

I am stuck at that. I did this before with Pandora music and custom code I wrote to get album art. HA camera used to be easy in YAML, now it SUCKS trying to get it to work. I may have to do some automation that changes URL of a web browser.

Normally, I would do this:

- platform: generic
  name: Pokemon Image
  still_image_url: "https://unpkg.com/[email protected]/sprites/pokemon/other/dream-world/{{ state_attr('sensor.random_pokemon','id')}}.svg"

But the HA powers to be thought it is right to take away YAML setup of cameras.

And I try to set that up in (non) YAML and I get “Image is not in the right format” even though I can easily surf to it and get it

1 Like

I don’t think the camera integration can use SVG images, there would be no reason for them to have SVG support.

What about the normal picture card?

Agreed. But it needs to be templates. Not static. So like template card wrapping it.

True…

But what if the file is downloaded to the HA server using

and always with the same file name.
That way the card is static but the download can be dynamic

Never tried it. What would worry me would be static name and caching. But worth a try.

This is the way to go. I will set up some pretty custom card … GitHub - iantrich/config-template-card: 📝 Templatable Lovelace Configurations

I’m still having trouble just getting the template sensor to work, I set up the rest sensor in my configuration but because of the json attributes, I’m having trouble figuring out how to pull the right data (like the type) without it being in a list, especially when there are multiple types. I pretty much copied what you had in your screenshot above, so how can I just get it to pull some of the info without the whole list?

There are multiple ways. Can you post what exact information you want? I like to use ‘jq’ to process JSON and re-create smaller JSON, but there are other ways too.

And using custom:config-template-card, a short example would be this:

type: 'custom:config-template-card'
entities:
    - sensor.random_pokemon
card:
    type: picture
    image: >-
       ${'https://unpkg.com/[email protected]/sprites/pokemon/other/dream-world/' + states['sensor.random_pokemon'].attributes.id + '.svg'}


So it uses the ID of the random pokemon in the sensor and grabs the SVG and displays it:

1 Like

I’m not 100% sure if this is what you were asking, but I’d want the info like the name and Id, which I got as they seem to be end points. Then there’s info like the type, which can have multiple, so when I try to pull it into a template, something like this:


types:
  - slot: 1
    type:
      name: grass
      url: https://pokeapi.co/api/v2/type/12/
  - slot: 2
    type:
      name: flying
      url: https://pokeapi.co/api/v2/type/3/

Since there are multiple types, I’d want to just get like ‘grass’ or ‘flying’ without the url or the slot number.

For example, in a markdown card I have something like this:


type: markdown
content: |2
    {{ state_attr("sensor.random_pokemon", "name") }}: #{{ state_attr("sensor.random_pokemon", "id") }}

  
  type: {{ state_attr("sensor.random_pokemon", "types") }}
title: Random pokemon

How would I get the types to just be the info?

If you just want a string of the type names, you would do like this in template:

{% for tp in state_attr("sensor.random_pokemon","types") -%}
  {{ tp.type.name }}
{% endfor %}

So in state_attr you can grab the target attribute, use for to walk through lists, grabbing what you want.

1 Like

Sweet! Thank you! That’s gonna be really helpful. Now I just have to make it pretty

Hopefully last question, but im not getting the image to display. I just see a box with a question mark. I copied your code exactly, any idea why it wouldn’t be showing for me?