Jython, QuantityType, unexplained behaviour of a rule

Hello,

I have this Jython rule, it usually works pretty good. But recently I have registered a switching of my dehumidifier, which I cannot explain.
The excerpt from the log file is shown below. Both code sections for switching on and off are basically identical, but once the logfile contains “52.0” and the second time “52 %”.

How can this behaviour be explained? Thanks for your help!


maxHumidity = u"63 %"
minHumidity = u"55 %"

@rule("Entfeuchter schalten")
@when("Item LaCrosseTemperatureSensor1_Luftfeuchtigkeit changed") 
def Entfeuchter_schalten(event):
	LOG = logging.getLogger("{}.Jython_Andi".format(LOG_PREFIX))

	sensor1Hum = ir.getItem("LaCrosseTemperatureSensor1_Luftfeuchtigkeit").state

	if sensor1Hum > QuantityType(maxHumidity):
		LOG.info("Entfeuchter muss eingeschaltet werden {} > {}".format(str(sensor1Hum), str(maxHumidity)))
		if(ir.getItem("ZWaveNode2_Switch_greenWAVE").state != ON):
			events.sendCommand("ZWaveNode2_Switch_greenWAVE", "ON")
			send_info_telegram("Entfeuchter ein bei {}".format(str(sensor1Hum)))

	elif sensor1Hum < QuantityType(minHumidity):  
		LOG.info("Entfeuchter muss ausgeschaltet werden {} < {}".format(str(sensor1Hum), str(minHumidity)))
		if(ir.getItem("ZWaveNode2_Switch_greenWAVE").state != OFF):
			events.sendCommand("ZWaveNode2_Switch_greenWAVE", "OFF")
			send_info_telegram("Entfeuchter aus bei {}".format(str(sensor1Hum)))

itemsfile:

Number:Dimensionless	LaCrosseTemperatureSensor1_Luftfeuchtigkeit	"Luftfeuchte Kellerraum groß [%.1f %% RH]" <humidity>  (gLaCrosseHum) {channel="jeelink:lacrosse:18:humidity"}

openhab.log

2020-04-04 19:43:44.357 [INFO ] [jsr223.jython.Jython_Andi ] - Entfeuchter muss eingeschaltet werden 52.0 > 63 %
2020-04-04 19:43:44.359 [INFO ] [jsr223.jython.Jython_Andi ] - Entfeuchter ein bei 52.0
2020-04-04 19:44:15.013 [INFO ] [jsr223.jython.Jython_Andi ] - Entfeuchter muss ausgeschaltet werden 52 % < 55 %
2020-04-04 19:44:15.015 [INFO ] [jsr223.jython.Jython_Andi ] - Entfeuchter aus bei 52 %

I would use:

sensor1Hum = items["LaCrosseTemperatureSensor1_Luftfeuchtigkeit"].floatValue()
LOG.info("Entfeuchter muss eingeschaltet werden {} % > {}".format(str(sensor1Hum), str(maxHumidity)))

thanks for your answer, but unfortunately it doesn’t work. for me the question is anyway, why here 2 equal expressions

sensor1Hum > QuantityType(maxHumidity)

and

sensor1Hum < QuantityType(minHumidity)

deliver different results.

here my tests:

# only for testing

maxHumidity = u"45 %"
minHumidity = u"25 %"

sensor1Hum = items["LaCrosseTemperatureSensor1_Luftfeuchtigkeit"].floatValue()

if sensor1Hum > QuantityType(maxHumidity):
	LOG.info("Entfeuchter muss eingeschaltet werden {} > {}".format(str(sensor1Hum), str(maxHumidity)))
	if(ir.getItem("ZWaveNode2_Switch_greenWAVE").state != ON):
		#events.sendCommand("ZWaveNode2_Switch_greenWAVE", "ON")
		send_info_telegram("Entfeuchter ein bei {}".format(str(sensor1Hum)))

