Print the state of switch

Hi, i am new to openhab, so maybe this answer will be stupid for someone, but … ,
i’ve got one Switch and i want to print state of this Switch (ON/OFF).

My sitemaps:

Switch item=A mappings=[ON=“ON”, OFF=“OFF”]
Text item=StateA label=“State A”

and items:

Switch A “A SWITCH” {exec=">[ON:/home/pi/openHAB/program turn b1 ON] >[OFF:/home/pi/openHAB/program turn b1 OFF]"}

String StateA “state is [%s]” {exec=">[/home/pi/openHAB/program getState b1:1000:REGEX((.*?))]"}

i try too:
String StateA “state is [%s]” {exec=">[./home/pi/openHAB/program getState b1:1000:REGEX((.*?))]"}

String StateA “state is [%s]” {exec=">[/home/pi/openHAB/program getState b1:1000]"}

String StateA “state is [%s]” {exec=">[/home/pi/openHAB/program getState b1:1000:]"}

but String doesn’t work. Any advice how to do it?

To display the switch you should try in your sitemap

Text item=StateA label="State A [%s]"


don’t work

Care to elaborate on what you are actually seeing?

@Dibbler42 has presented the correct way to get the value of the Switch as part of the label.

I just see minus sign ( - ).

Is this exec correct?
{exec=">[/home/pi/openHAB/program getState b1:1000:REGEX((.*?))]"}

I try too
{exec=">[./home/pi/openHAB/program getState b1:1000:REGEX((.*?))]"}

I’ve got program which return string ON or OFF.

The minus shows that the switch (in openhab) is in an undefined state. But i dont know if the exec works correct.


How can be in undefined state? I was think that can be just in ON/OFF state

All items in openHAB defaults to an undefined state after startup, and remains in this state until they are set to a specific state (e.g. ON or OFF for a switch).

Please note that if you are using persistence with restoreOnStartup the items will be set to the last (persisted) state during startup, and thus should ideally* never have an undefined state while the system is running.

*) Note that this is not strictly true due to the way that openHAB loads files at startup.

OK, i understand.

Now forget about it, new question.

How can i just print in openhab return text from some program.

For example:
in terminal i write: ./program getState b1
and it print me: ON

Now i want do that in openhab.
Is that possible?


If we assume for now that your Item (e.g. String StateA) have a proper state that you want to display then you have two options (as far as I can see):

  1. Display it in a sitemap

This is done like @dibbler42 explained above:

Text item=StateA label="State A [%s]"

  1. Print it to a log file

Then you may do something like this:

logInfo("", "", StateA.state.toString)

and it should end up in your openhab.log (/var/log/openhab/openhab.log if you are on Linux).

All of this is assuming you already have a proper state. Your problem, on the other hand, may be that you have not been able to set a proper state from the Exec binding.

I have only briefly used the Exec binding myself, so I am far from an expert, but I think your binding should be an in-binding (i.e. you need to use “<” instead of “>”). You may also need to do the “@@” trick for the spaces in your command line.

Try the following and see if it works:

String StateA "state is [%s]" {exec="<[/home/pi/openHAB/program@@getState@@b1:1000:REGEX((.*?))]"}

1 Like


while you wrote, i try this in items file and it works

String StateA {exec="<[/home/pi/openHAB/program getState b1:1000:REGEX((.*?))]"}

So, thanx again for your advice, i hope that will fine.