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)))
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.
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…
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):
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…
See if using event.itemState instead of items[“LaCrosseTemperatureSensor1_Luftfeuchtigkeit”] makes a difference.
Stop the rule if items[“LaCrosseTemperatureSensor1_Luftfeuchtigkeit”] is not a QuantityType…
if not isinstance(items["LaCrosseTemperatureSensor1_Luftfeuchtigkeit"], QuantityType):
return
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?