Using Alexa NextAlarm Channel Minus X Minutes To Trigger a Routine

I’ve installed a local instance of openHabian 3.1.0 and installed and configured the Alexa binding. I can see the channel for the “NextAlarm” for my alexa device and the date/time.

Now the tricky part. I want to be able to use the next alarm value less, for example 10 minutes, as a trigger for an Alexa routine that would kick off before my alarm actually goes off.

I’ve set it up like this so far:

triggers:

  • id: “1”
    configuration:
    cronExpression: “*0 * * ? * *”
    type: timer.GenericCronTrigger
    conditions:
  • inputs: {}
    id: “3”
    configuration:
    type: application/vnd.openhab.dsl.rule
    script: EchoDotG1_NextAlarm.minusMinutes(10)==LocalTime_Date
    type: script.ScriptCondition
    actions:
  • inputs: {}
    id: “2”
    configuration:
    itemName: EchoDotLivingroom_StartaRoutine
    command: Open the living room blinds
    type: core.ItemCommandAction

The way I’m trying to do this is to say, constantly check, every second, if the next alarm set on a specific Alexa device, less 10 minutes, is equal to the current time. If it is, kick off a routine I normally use by asking the Alexa in the living room to “Open the living room blinds” (this routine is configured in Alexa and works on its own).

I know this isn’t a very efficient way to do this, checking every second, and maybe that’s the issue, but I’m at a loss on how to configure this. I’ve seen some code examples out there, but they all appear to be at least two years old, and the syntax of rule code appears to have changed.

Any assistance is greatly appreciated.

Hi!

I think you could do that with a timer.

I can’t give you an exact code example right now but here’s the outline in pseudo code:

Trigger: Item NextAlarm changed
Then:

  • check if old timer is running and cancel it
  • check if NextAlarm is more than 10 minutes in the future (to handle errors)
  • set new timer with end time NextAlarm.minusMinutes(10) and specify your action

That should do, and you’re getting rid of the cron job…

I greatly appreciate the reply, but I’m at a loss to understand how to set a timer on Alexa as you describe. I keep looking for examples of code, since I’m assuming this can’t easily be done in the rule GUI. However, everything I see appears to be from 2019 or earlier and the code format doesn’t appear the same now.

I see how to set the trigger of “When EchoDotG1_NextAlarm changed”, but how to do I set a timer? I see there’s a channel of “EchoDotG1_NextTimer”, but I’m unfamiliar with the commands that can be given to see the value, change the value, etc. after adding that channel as an Item.

Sounds like you are in “my first rule” territory?
You’ve chosen something to begin with that is a bit more challenging than “turn on the lights when dark”, treat it as a learning experience.

It’s basically like any number of ‘alarm clock’ type actions. There are a great number of examples - but as you note, the older ones won’t deal with GUI entered rules. The principles will be right, but not the exact syntax if you seek copy-paste solutions.

As you’ve decided to use GUI and you are likely to need advanced timer functions, you might choose to write your rule script in javascript - a popular choise which offers more timer capability in the GUI context than the common DSL rules do.

A key bit of information is that a rule can set up an independent timer for a given arbitrary time.
So you might create a rule that runs when the Alexa Item state changes.
There are functions to examine that Items state.
There are functions to do maths on that - subtract 10 minutes.
There are functions to do time comparisons - is that new time in the future?
Then we can create that independent timer with the new target.
When timer runs, it does whatever it was you wanted - commanded some other light Item ON or whatever.

In real life you’ll need some if-but refinements -
Is a timer already running? Cancel that before making a new one.

You’ll find some useful pieces here

I think an important distinction to make here is what we mean by “timer”. Is the timer BobMiles listed in pseudo code a timer on Alexa, or is it a timer declared via code that “lives” on the OpenHab instance? I assumed he was referring to a timer on Alexa that would count down until my event, triggering the routine, which also existing in Alexa.

Hi,

no I was referring to a timer in openhab. But I have no experience of rule coding in the main UI, I only do it via file based rules so I can’t help you with your code. But there are plenty of timer questions and examples around and @rossko57 gave good advice and I’m sure you’ll figure it out :slight_smile:

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