Is there any way to regularly shuffle todo entries? For example once a day. Or manually with a script. I didn’t find any solution via YAML. I went for a python script, but ended at the situation that this python script didn’t find any todo entries.
# python_scripts/shuffle_todo_list.py
def main():
try:
todo_list = hass.states.get("todo.mylist")
if not todo_list:
raise ValueError("❌ Entität 'todo.mylist' existiert nicht")
items = todo_list.attributes.get("items") or todo_list.attributes.get("tasks") or []
logger.debug(f"Debug: Gefundene Attribute -> {todo_list.attributes.keys()}")
logger.info(f"Anzahl aller Einträge: {len(items)}")
if not items: # Check if the list is empty
logger.warning("ℹ️ Keine Einträge in der To-Do-Liste")
return
active_items = []
for idx, item in enumerate(items, 1):
if not isinstance(item, dict):
logger.error(f"⚠️ Ungültiger Eintrag (Index {idx}): {item}")
continue
status = item.get("status", "needs_action").lower().strip()
if status in ["needs_action", "active"]:
active_items.append((
item.get("uid", f"missing_uid_{idx}"),
item.get("summary", f"Unbenannter Eintrag {idx}")
))
if not active_items:
logger.warning("ℹ️ Keine aktiven Einträge (Status: 'needs_action' oder 'active')")
return
shuffled = sorted(
active_items,
key=lambda x: hash(f"{x[0]}_{int(hass.time.timestamp())}")
)
removed_uids = []
try:
for uid, summary in active_items:
hass.services.call(
"todo",
"remove_item",
{"entity_id": "todo.mylist", "item": uid},
blocking=True
)
removed_uids.append(uid)
for _, summary in shuffled:
hass.services.call(
"todo",
"add_item",
{"entity_id": "todo.mylist", "item": summary},
blocking=True
)
logger.info(f"✅ Erfolgreich {len(active_items)} Einträge gemischt")
except Exception as e:
logger.error(f"🔥 Kritischer Fehler: {str(e)}")
logger.info("🔄 Versuche Rollback...")
for uid in removed_uids:
try: # Add a try-except block for the rollback
hass.services.call(
"todo",
"add_item",
{"entity_id": "todo.mylist", "item": next(s for u, s in active_items if u == uid)},
blocking=True
)
except Exception as rollback_e:
logger.error(f"🔥 Rollback Fehler für UID {uid}: {rollback_e}")
raise # Re-raise the original exception after rollback attempt
except Exception as e:
logger.error(f"💥 Schwerwiegender Fehler: {str(e)}")
raise
main()
Any easier way to get this to work?