[SOLVED] Cli script (python) to determine if today is in array?

Hi everybody,

this is more python related than just Home Assistant, I hope it is okay to post this anyway.

I am trying to write a python script to determine whether or not today is a particular (pre-defined) date, and, if so, print it; this printed value will be used as a sensor, which I want to work further with via Home Assistant automation.

My attempts are below; would somebody please help me finish the script?

first attempt

works, but I don’t like having countless variables

#!/usr/bin/env python3

import time

today = time.strftime("%d-%m")

halloween = "31-10"
test = "14-09"

if (heute == halloween):
     print("Halloween")
elif (heute == test):
    print("it worked!")
else:
  print("nope.")

print(heute)
print(test)

This will output

It worked.
14-09
14-09

I could change the print statements accordingly and already have the sensor I want, however, this would require a variable for each date I want to define and a custom print statement for each case in the loop.
So I tried the following

#!/usr/bin/env python3

import time
import json

today = time.strftime("%d-%m")

mydays = {
  "test": "14-09",
  "halloween": "31-10"
}

for name, date in mydays.items():
# (...)

This is where I cannot continue. I tried some things, but none of those work (don’t have any previous python knowledge).

The goal here is to see if the variable today is equal to any value in the json; if so, print the according key to that value. So if today is equal to value of test, print test; if today is equal to halloween (31-10), print halloween.

Thank you for your ideas :slight_smile:

Not sure if you are set with this being in python.

This is what I use.

And the file of dates…

1 Like

Thank you! I had seen this before and totally forgotten about it… That works perfectly!