Get Amazon Alexa to read a random line of a text file

Another post targeted at beginning users (like me)…

Wouldn’t it be great to have a rule to make Alexa to give us a random thought for the day out of our own list of quotations?

If you already have the capability to launch a daily briefing using OH, read on. Otherwise, you first should start with the Amazon Echo Control Binding documentation.

First we need to install a debian linux application. On the command line of your openhab/openhabian installation, enter:

sudo apt install randomize-lines

Go to the /etc/openhab2/scripts folder and create a sample file called quotes.txt

Add several lines of random text like:

This is the first line
This is the second line
This is the third line
This is the fourth line

You can test the randomize program by entering this on the command line:

rl -c 1 /etc/openhab2/scripts/quotes.txt

Create another file in the scripts folder called randomline.sh and enter the following:

#!/bin/bash
rl -c 1 /etc/openhab2/scripts/quotes.txt

Save, and change the file permissions so randomline.sh is an executable file:

sudo chmod +x /etc/openhab2/randomline.sh

Your Echo device should have a speak item for it. Writing a rule to have Alexa speak a random line will require you to identify the speak item for your device. For illustration, we will assume it is called EchoPlus_Speak and we will further assume the quote will be spoken when the kitchen computer (computer_kitchen) is turned on:

rule "Alexa Quote"
when    
    Item computer_kitchen changed from OFF to ON
then
    var Random_Line = executeCommandLine("/etc/openhab2/scripts/randomline.sh", 1000)
    EchoPlus_Speak.sendCommand("thought for the day...." + Random_Line)
end

That’s it!

Note that Alexa seems to speak without interruption, even when there are commas and periods for punctuation. The trick is to add periods, because the Alexa routine pauses for a fraction of a second for each period found. So when you populate the quotes.txt file, instead of:

Seek first to understand, then to be understood

write the quote out as:

Seek first to understand..  then to be understood
5 Likes

Continuing the discussion from Get Amazon Alexa to read a random line of a text file:

Hi, thanks for sharing. I tried something similar through a rule with little success. One last problem I couldn’t solve seems to be the correct typ of the variable I am using for the TTS-command. Does anyone ha an idea?

What I am trying:

Defining up to 10 different texts, on motion detection cerate a random number, let Alexa say the predefiend text.

Thats is what is happening:

Alexa says “Alexa text [1…9.]” instead of “Text [1…9]” as predefined.

Any idea? Thank you!

var TextToSpeak String
val AlexaLevel = 30
val AlexaText1 = 'Text 1'
val AlexaText2 = 'Text 2'
val AlexaText3 = 'Text 3'
val AlexaText4 = 'Text 4'
val AlexaText5 = 'Text 5'
val AlexaText6 = 'Text 6'
val AlexaText7 = 'Text 7'
val AlexaText8 = 'Text 8'
val AlexaText9 = 'Text 9'

rule "Zufall Sprachausgabe"
when Item MotionSensor1 changed to ON
then
    logWarn("Tradfri Motion Sensor", "Bewegung erkannt")
		RandomChoice = 0+ (Math::random * 10.0).intValue
	logWarn("Random", "Random Number is "+RandomChoice)
	    sendCommand(ZigbeeLight1, ON)
		
	Echo_Living_Room_Volume.sendCommand(AlexaLevel)
	var TextToSpeak = ("AlexaText" + RandomChoice.toString())
	Echo_Living_Room_TTS.sendCommand(TextToSpeak)
	
end

Hi agian, still curious about my problem shown above.

Trying your first approach: After not being able to install “randomize-lines”, I tried “shuffle” instead. This is doing the trick as well:

shuf -n 1 /etc/openhab2/scripts/textfile.txt