Pyscript - new integration for easy and powerful Python scripting

Nope! I suspect pyscript takes care of that itself. You should be good just calling it normally.

Understood. Something is not right then. See error above.

I am most certain that the timer is running and expiring otherwise this code would not be executed. The issue is that it is giving that error instead of firing the event. Seems like what you provided is close to working as intended but does not.

from datetime import datetime

@service
def schedule_event_at_time(event_name: str, event_data: dict = {}, target_time: str = ""):
    # Parse the target time from the input string
    target_time = datetime.strptime(target_time, "%Y-%m-%d %H:%M:%S")
    
    # Get the current time
    now = datetime.now()
    
    # If the target time is in the past, log an error and return
    if target_time < now:
        log.error(f"Target time {target_time} is in the past. Event not scheduled.")
        return
    
    # Calculate the sleep duration
    sleep_duration = (target_time - now).total_seconds()
    
    # Wait until the target time
    task.sleep(sleep_duration)
    
    # Fire the event
    event.fire(event_name, **event_data)
    log.warning(f"Event '{event_name}' fired at {target_time}")

I spent a bunch of time troubleshooting and writing and then decided to see if GPT could help. Got me here with a few hand edits based on what I learned from the links @Slalamander supplied.

This works great but…for some unknown reason the service call gets made but does not return until the timer completes. This is problematic as I am trying this to voice so:

Me: “Set a timer for 30 seconds”
Thirty seconds pass
Assist: “Timer set for 30 seconds”

Shouldn’t this be running the service in the background?

EDIT Got this going with a bit of help in HA Discord:

from datetime import datetime

def timerwait(duration, event_name, event_data, target_time):
    task.sleep (duration)

    # Fire the event
    event.fire(event_name, **event_data)
    log.warning(f"Event '{event_name}' fired at {target_time}")    
    
@service
def schedule_event_at_time(event_name: str, event_data: dict = {}, target_time: str = ""):

    # Parse the target time from the input string
    target_time = datetime.strptime(target_time, "%Y-%m-%d %H:%M:%S")
    
    # Get the current time
    now = datetime.now()
    
    # Calculate the sleep duration
    sleep_duration = (target_time - now).total_seconds()
    
    # Wait until the target time
    task.create(timerwait, sleep_duration, event_name, event_data, target_time)
1 Like

Ah, thanks for sharing! I wasn’t aware pyscript had its own asyncio wrapper.

Does that mean the timers work now? :smiley:

Yes! Finally have it working and the beginnings of a very big missing piece for voice. I know that there is a timer implementation for voice but it is limited to only ESPHome satellites whereas this is much more universal.

Thank you for your continued help and patience.

1 Like

No problem! It helps me learn too, and you’ve always been clear in your questions, as well as polite and very appreciative in your answers. On multiple occasions I’ve found myself wanting to give a short answer, only to fall into a rabbit hole because I want to give one that is actually complete :joy:

1 Like