This took me entirely too long (IMO) - BUT, I’m also a n00b at programming and only 2 months into my journey with HA. I’m coming off of SmartThings as it wasn’t cutting the mustard with what I wanted to do, and a good friend of mine turned me on to HA. I started this project about 4 days ago and have struggled but mostly because I didn’t understand the syntax; of which, between searching Google, searching here and using multiple AI assistants (ChatGPT, Gemini and Grok) I was FINALLY able to get this going. HUGE shout out to all those that contribute!!
Backstory: We live out in the country - and any decent grocery store is literally 30 miles away. Most of it is highway, but I try not to go into “town” more than once or twice a week if I don’t have to. That being said, my wife and I have had an issue with forgetting about food we buy and it winds up expiring before we get to eat it, or we eat part of it before it expires (but not all of it.) I wanted a simple and easy-to-use way to track what foods will expire when - have that feed into an HA calendar (called “Food Expiration”) that my wife could check on, and figure out what would be an ideal meal that she can prepare using those foods up before they expire. (Ideally reducing waste - not just food, but money!)
Of course, I have to make it “user friendly” for my wife (and son) as I don’t want them to have to think like a computer to make this work. We’ve had Alexa devices since their inception (we had a Tap, if anyone remembers that) but I liked the idea of having a specific voice assistant in the kitchen (that’s not Alexa, as I’m trying to work my way off of them.) I bought one of the $20 M5stack Echo devices and figured out how to get that into HA after a bunch of reading, then started my quest on how to create this automation.
The code is simple, but getting there was a giant pain; the first was getting the inputs from the voice into the automation as variables. Once I figured that out, the big issue is figuring out the date formatting since I want to use natural languge; i.e. “shredded chicken expires on August 20th, 2025.” That poses several problems; mainly that calendar creation events want the date in the format 2025-08-20 - obviously not a match. That sent me down several rabbit holes, especially considering that no matter how I said the date (i.e. August 20, 2025) the voice assistant always added the suffix onto the day (i.e. August 20th, 2025.) That complicates things of course as you have to remove that suffix before doing anything else. Even worse, if you remove “st” from “1st” - guess what it also removes? The st from August so you get Augu instead. Easy fix at least (you’ll see in the code.)
After that, it was just figuring out how to format that date properly using the strftime() and strptime() functions. To make it “one day” you do have to add a timedelta(days=1) so that it shows up as a single day event (otherwise it errors out saying it needs to be at least one second duration from start to end.) Keep in mind “days” start at midnight.
Sorry for the long post, but I’m hoping that others who are struggling with HA realize that many things are possible and with some determination and searching, you CAN figure out how to do this. I mean c’mon, I’m just some dummy who lives out in the country (/sarcasm.) Seriosuly though, I did all this (along with many other things) without posting - the knowledge is here and many awesome people have contributed. Use a combination of searches here, Google (or your preferred search engine) along with some AI - and you’ll be shocked at what you’re capable of. (I know I was!) You’ll learn along the way and be able to do some amazing things.
So without further adieu; here’s the automation code:
alias: Food Expiration
description: ""
triggers:
- trigger: conversation
command:
- "{event_title} expires on {end_date}"
conditions: []
actions:
- action: calendar.create_event
metadata: {}
data:
summary: "{{ trigger.slots.event_title }}"
start_date: >-
{{ (strptime((trigger.slots.end_date | replace('1st', '1') |
replace('nd', '') | replace('rd', '') | replace('th', '')), '%B %d
%Y')).strftime('%Y-%m-%d') }}
end_date: >-
{{ (strptime((trigger.slots.end_date | replace('1st', '1') |
replace('nd', '') | replace('rd', '') | replace('th', '')), '%B %d %Y')
+ timedelta(days=1)).strftime('%Y-%m-%d') }}
target:
entity_id: calendar.food_expiration
mode: single
Now all my wife has to do as she unloads groceries is to say “hey (wake word) - (food item) expires on (whatever date she reads off the food item.)” I don’t need a fancy speaker for this, as all it responds with is “done.” This creates a single-day entry in the “Food Expiration” calendar and she can look at the week and plan out what she wants to make for breakfast/lunch/dinner.