I thought this one was pretty fun so I thought I would share. My kids school lunch menu is provided by Nutrislice which I discovered has an exposed API you can access. I had been displaying it as raw data which was basically just a dictionary of items. It was boring, but it served the purpose. My kids could tap on the tablet on the wall and bring up the menu for tomorrow.
I’ve been playing around with generative AI and the idea came to me to make this a little bit more fun for the kids. I’m passing the lunch menu into AI and having it turn the menu into something as read to you as a character/person with what I think have unique talking patters. So far, I’m using Gordon Ramsay, Optimus Prime, a pirate, and Donald Trump. The results are pretty hilarious, at least in my opinion. It’s Friday as I type this, so there is no lunch tomorrow, but the results are still worth sharing to give you the jist.
Optimus Prime
Donald Trump
A Pirate
Gordon Ramsay
Without getting into a detailed step by step, this is how I did it.
Create a rest sensor to get the data.
- platform: rest
name: lunch tomorrow
scan_interval: 28800
unique_id: lunchtomorrow
resource_template: https://city.api.nutrislice.com/menu/api/digest/school/school_name/menu-type/lunch/date/{{(now().date() + timedelta(days=1)).strftime('%Y/%m/%d')}}?format=json
value_template: >
{{ value_json.menu_items }}
json_attributes:
- menu_items
Create a template with a trigger and action. The trigger being any time the menu changes, the action is to push the menu into Google’s generative AI integration. The prompt being an injection of the menu items with an array of characters, one of which is selected randomly, and then have it formatted to look good in a markdown card.
The fully formatted after the AI is pretty long and an entity can only hold 100 characters, so I created another template sensor to put the AI response into an attribute as attributes can be basically unlimited in size.
trigger:
- trigger: state
entity_id:
- sensor.lunch_tomorrow
- input_button.change_munu
action:
- action: google_generative_ai_conversation.generate_content
response_variable: response
data:
prompt: >-
The following is a list of menu items for lunch tomorrow
{{ state_attr('sensor.lunch_tomorrow', 'menu_items') }}.
You are {{ ["Donald Trump", "Optimus Prime", "Gordon Ramsay", "a pirate"] | random }}.
Tell me the menu in a funny way.
Don't use more than 200 words.
Format it to look nice on a Home Assistant Markdown Card
sensor:
- name: Funny Lunch Text
unique_id: funnyllunchtext
state: None
attributes:
funny_text: "{{ response.text }}"