Voice rule with MULTIPLE IF

Hi - i’m trying to find out why this rule is not working:
Basically is a voice rule where i want to do three different actions for three different voice commands.
If i use a single condition, works - with two or more… no way. I can’t understand where i’m wrong.

rule “Accendi la luce dello studio”
when
Item VoiceCommand received command
then
var String = null
var String command = VoiceCommand.state.toString.toLowerCase
logInfo(“Voice.Rec”,“VoiceCommand received”+command)

if (command.contains("Accendi la luce dello studio")) 
    	{sendCommand(Luce_studio, ON)}

            else

            	if (command.contains("Spegni la luce dello studio")) 
    	{sendCommand(Luce_studio, OFF)}

            else

                    if (command.contains("Accendi la luce del corridoio")) 
    	{sendCommand(interruttori_corridoio, ON)}

end

You seem to convert the whole string to lowercase, but all contains statement start with uppercase. Could that be the problem?

If not, please post your logfile entries too.

In addition, you may want to change sendCommand(Luce_studio, OFF) into

Luce_studio.sendCommand(OFF)

(of course, similar for all other actions) probably not your issue here but in most cases, the latter is better than the former
https://docs.openhab.org/configuration/rules-dsl.html#sendcommand-method-vs-action

I’m currently having troubles executing my Voicecommand rules, after upgrade to OH2.3.0 Build#1232.

rule "Voice control"

when

    Item VoiceCommand received command

then
	var String command = VoiceCommand.state.toString.toLowerCase
    logInfo("Voice.Rec","VoiceCommand received "+command)

if 
(command.contains("tv"))
sendCommand(Loxone_AllMediaONOFF, "ON")

end

log:

2018-03-20 20:16:26.998 [ome.event.ItemCommandEvent] - Item 'VoiceCommand' received command tv

2018-03-20 20:16:31.413 [INFO ] [pse.smarthome.model.script.Voice.Rec] - VoiceCommand received null

Received command TV, but log returns NULL?

I may be totally wrong here, but I am reading quite a few posts where a rule gets triggered faster than the state of an item gets updated (apparently connected to the concept that OH2 is based on an event bus); which, if true and applicable to your case, may mean that your item state has not been updated yet when your rule triggers. An easy way to find out is to add sleep statement:

then
    Thread::sleep(50)
	var String command = VoiceCommand.state.toString.toLowerCase

this would add a 50ms second pause to allow your system to catch up…
just a thought…(and no guarantee that I am even on the right track)

It could be that the state has not changed yet. It takes a few ms.

rule "Voice control"

when
   Item VoiceCommand received command
then
    var String command = receivedCommand.toString.toLowerCase
    logInfo("Voice.Rec","VoiceCommand received " + command)
    if (command.contains("tv")) {
        Loxone_AllMediaONOFF.sendCommand(ON)
    }
end

@lipp_markus you are in the right track but sleeping for 50ms is a bit hacky.
Use implicit variables if you can. In this case receivedCommand is perfect.

1 Like

Alternatively, choose the correct rule trigger for what you want to do

when
   Item VoiceCommand changed
then

Thanks guys! With your help, I’ve now got my voice command rule working again :smiley: