Global function with state and timers

Hello!

I’m trying to understand how i could snooze phone notifications, specifically for scenarios like “washing machine cycle completed”, something similar to what you find in alarm or calendar apps.

What I’d like to create is a reusable notification system with snooze functionality that I can apply across different notification types (washing machine, dishwasher, security alerts, etc.) without having to implement snooze handling logic for each one separately.

Ideally, I’m looking for:

  1. A way to extend notificationBuilder to add snooze buttons automatically
  2. A centralized handler for snooze requests that can reschedule notifications
  3. Something that’s reusable across different notification scenarios

I’m comfortable with JavaScript which i guess is the only option anyway. I’m thinking along the lines of a helper module or function that could be imported into my rules.

Has anyone implemented something similar or have suggestions on the best approach? Any code examples or patterns would be greatly appreciated. I am not interested in something that specifically solves my problem but advice on how this is normally done, i am installing 4.x from scratch and i’d like to create reusable functions rather than using a “single rule file approach” i had in the past with 3.x

Thanks in advance!

You didn’t mention what language but I think notificationBuilder only exists for JS so I’ll assume that.

JS allows monkey patching so you can add your own functions to notificationBuilder with automatically add stuff. Create a library that adds your new stuff to it and import it and call it.

Buttons usually operate on commands to Items so you can probably just put all the items into a Group to trigger a rule using a member of trigger.

A library can be imported and use from any rule. If you need to share days between rules use the cache.shared.

There are instructions at the bottom of the docs for the JS Scripting add-on.

Note JS is far from the only option.

Thank you! I am familiar with JS, Python and Kotlin so language is not an issue, i was just trying to understand the proper pattern to create a centralized handler, thanks to your hing to cache.shared i found the post below which is basically doing exactly what i was loooking for, so a shared timer resource / scheduler.

Thanks!