[SOLVED] Get Text and Number variable from IFTTT data

Tags: #<Tag:0x00007f617fd80670> #<Tag:0x00007f617fd803a0> #<Tag:0x00007f61803efe98>

Hi all
Having a bit of fun with Google Home talking to OpenHAB with the latest play using IFTTT to parse VoiceCommands to change channels on the Samsung TV.

The IFTTT applet


sends data as {{TextField}} {{NumberField}} so I need to figure out how to get both bits into OH.

Even with DEBUG set on the log only shows

[INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘VoiceCommand’ received command channel 90
[INFO ] [marthome.event.ItemStateChangedEvent] - VoiceCommand changed from ---- to channel 90
[INFO ] [smarthome.model.script.voice control] - VoiceCommand received channel 90

So I compared this to what HabPanel sends from a slider:
[smarthome.event.ItemCommandEvent ] - Item ‘TV_Channel_Num’ received command 90
[INFO ] [marthome.event.ItemStateChangedEvent] - TV_Channel_Num changed from 20 to 90

My limited knowledge of rules gets me as far as

import import smarthome.model.script.voice
.
.
else if (cmd == "SET" || cmd == "CHANGE") {
        // TV or Windows
        if (command.contains("channel")) {
                val Number num = (TV_Channel_Num.state as number).smarthome.model.script.voice.control.get({{NumberField}})
                sendCommand(TV_Channel_Num.state, num)
        }
    }

which I know is not correct but I have no real knowledge of where I am trying to get {{NumberField}} from.
I have set smarthome.model.script.voice to DEBUG but get no DEBUG information.

The browser inspector for IFTTT shows
{{TextField}} {{NumberField}}

Maybe my import line is not quite right. Any pointers? Thanks in anticipation

I’m going to suggest you take a step back and work with more basic Rules and configurations first before you tackle this problem because there is almost nothing about your Rule that makes sense and I don’t think you will be able to be successful at this until you understand Rules better in general as well as how Items work.

I have no experience with Voice Control but I see the following issues:

  • There appears to be a VoiceCommand Item, which is probably a String Item which receives the already translated text

  • You have import listed twice, which could be a typo, but there is no smarthome.model.script.voice package to import. Package names get truncated in the logs so the full package name would be org.eclipse.smarthome.model.script.voice.package. But that doesn’t really matter because you should not import anything from the org.eclipse package anyway. Everything you should have access to is already imported.

  • You skip the important parts of the Rule. You should be using commands to the VoiceCommand Item as the trigger to this Rule.

rule "Process a voice command"
when
    Item VoiceCommand received command
then
  • You need to parse out your command from VoiceCommand. Assuming it is always at least a single word
    val commandParts = VoiceCommand.state.toString.split(" ")
    val cmd = commandParts.get(0)
  • You are skipping a whole lot of important code here that decides what to do based on the value of cmd. What is cmd? What is command? Clearly you have copied someone’s working code but you lack the understanding of some OH basics to understand how it works. That isn’t surprising, OH is complicated and takes a good deal of work and experience to understand. Regardless, since you are only sending “channel 90”, this else if statement cannot work because you do not have cmd available in the command text you are sending.

  • All class names start with a capital letter. So it would be (TV_Channel_Num.state as Number)

  • That doesn’t really matter much because that whole line is nonsensical. There is no .smarthome.model.script.voice.control.get method on a DecimalType object. “{{NumberField}}” is an IFTTT concept and has no meaning inside an OH Rule. You can’t just “say” that a String is a Number, you have to parse it.

Because you don’t. That is an IFTTT feature that lets you format the Text that gets sent to OH as a command to the VoiceCommand Item.

Because there is no such package name. And even if you had gotten that DEBUG to work, it would not have shown you anything useful.

This is immaterial to OH. That helps format the text that gets sent to OH. OH has no such concepts.

It isn’t, as mentioned, but it isn’t the source of your struggles.

I applaud your willingness to just jump right in but your lack of understanding of how OH works is greatly impeading your progress. You can use this as a test case to help you learn some more though I would recommend starting with some easier problems.

Be sure to review the Beginner’s Tutorial and the Concepts and Rules sections of the User’s Manuals.

Then review the code you found that does voice control and make sure you actually understand how it works. Then you will be successful.

As always, we are here to help with any road blocks you encounter along the way.

Hi Rich
Thanks for your response but you sure have a harsh way of conveying yr message.

I have String VoiceCommand

It was a mistake to have not posted the whole rule. Here it is now with (excessive logging for troubleshooting and) the mod’s I have made following your reply.

rule “Voice control”

when
Item VoiceCommand received command
then
// https://github.com/openhab/openhab/wiki/Controlling-openHAB-with-your-voice
var String command = VoiceCommand.state.toString.toLowerCase
logInfo(“voice control”,“VoiceCommand received " + command)
val commandParts = VoiceCommand.state.toString.split(” ")
val cmdChN = commandParts.get(2)
logInfo(“voice control”,"VoiceCommand sending " + cmdChN)

var String cmd = null
if (command.contains("on")) {
    cmd = "ON"
} else if (command.contains("off")) {
    cmd = "OFF"
} else if (command.contains("open")) {
    cmd = "OPEN"
} else if (command.contains("close") || command.contains("shut")) {
    cmd = "CLOSE"
} else if (command.contains("set") || command.contains("change")) {
    cmd = "SET"
  logInfo("voice control","VoiceCommand eval'd to " + cmd)
}

if (cmd == "ON" || cmd == "OFF") {
    // main floor
    if (command.contains("west wall") || command.contains("living room")) {
        sendCommand(Light_GF_West_Wall_Lights, cmd)
    }

    if (command.contains("dining room") || command.contains("main floor")) {
        sendCommand(Light_GF_Dining_Table, cmd)
        Lights_GF_Dining_Table.postUpdate(cmd)
    }
} else if (cmd == "OPEN" || cmd == "CLOSE") {
    // laundry
    if (command.contains("laundry window")) {
        if (cmd == "OPEN") {
            sendCommand(MtrWinLaundr, ON)
        } else if (cmd == "CLOSE") {
            sendCommand(MtrWinLaundr, OFF)
        }
    }
} else if (cmd == "SET") {
    // TV or Windows
    if (command.contains("channel")) {
            logInfo("voice control","VoiceCommand changing " + cmdChN)
            sendCommand(TV_Channel_Num, cmdChN)
            postUpdate(TV_Channel_Num, cmdChN)
    }
}

end

This was the key bit of info I needed:

Thanks. I thought it better to have a good go at it than just say “how do I do this?”.
It is now working. Thanks for your help and advice.