elif sensor1Hum < QuantityType(minHumidity):  
	LOG.info("Entfeuchter muss ausgeschaltet werden {} < {}".format(str(sensor1Hum), str(minHumidity)))
	if(ir.getItem("ZWaveNode2_Switch_greenWAVE").state != OFF):
		#events.sendCommand("ZWaveNode2_Switch_greenWAVE", "OFF")
		send_info_telegram("Entfeuchter aus bei {}".format(str(sensor1Hum)))

logfile:

2020-04-05 19:15:01.680 [INFO ] [jsr223.jython.TEST_andi ] - Entfeuchter muss ausgeschaltet werden 52.0 < 25 %

I suspect that is ambiguous for the interpreter …
sensor1Hum < QuantityType
might become sensor1Hum<QuantityType>

Is your Item actually of Number:Dimensionless type?

Yes

Okay, so why are you stripping away the QuantityType from the Item state with a floatValue, but then comparing with QuntityTypes and not floats? Seems to me that you start with an apple, convert it to an orange, and thenhave trouble comparing orange with apples.

I would always work with simple values (float) and in the presentation add %

Stripping the QuantityType from the item was a test and advice from lukics. Please see my first post. There you can see the real problem

This is unnecessary, since format will convert the valuies to strings for you. Use…

.format(sensor1Hum, maxHumidity))

This will not work, since you cannot compare float and QuantityType. There’s no need to convert anything (leave them as QuantityType).

I haven’t tested this, but I think the crux of your problem is that this does not look to be a valid Item definition for QuantityType. Try using the following and see if that helps…

"Luftfeuchte Kellerraum groß [%.1f %%]"

thank you you are basically right, but I think the idea with the UoM is a very good idea and in principle it should be possible to use it throughout.

I tested this now and your Item definition seems to be the issue.

I also left out that there is no need to go to the ItemRegistry to get the Item’s state, since they are all in the items object. Use this…

		if (items["ZWaveNode2_Switch_greenWAVE"] != ON):

it was a mistake on my part, I saw it and it’s obvious.

I will now try

"Luftfeuchte Kellerraum groß [%.1f %unit%]

this should also work?

thanks for the hint

thank you very much also for this hint, i will adapt it

Yes, this should work too, but the example I gave worked in my tests and is how I define my Items.

FYI, it happened again!

The problem I shielded in the first post has reappeared. Here is the log entry:

2020-04-08 19:25:58.099 [INFO ] [.smarthome.model.core.internal.ModelRepositoryImpl] - Refreshing model 'meinHaus.items'
2020-04-08 19:25:58.129 [INFO ] [jsr223.jython.Jython_Andi                         ] - Entfeuchter muss eingeschaltet werden 53.0 > 63 %
2020-04-08 19:25:58.131 [INFO ] [jsr223.jython.Jython_Andi                         ] - Entfeuchter ein bei 53.0

The interesting thing is that at “19:25:58.099” the items file was reloaded ( I changed something about it). This is probably the reason for this, because I could see that the items file was reloaded in the first post as well.

Here is the current items definition (meinHaus.items):

....
Number:Dimensionless	LaCrosseTemperatureSensor1_Luftfeuchtigkeit	"Luftfeuchte Kellerraum groß [%.1f %unit%]" <humidity> (gLaCrosseHum, gGrenzwertUeberwachung) 	{channel="jeelink:lacrosse:18:humidity"}
...

and the rule:

maxHumidity = u"63 %"
minHumidity = u"55 %"


