Couple tips to share:
[1] I’m not sure if this has been discussed before, but I discovered that if a echo dot is offline (unplugged or dropped wifi), and a rule sets the volume, the entire .rule file will crash. Ugh!
I haven’t investigated further, but I suspect this issue would also affect any Echo Control command that is R/W (read/write). Fortunately it does not affect TTS because it is W only.
I use the volume command a lot and I want to prevent anything that could break/crash a rule file. My solution is to wrap the volume command with a test of the volume state. Like this:
//Near the top of your rule file:
val EchoVolumeKitchen = 60 // Volume Level, should be in steps of 10.
//Somewhere in your rule file:
if(Echo_Kitchen_Volume.state!=UNDEF && Echo_Kitchen_Volume.state!=NULL) {
Echo_Kitchen_Volume.sendCommand(EchoVolumeKitchen)
}
A special rule to trap the device’s offline state needs to be created too, like this:
rule "Kitchen Echo Dot Online Status"
when
Thing "amazonechocontrol:echo:accountTom:echoKitchen" changed
then
var StatusEchoKitchen = ThingAction.getThingStatusInfo("amazonechocontrol:echo:accountTom:echoKitchen").getStatus()
logInfo("AlexaTTS.rules" , 'Attention: Kitchen Echo Dot Status = ' + StatusEchoKitchen)
if(StatusEchoKitchen.toString() == 'OFFLINE') {
Echo_Kitchen_Volume.postUpdate(NULL)
}
end
[2] A useful volume control trick for saving and restoring the echo device’s default local volume is to use OpenHab’s storeStates() and restoreStates() functions. These are very clever functions that do all the work with little effort on our part. For full details see:
See https://docs.openhab.org/addons/actions.html#event-bus-actions
For example, before I change the volume (for a TTS announcement or PlayAlarmSound), I store the echo item’s current volume like this (two echo dots are shown in this example):
var VolumeStates = storeStates(Echo_Kitchen_Volume,Echo_Theater_Volume)
Then using a timer that executes after the TTS and/or alarm sound is finished, I do this:
restoreStates(VolumeStates)
When the volume states are restored, not only are their individual values refreshed with the saved values, the function also sends them (using the .items volume command) to all the affected echo devices for you. BTW, if an echo device is offline, and use the offline rule shown in Tip #1, these functions will gracefully ignore the offline device. Very cool!