Alexa Voice Command Room Awareness via OH

Below is a simplified version of the rule using the relevant Alexa-linked activity item state as the device state, removing the need of parsing the voice command for all the different permutations especially for non-English users that had to translate the regular expression.

import org.eclipse.smarthome.model.script.ScriptServiceUtil

rule "Alexa Voice Command Room Awareness"
when
  Member of gAlexaVoiceCommand received update
then
  // Exit if voice command is "alexa"
  if (triggeringItem.state == "alexa") return;
  // Determine room location based on triggering item name
  val location = triggeringItem.name.split("Echo").get(0)
  // Determine device category and voice command based on triggering item state regex matching
  val category = transform("REGEX", ".*\\b(fan)\\b.*", triggeringItem.state.toString)
  // Exit if category is null
  if (category === null) return;
  // Determine device name based on category-location mapping
  val deviceName = transform("MAP", "alexa.map", category.toUpperCase + "-" + location)
  // Determine device state based activity item category related state
  val deviceState = ScriptServiceUtil.getItemRegistry.getItem("AlexaActivity" + category.capitalize).state.toString
  // Send command to device if name and state defined, otherwise generate text to speech error message
  if (deviceState == "ON" || deviceState == "OFF")
    if (deviceName != "")
      sendCommand(deviceName, deviceState)
    else
      sendCommand(triggeringItem.name.replace("LastVoiceCommand", "TextToSpeech"),
        "There is no " + category + " to control in this room")
end
1 Like