@rule("Entfeuchter schalten")
@when("Item LaCrosseTemperatureSensor1_Luftfeuchtigkeit changed") 
def Entfeuchter_schalten(event):
	LOG = logging.getLogger("{}.Jython_Andi".format(LOG_PREFIX))

	sensor1Hum = items["LaCrosseTemperatureSensor1_Luftfeuchtigkeit"]

	if sensor1Hum > QuantityType(maxHumidity):
		LOG.info("Entfeuchter muss eingeschaltet werden {} > {}".format(sensor1Hum, maxHumidity))
		if(items["ZWaveNode2_Switch_greenWAVE"] != ON):
			events.sendCommand("ZWaveNode2_Switch_greenWAVE", "ON")
			send_info_telegram("Entfeuchter ein bei {}".format(sensor1Hum))

	elif sensor1Hum < QuantityType(minHumidity):  
		LOG.info("Entfeuchter muss ausgeschaltet werden {} < {}".format(sensor1Hum, minHumidity))
		if(items["ZWaveNode2_Switch_greenWAVE"] != OFF):
			events.sendCommand("ZWaveNode2_Switch_greenWAVE", "OFF")
			send_info_telegram("Entfeuchter aus bei {}".format(sensor1Hum))

I do not get it why you are working with quantity type inside rules?

Why not doing it this way:

maxHumidity = 63
minHumidity = 55

.......

sensor1Hum = items["LaCrosseTemperatureSensor1_Luftfeuchtigkeit"].floatValue()
if sensor1Hum > maxHumidity:

.........

Which persistence service are you using? It would be good to do some more testing and open an issue in GH for this. Although, OH 3.0 will have new persistence services and there have been some updates to UoM, so it may no longer be an issue.

There are two more things you can try…

  1. See if using event.itemState instead of items[“LaCrosseTemperatureSensor1_Luftfeuchtigkeit”] makes a difference.
  2. Stop the rule if items[“LaCrosseTemperatureSensor1_Luftfeuchtigkeit”] is not a QuantityType…
if not isinstance(items["LaCrosseTemperatureSensor1_Luftfeuchtigkeit"], QuantityType):
    return

I believe Andi had already answered this for you…

1 Like

influxdb

Items {
  .....
  LaCrosseTemperatureSensor1_Temperatur             : strategy = every5Minute, restoreOnStartup
  LaCrosseTemperatureSensor1_Luftfeuchtigkeit       : strategy = every5Minute, restoreOnStartup
  LaCrosseTemperatureSensor2_Temperatur             : strategy = every5Minute, restoreOnStartup
  LaCrosseTemperatureSensor2_Luftfeuchtigkeit       : strategy = every5Minute, restoreOnStartup
  LaCrosseTemperatureSensor3_Temperatur             : strategy = every5Minute, restoreOnStartup
.....

I have tested it, unfortunately no difference:

2020-04-09 09:37:39.027 [INFO ] [.smarthome.model.core.internal.ModelRepositoryImpl] - Refreshing model 'meinHaus.items'
2020-04-09 09:37:39.060 [INFO ] [jsr223.jython.Jython_Andi                         ] - Entfeuchter muss eingeschaltet werden 52.0 > 63 %
2020-04-09 09:37:39.064 [INFO ] [jsr223.jython.Jython_Andi                         ] - Entfeuchter ein bei 52.0

another test with:

LOG.info("itenname {}: {}".format(event.itemName,type(items["LaCrosseTemperatureSensor1_Luftfeuchtigkeit"])))
2020-04-09 09:53:09.129 [INFO ] [.smarthome.model.core.internal.ModelRepositoryImpl] - Refreshing model 'meinHaus.items'
2020-04-09 09:53:09.162 [INFO ] [jsr223.jython.Jython_Andi                         ] - itenname LaCrosseTemperatureSensor1_Luftfeuchtigkeit: <type 'org.eclipse.smarthome.core.library.types.QuantityType'>
2020-04-09 09:53:09.163 [INFO ] [jsr223.jython.Jython_Andi                         ] - Entfeuchter muss eingeschaltet werden 52.0 > 63 %
2020-04-09 09:53:09.165 [INFO ] [jsr223.jython.Jython_Andi                         ] - Entfeuchter ein bei 52.0

In the label of your Item definition, try using the actual unit rather than %unit%.

yes, that seems to be the problem, if i change the item as you write, the problem will not occur anymore.
the problem can be clearly reproduced if i reverse the changes.

but in principle it should also work with %unit%, should I open an issue on GH for this?