[SOLVED] How do I report a variable in the openHAB logs

I’m using OH2 on a Raspberry Pi and using rules to debug my rules.

I’m trying to pass the contents of a variable into the log file but it’s not working as expected:

When I push a ‘test’ switch a rule triggers and records a log entry, absolutely fine. But when I try to add a variable, the response does not include the variable.

logInfo("default.rules", "At Home state is: %1$", At_home.state )

The entry in the log is:

2018-06-22 20:52:32.544 [INFO ] [smarthome.model.script.default.rules] - At Home state is: %1$

I thought the %1$ would cause it to report the variable. Have I got something wrong, or does this imply a configuration error somewhere?

Try:

logInfo("default.rules", "At Home state is: " + At_home.state )

To explain, the logInfo wants to report a single string. Alpoy’s suggestion builds a string from your text fragment and the Item state.

Note that a .state isn’t necessarily a string, but OH will try to convert as well as it can. Sometimes you’ll need to be explicit, myItem.state.toString

1 Like

As logInfo needs <string>, <string>, the correct command would be either

logInfo("default.rules", "At Home state is: " + At_home.state.toString)

or

logInfo("default.rules", "At Home state is: {}", At_home.state)

where {} is substituted with the value At_home.state. Pay attention to the fact, that no .toString is necessary.
It’s even possible to use more than one substitution:

logInfo("default.rules", "1st Item: {}, 2nd Item: {}, 3rd Item: {}", Item1.state, Item2.state, Item3.state)
3 Likes

Worked perfectly, thanks.