Alexa routine - rule in openhab

Hi,

I want to use Alexa routines as it allows me to say “I am leaving”. I don’t want to use Amazon routine mechanism as it is very limited. I want to trigger rule / script in Openhab.

The way I see to implement it is to define Routine switches, e.g.:

Switch Alexa_Routine_0                  "Routine_0"                 (Alexa_Routine) [ "Switchable" ]
Switch Alexa_Routine_1                  "Routine_1"                 (Alexa_Routine) [ "Switchable" ]
Switch Alexa_Routine_2                  "Routine_2"                 (Alexa_Routine) [ "Switchable" ]

Define a rule ( (based on A simple scene management: finally a good use of scripts):

rule "Alexa routine"
when
  Member of Alexa_Routine received command
then
  val script = transform("JS", "routines.js", triggeringItem.name.toString)
  callScript(script +".script")
end

That uses the transform to map routine to script name:

(function(i) {
    var map = {
      "Alexa_Routine_0": "leaving"
    }
    var name = map[i];
    if (name) {
      return name;
    } else {
      return "unknown"
    }
})(input)

And then I define the routine in Alexa app:

Is there a simple way to do it? correct way? other way?

Thx

You approach seems sound.

But since your JS transform just does a mapping, why not just use the Map transform?

You could even drop the map and name the routine Item and script the same. Then you can use:

callScript(triggeringItem.name+".script")

As a bonus the Items could end up with more meaningful names. I strongly recommend using meaningful names where ever possible so you can tell, just by looking at the name of the Item, that this Switch controls your “leaving” routine.

@rlkoshak

Yes, I can use the map transform. Just personal preference to use JS … (Is JS slower then MAP BTW?)

Yes, I can drop and call the script directly, but I didn’t want it this way as I am trying to decouple things, and using PROXY here seems to me more accurate.

The benefit of using patterned names, is that I don’t need to discover items every time I do a change or change Alexa routine.

I can agree with u about meaningful names, but in this case I assume documentation can replace it (assuming u document your code :slight_smile:)

// Leave routine
Switch Alexa_Routine_0                  "Routine_0"                 (Alexa_Routine) [ "Switchable" ]

And as usual, thx for your great feedback!

Just create a switch like you said then make your rule turn on / off the proper stuff when Alexa toggles the switch.

I do this right now. I have i’m Leaving as a routing in Alexa. My Alexa routine toggles my all lights master switch when I tell her I am leaving.

Not sure why you needs anything but a simple rule with some send command statement.

Create a rule for each routine I found the simplest.

lastVoiceCommand

Quite cool repository a awesome work!

Just one question regarding your 01_voicecommand.js:

I havn’t found any rules in your repo from this script is called.
Do you use another way to call this script?

regs Sven