Best way to insert a list into Home Assistant?

I have a list of knock knock jokes… a few hundred for now, and I want one to be picked at random as part of my son’s bedtime routine. I Could input them all into the template in the automation, but that feels wrong… I guess if it works, it works, but I’d prefer if the list lived somewhere else as a list or dictionary or whatever, then just pull that sensor |list|random in the bedtime script to keep things cleaner.

the thing is, I don’t really know how to get this list into home assistant as a dictionary or list or whatever? any good tips on doing this? or just a 6-halfdozen situation where it has to live somewhere and if I’m only calling it in this script I might as well just have the whole list in the script to randomly pull as a variable or something at the top?

If you’re only using it the one script, just define it as a variable:

variables:
  jokes:
    - this is joke 1
    - this is joke 2
    - this is joke 3
    - this is joke 4
sequence:
  - action: notify.example
    data:
      message: "{{ jokes | random }}"

If you really want it to be more globally accessible, you could store the list as an attribute in a template sensor:

template:
  - sensor:
      - name: All My Jokes
        state: this is not important
        attributes:
          jokes:
            - this is joke 1
            - this is joke 2
            - this is joke 3
            - this is joke 4

Then the script:

sequence:
  - action: notify.example
    data:
      message: "{{ state_attr('sensor.all_my_jokes', 'jokes') | random }}"

There is another option, which is to store the list as a variable defined in a .jinja file in config/custom_templates as is done for template macros.

1 Like

Along the line that Drew draws, my color template macro could easily be adapted to do this. Most of the code is there.

1 Like

feels like magic code if I read that :slight_smile:

btw, for topic starter, isnt it easier to store all your jokes in a JSON and pull from there a random one? Keeps your script clean and updating jokes would be easy seperated

There is someone with a website of Dad jokes that I use. I have the feedparser custom integration running and so this: Home-Assistant-Config/sensor2/QOTD_snr.yaml at 219b36f22b21f0b2acbb4ad68e6eaafdf04187e0 · SirGoodenough/Home-Assistant-Config · GitHub. Then when I call a joke I use this afterwards to pull a new one


      - action: homeassistant.update_entity
        target:
          entity_id: sensor.joke
        data: {}

This gets me a fresh one every time without polling the site for nothing.

But the template with a list of jokes is like what I started with.
My house tells me a joke whenever someone walks up to the door.

1 Like

most funny house on the block ? :wink:

It’s Dad jokes, so not that funny…

thanks for the tips! I Was using dad jokes integration on hacs to pull from web automatically, but my 6y/o told me he likes knock knock jokes the best and I wasn’t able to limit to only knock-knocks, so I pivoted to the local list.

the evening briefing is LLM based, so originally I had it make up a relevant joke based on weather or calendar or whatever else it brought up in the briefing that night. That was awesome and I really liked the results, but with my child having a fairly steady schedule it ended up repeating jokes very quickly and I don’t know enough with the generate ai task if it’s possible to keep a running list or memory or something to minimize repeats. So i pivoted to feeding it a joke from the dadjoke integration and now pivoted again to the local list of knockers.