Reputation: 61
So I'm making python voice control for my smart lights. But when I say 'crimson' it triggers 'on' and 'crimson' at the same time because the word 'crimson' contains 'on' at the end. I really don't want to change the name to something else.
elif ('light' in query) or ('lights' in query): # Home automation
if 'on' in query:
control_lights("light on")
speak("okay, turning on light")
if 'off' in query:
control_lights("light off")
speak("okay, turning off light")
if 'crimson' in query:
control_lights("light crimson")
speak("okay, turning light to crimson")
Upvotes: 3
Views: 83
Reputation: 2670
You can change the order of your if statements and use some elif statements so that once the word crimson
is identified, the code will ignore the word on
:
if 'crimson' in query:
control_lights("light crimson")
speak("okay, turning light to crimson")
elif 'on' in query:
control_lights("light on")
speak("okay, turning on light")
if 'off' in query:
control_lights("light off")
speak("okay, turning off light")
Btw I think that your project is super cool :) I kind of want to do the same now lol
Upvotes: 1
Reputation: 1003
I am assuming your commands are being translated to text before execution, in that case the first if
is satisfied because 'on' is indeed in 'crimson'. So changing 'on' into some different word might fix your problem. Alternatively you can just switch the order of your if
statments so that if you say 'crimson' it will be recognized before 'on'.
Secondly, I would change the second and third if
s to elif
to avoid multiple entries to conditions in general, so once a command has been chosen, no other command will be chosen as well.
Upvotes: 0
Reputation: 37
Can you create a switch case so that it only executes 1 rather than cycling through all of your IFs?
If your voice recognition recognizes "light crimson" as "light on" and triggers that case in the switch statement, you have no choice but to change either "light on" or "light crimson" as commands, since "light crimson" = "light on" as far as the recognition can tell.
Upvotes: 0