Working on some code. Using notify to send an asynchronous message from Rhasspy. Used the example on the site to speak the time every number of seconds. This uses threading.Timer. When trying to test a one time event and send a notify message, it does not work. Code below from the example with the threading.Timers commented out and simplified code does not work. Any ideas???
This first version below with a straight function call: The print statement works in the function but nothing from Rhasspy on the notify.
“”“Example app to tell you the time every minute with a notification.”“”
import logging
import threading
from datetime import datetime
from time import sleep
from rhasspyhermes_app import HermesApp
_LOGGER = logging.getLogger(“TimeNotificationApp”)
SECONDS = 15
SITE_ID = “satellite”
def tell_time():
“”“Tell the time with a notification.”“”
now = datetime.now().strftime(“%H %M”)
print(“now is:”,now)
app.notify(f"It’s {now}", SITE_ID)
#threading.Timer(SECONDS, tell_time).start()
app = HermesApp(“TimeNotificationApp”)
#threading.Timer(SECONDS, tell_time).start()
tell_time()
app.run()
If you enable the threading timer at the end of the function, the print statement works the first time the function is called but, notify does not. Subsequent calls each 15 seconds, the print and the notify works. A simple call of a function with a Rhasspy notify within the function does not seem to work for me.
Hello
It seems like you’re encountering issues with the notify function not working as expected in your Rhasspy Hermes app when called directly versus when called through a threading.Timer. Let’s address this step by step.
Rhasspy uses Hermes protocol for communication, and notifications should be sent through app.notify(message, site_id). Here’s how you can troubleshoot and potentially fix the issue:
import logging
from datetime import datetime
import threading
from rhasspyhermes_app import HermesApp
_LOGGER = logging.getLogger(“TimeNotificationApp”)
SECONDS = 15
SITE_ID = “satellite”
def tell_time():
now = datetime.now().strftime(“%H %M”)
print(“Current time:”, now)
app.notify(f"It’s {now}", SITE_ID)
def start_timer():
tell_time()
threading.Timer(SECONDS, start_timer).start()
if name == “main”:
app = HermesApp(“TimeNotificationApp”)
start_timer()
app.run()
• Functionality: This script notifies the current time every 15 seconds using Rhasspy’s HermesApp for communication.
• Key Functions:
o tell_time(): Formats the current time and sends a notification using app.notify().
o start_timer(): Initiates the timer loop by calling tell_time() initially and then scheduling itself to run every 15 seconds.
• Execution: The main block initializes the Hermes app (app), starts the timer with start_timer(), and runs the app using app.run() to handle asynchronous operations effectively.
This structure ensures notifications work reliably, including on the first call, within the Hermes app’s context. Adjust SECONDS and SITE_ID as needed for your specific use case.
Hope it helps !
Thank you
boblewis