Trying to display a value in sitemap derived from a rule

I know this is probably something silly I am looking over but I have a value (lightsensor1) that I got after it ran through a rule and I cannot get it to show up on my sitemap.

rule "Converting LDR value to %"
		Item sensornode1light received update
		var Number ls1 = sensornode1light.state as DecimalType 
		var int ls1p = (ls1 / 1000).intValue

If I just simply do the following nothing is displayed other than the item name lightsensor1

Text item=lightsensor1 icon="light"

Ultimately I would like the sitemap to display something like if I were to set it up as an item as follows:

Number	lightsensor1 "Light sensor 1 [%.1f %%]"

But I tried this which just creates an error

2017-06-04 11:18:49.046 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Converting LDR value to %’: An error occured during the script execution: The name ‘lightsensor1’ cannot be resolved to an item or type.

I have read several examples for several hours between yesterday and today and have not figured it out obviously. So I figured it is something that everyone knows and does not have any issues with or something not commonly done.

That suggests that you have not set up such an Item? You cannot postUpdate to items that do not exist.

The last thing I noted was I did try setting it up as an item, in the .items configuration, and that is when I got the [ERROR] message that it could not be resolved as and item or type. If I remove that from the .items configuration then I get no errors but still no data displaying in the .sitemap. Is there another way to add it as an item that will not give me an error?

Hi Thomas,

You should definitely define the “lightsensor1” as an item (in an .items file or via PaperUI) before you can use it in a rule and/or display it on a sitemap.

Can you show that part of the .items file where you defined your item?

Yes. This section deals with the sensors.

Number	sensornode1light "Light sensor 1 [%.0f Ohm]"{mqtt="<[broker:bruh/sensornode1:state:JSONPATH(ldr):.*]"} 
Number	sensornode1humidity "Humidity sensor 1 [%.2f %%]" {mqtt="<[broker:bruh/sensornode1:state:JSONPATH(humidity):.*]"} 
Number	sensornode1temp "Temperature sensor 1 [%.1f °F]" {mqtt="<[broker:bruh/sensornode1:state:JSONPATH(temperature):.*]"} 
Number	sensornode2light "Light sensor 2" {mqtt="<[broker:bruh/sensornode2:state:JSONPATH(ldr):.*]"} 
Number	sensornode2humidity "Humidity sensor 2" {mqtt="<[broker:bruh/sensornode2:state:JSONPATH(humidity):.*]"} 
Number	sensornode2temp "Temperature sensor 2" {mqtt="<[broker:bruh/sensornode2:state:JSONPATH(temperature):.*]"} 
Number	lightsensor1 "Light sensor 1 [%.1f %%]" 

I am just not sure if I have to add something to it since the value it is being set to derives from what it gets in the rule.

Try to change it to

var int ls1p = ((ls1 / 1000) as DecimalType).intValue

Tried it and I get a different error:
2017-06-04 13:37:29.242 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Converting LDR value to %’: org.eclipse.smarthome.core.library.types.DecimalType

I use the Eclipse SmartHome Designer and it says that “Cannot cast from BigDecimal to DecimalType”

I have looked at other examples and so far nothing stands out that in structure is all that different,

There’s a clue. If Item lightsensor1 were unknown to the system it should throw the error you see. The implication is that you already have an item, or maybe even a variable, of the same name defined somewhere.

Have you restated OH amongst all the tinkering? People get caught out by OH2 tendency to apparently cache item definitions etc.

Haven’t given up yet but restarting did not help either. Just tried with and without it being part of the .items configuration.

Really appreciate all the input from everyone so far though. Thanks so much. I am still looking at other examples to see if I see anything contrary to what I have.

Divide to conquer, simplify

your.items file

Number	lightsensorxx "Light sensor X [%s]"
your.rules file

rule "Converting LDR value to %"
		Item sensornode1light received update

On that I do get the output of 123 like you would expect. I also do know for a fact the sensornode1light is getting an actual value and not an error or 0. So perhaps it is in the formula?

Is there a way to output the values of ls1 and ls1p in either the events.log or OpenHAB.log just to make sure each step is correct?

rule "Converting LDR value to %"
		Item sensornode1light received update
		logInfo("testA", "raw input " + sensornode1light.state.toString)
		var Number ls1 = sensornode1light.state as DecimalType 
		logInfo("testB", "captured input " + ls1.toString)
		var int ls1p = (ls1 / 1000).intValue
		logInfo("testC", "tweaked input " + ls1p)
//		postUpdate(lightsensor1,ls1p)	
		lightsensorxx.postUpdate(ls1p)	// in case of funny Item
		lightsensor1.postUpdate(ls1p)	// use preferred method

ok we are getting somewhere. Test A and B do get the actual values but C gets a 0. So it appears the issue is in the tweaked calculation.

On the output then in the sitemap lightsensorxx (which has no defined item) is blank and lightsensor1 (which is defined) displays 0, since that is the tweaked value I would assume.

For the non defined item I got the usual error:
2017-06-04 16:05:34.220 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Converting LDR value to %’: An error occured during the script execution: The name ‘lightsensorxx’ cannot be resolved to an item or type.

Okay, didn’t realise you had now removed the Item definition for the test itemxxx. Bear in the mind the rule would break and stop on the missing Item, so you have not yet tried the postUpdate to your ‘real’ Item.

What is the value of ‘lsl’ before you divide by a thousand? If it is less than 1000 the int result will be zero.

1 Like

I tried it both with item.xx and no item.xx. With was zero. And yes I need to use some other than an integer. Been way to many years since I have done this. ls1 would always be less than 1,000 to give me a rough percentage. That’s what I get for copying an example.

So basically it was something simple by just dropping the integer declarations to get the proper tweaked value. which I assume this means I just need to display lightsensor1’s State value as a percentage.

2017-06-04 16:47:26.778 [INFO ] [eclipse.smarthome.model.script.testE] - final output lightsensor1 (Type=NumberItem, State=0.39500000, Label=Light sensor 1, Category=light)