Rule for central voice output via Alexa

Some time ago I created a central routine to be able to execute speech output via my Alexa in a uniform and easy-to-maintain manner. Since I think that other OH users also have a similar requirement and are tired of having to touch various rules again and again when making changes, I’ll post this as an example.

My requirement was to control voice output via different Alexas. It was important to distinguish between what is an information message (only a few selected Alexas) and what is an alarm message (all Alexas and possibly pushovers). The integration of PushOver messages for alarm messages was possible at a later date.

It also turned out that the volume of the Alexa and the stereo system also play a role. One result of this was that you remember the volume of the receiver when it is switched on at the beginning, then turn it down and then return it to the original level when the output is finished. A flat period of time is assumed for this. Setting the volume of the Alexa was commented out again because normally it is correct for the corresponding room and would therefore only be adjusted.

A later change was that the Alexa function “say” was swapped for “announcement”. Announcement has the advantage that you can also see the text itself on the EchoShow display, which is not the case with say. Makes sense if you didn’t immediately understand what was said.

To achieve this there are two switches and the text item :

  • Alexa_Speech_Say → Trigger to activate the rule
  • Alexa_Speech_Output_Alarm → Switch that the alarm routine should also run through.
  • Alexa_SpeechOutput → The text that should be output

To activate the srcipt in a rule, the following lines are necessary where voice output is to take place

  • Alexa_SpeechOutput.postUpdate(“say text”)
  • Alexa_Speech_Output_Alarm.sendCommand(ON) oder OFF
  • Alexa_Speech_Say.sendCommand(ON)

It is important to take into account that the text is set with postUpdate and that the trigger switch is set last.

Trigger definition

The include DSL inline Script

// Voice output via Alexa - if necessary, reduce the volume and reset it again after output

var Timer MyTimerAx = null

// Init Main Volume
  
TMP_SV__Main_Volume.sendCommand(60)

// If someone is present then output via Alexa
if (SW_Present.state == ON)
     {
     if (YamahaReceiverRXS601D_Power.state == ON)
         {
        TMP_SV__Main_Volume.postUpdate(RXS601DMain_Zone_Zone_channels_Volume.state as Number)
         RXS601DMain_Zone_Zone_channels_Volume.sendCommand(50)
        }

// Living rooms generally always output language
// ShowWohnzimmer_LautstRke.sendCommand(50)
  
// changed May 20, 2023 -> In other words, exchanged for announcement. When announced, the text will also be displayed on the show
     EchoShowLivingroom_announcement.sendCommand(Alexa_language_output.state.toString)

// Study only turns on when there is movement or generally when there is an alarm
     if (HmIPSMI000xxxxxxxxxxxx_1_Motion.state == ON || Alexa_Speech_Output_Alarm.state == ON)
         {
// EchoWorkroom_LautstRke.sendCommand(50)
         EchoWorkroom_Speak.sendCommand(Alexa_SpeechOutput.state.toString)
         }

// For alarm messages also in the bedroom and bathroom (unfortunately currently linked to the light on)
     if (Alexa_Speech_Output_Alarm.state == ON)
         {
// Bedrooms and bathrooms -> rooms without motion detectors
// ShowBedroom_LautstRke.sendCommand(50)
      
// changed May 20, 2023 -> In other words, exchanged for announcement. When announced, the text will also be displayed on the show
         EchoShowBedroom_announcement.sendCommand(Alexa_voiceoutput.state.toString)

// EchoBadezimmer_LautstRke.sendCommand(50)
         EchoBadezimmer_Sprich.sendCommand(Alexa_Sprach Output.state.toString)
         }
     }
   else
// If no one is present and it is an alarm message, then push it to your cell phone
     {
	 if (Alexa_Speech_Output_Alarm.state == ON)
         {
         val actions = getActions("pushover", "pushover:pushover-account:xxxxxx")
         actions.sendMessage(Alexa_language.state.toString,"yyyyyyyyy")
         }
     }

// Reset Yamaha to volume - can actually be in the IF branch. It generally works here - no problem
// because the power status is checked.
if (YamahaReceiverRXS601D_Power.state == ON)
     {
     createTimer(now.plusSeconds(6), [|
         RXS601DMain_Zone_Zone_channels_Volume.sendCommand(TMP_SV__Main_Volume.state as Number)
         ])
     }

// Reset Alexa items
Alexa_Speech_Say.sendCommand(OFF)
Alexa_Speech_Output_Alarm.sendCommand(OFF)
Alexa_SpeechOutput.postUpdate("")

Thank you for posting a tutorial!

Especially for tutorials, use code fences for code and logs.

```
code goes here
```

This preserves white space and makes it easier to copy.

That doesn’t appear to be a full rule, just a Script Action. I assume this is Rules DSL?

In the rule, click on the “Code” tab and post the YAML you find there. That will include the rule trigger, rule conditions, and everything else that makes up the rule so the code of the Script Action can be seen in context.

@rlkoshak I agree, this is an better overview.

I’m actual not able to share the the script as YAML because it’s in german. I translate it once for sharing via Google Translate. But I guess the hardcopy of the trigger setting is ok.

There are so many people here which speak german, just post it in German. Google translate everybody can use, anyway the result is sometimes Mikey Mouse English.

Vielen Dank
Solong
B

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