Multiple calls of timer in scrips

I want to write a blockly script that notifies me when a window contact is open for a specific time.
The script is to be called up via several rules for different windows. For this I pass the item of the contact to the script as a parameter. Unfortunately, the first call of “cancel timer” seems to end all script timers or the timerName is overwritten by the last call.

Is there a solution to the problem?

here ist what I did:

The issue is, that even e.g. you have two instances of the timer running you only have one instance of the item & contact name variable and once a new timer is starting your variables value will change.

The only way I know to accommodate this issue is to pass additional arguments to the timer.
I have resolved it by creating a new timer block that will keep the original event context (link). However this is only working if the blocky code is directly called from a rule and not as a script by another rule. maybe you need some further adjustments if you want to use it within a script triggered by a rule

Indeed, timerName gets overwritten every time this script gets called. So when the timer goes off, it will cancel the last timer that was created, not necessarily the current timer.

But it makes no sense to cancel a timer inside itself. The fact that the code is running means that the timer has already terminated. There’s nothing to cancel. So get rid of that cancel timer and I think that should fix things.

Alternatively, you could use Open Reminder.

  1. You can install this rule template from Settings → Automation → Rule Templates.
  2. Change your script to just include the “send notification” stuff as all the timer stuff will be handled in the rule template.
  3. Create a Group and add all your door sensors to that Group
  4. Instantiate a rule based on the template with the following configurations.
  • dndEnd: “”
  • defaultTimeout: PT1m
  • invert: false
  • groupTriggers: <select Group created in step 3>
  • alertRuleUID: <select the script with the notification code>
  • dndStart: “”
  • repeatPeriod: “”
  • timeoutMetadata: rem_time
  • alertState: CLOSED
  • reschedule: false

That should duplicate the behavior you are currently after. Your notification script will be called when a door/window is left open for one minute. But you will notice you have more options.

For example, you can specify a “do not disturb” time where alerts will be suppressed during those times. For example, if a need for an alert occurs between 23:00 and 08:00 (overnight), the alert will be suppressed until 08:00 (if the door is still open).

You can specify the amount of time for a door/window to be open in metadata so that each one has it’s own timeout instead of having one timeout for them all.

You can define a repeat period that will recall the alert rule repeatedly every X amount of time until it’s finally closed.

But the most important benefit is you get all of this and the only code you have to write is to send the notifications. The rest if handled by the rule template. That’s what rule templates are for. To free you from having to code common use cases like this yourself. This one template can be used in lots of ways really, see How I Use Rule Templates

oh wow, that looks pretty powerful, thanks a lot! I have to take a closer look at this.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.