[SOLVED] How to get the state of an item whose name is stored in a String

  • Platform information:
    • Hardware: RPI 3B+
    • OS: Raspbian Stretch Lite
    • openHAB version: 2.5.M4

I am trying to use snips with openhab which has turned out pretty well. The following rule works for intents- Turn On and Turn Off but I am really confused over how to make it work for Toggle.

rule "Snips Voice Command"
when
    Item SnipsIntent received update
then
    var String entity = transform("JSONPATH", "$.slots..value.value", SnipsIntent.state.toString);
    var String onlyIntent = transform("JSONPATH", "$.intent.intentName", SnipsIntent.state.toString);
    if (onlyIntent == 'TurnOn') {
           sendCommand(entity,'ON')
                }
    else if (onlyIntent == 'TurnOff') {
           sendCommand(entity,'OFF')
                }
    else if (onlyIntent == 'Toggle') {
           if(entity.state == 'OFF')
                  sendCommand(entity,'ON')
           else
                  sendCommand(entity,'OFF')
                }
    var String sessionId = "{ \"sessionId\":\""+ transform("JSONPATH", "$.sessionId", SnipsIntent.state.toString) + "\" }"
    val actions = getActions("mqtt","mqtt:broker:snipsBrokerThingId")
    actions.publishMQTT("hermes/dialogueManager/endSession",sessionId)
end

I dont know how to implement this part.

    else if (onlyIntent == 'Toggle') {
           if(entity.state == 'OFF')
                  sendCommand(entity,'ON')
           else
                  sendCommand(entity,'OFF')
                }

Here, if(entity.state == ‘OFF’) gives following error which is obvious.

'state' is not a member of 'java.lang.String'

How to find the state of the entity here??

I’m no familiar with Snips, but for the rules DSL, you can get an Item from a string using this…

Also, when comparing the states, you will want to use OnOffType.OFF rather than a string, and you were missing some curly braces…

if (item_derived_from_entity.state == OFF) {
    sendCommand(entity, 'ON')
} else {
    sendCommand(entity, 'OFF')
}

Hi !
Thanks a lot. It works.
:smile:

Great! Could you post your final rule in case it might be useful for others?

Sure. I am trying to add some more lines. I’ll post the final result in a while.

Here it is.

```
import org.eclipse.smarthome.model.script.ScriptServiceUtil
rule "Snips Voice Command"
when
    Item SnipsIntent received update
then
    var String entity = transform("JSONPATH", "$.slots..value.value", SnipsIntent.state.toString);
    var String onlyIntent = transform("JSONPATH", "$.intent.intentName", SnipsIntent.state.toString);
    if (onlyIntent == 'TurnOn') {
           sendCommand(entity,'ON')
                }
    else if (onlyIntent == 'TurnOff') {
           sendCommand(entity,'OFF')
                }
    else if (onlyIntent == 'Toggle') {
           val testItem = ScriptServiceUtil.getItemRegistry.getItem(entity)
           if(testItem.state == OnOffType.OFF)
                  sendCommand(testItem,'ON')
           else
                  sendCommand(testItem,'OFF')
                }
    var String sessionId = "{ \"sessionId\":\""+ transform("JSONPATH", "$.sessionId", SnipsIntent.state.toString) + "\" }"
    val actions = getActions("mqtt","mqtt:broker:snipsBrokerThingId")
    actions.publishMQTT("hermes/dialogueManager/endSession",sessionId)
end
```