How do I use a variable (which is the name of my item) to send it a postUpdate?

  • Platform information: Raspberry PI4 B+
    • Hardware: ARM V7 + SD 32G0

    • OS:Busterstrong text

    • Java Runtime Environment: openjdk version “11.0.6” 2020-01-14
      OpenJDK Runtime Environment (build 11.0.6+10-post-Raspbian-1deb10u1)
      OpenJDK Server VM (build 11.0.6+10-post-Raspbian-1deb10u1, mixed mode)

    • openHAB version: 2.5.4

I have a map file which allows me to match the JSON received from an external box with my items in OH2

Itemtobox.map

0=Item1
1=item2 …

when receiving the JSON I extract the values:

“kakusensors”: [
{
“id”: 0,
“status”: “ON”,
},
{
“id”: 1,
“status”: “OFF”,
}
]
I recover the name of the switch and its status
status returns me “ON”
map → id = 0 returns me “item1”

and I match the status of OH2 with that received from box

item1.sendCommand(cmde)

All this is perfectly functional !!!
my problem is to get the status of my item before sendingCommand

I can not do :

if ( contact.state.toString !==“ON” ) { instruction … }

How can i do this ???
Thank you

sendCommand(the_item_name_as_string, the_value_as_string)

Thank you for your answer but …

my problem is not the sendCommand which works perfectly!
It is the reading of the state: "item1.state.toString"

You could use core.utils.send_command_if_different.

I must have made myself difficult to understand …

I’m not trying to send a command or change a state … but to read the state of a switch the same way I send the command: I use a variable string which is the name of the String such as:
items:

String String1 “[% s]”

sitemap:

sitemap test label = “Test”
{
Text item = String1
}

variable named “Item_Name” which has the value “String1” (the name of the String)
and my order =>

Item_Name.sendcommand (“ON”) … this works!

But my problem is that I want to read the state of the switch and I tried the same way:
variable named “Item_Name” which has the value “String1” (the name of the String)
and my order which is refused =>

if (Nom_Item.state.toString == “Test”) {…}

Thank you for your help

From your example, it looked like you were comparing the state of the Item to make sure it was different than the new state that you want to change it to. My previous response covers that. If you just want to get an Item from its name to check its state, then use the post @rossko57 linked to.

(@rossko57, what’s your name… other than rossko57?)

Real name is Ross but I have been Rossko on the interwebs since 1990-something

2 Likes

Nice to meet you, Ross!

For those who are interested :

I found a very simple solution without going through python:
1 - Import:

import org.eclipse.smarthome.model.script.ScriptServiceUtil

2 - Create a ‘GenericItem’ linking the item not the chain containing its name:

val GenericItem Virtual_Item = ScriptServiceUtil.getItemRegistry?.getItem (Item_name) as GenericItem

3- And then we can treat the Virtual_Item as the named item:

Var Status = Virtual_item.state.toString

Arpagor

That’s almost like Scott’s tutorial “Get Item from string name” for DSL rules.

1 Like