Rules: compare value but item state has units

Hi everyone!

I’m on openhab 2.5 using the next gen rules in paper ui, so my config is in JSON.
I’m not using old style item / rules files.

I’d like to create a rule to trigger a command if CO2 (from my netatmo) exceeds a vaIue:

{
    "link": "http://192.168.178.25:8080/rest/items/Netatmo_LivingRoom_CO2",
    "state": "1230 ppm",
    "stateDescription": {
        "pattern": "%d %unit%",
        "readOnly": true,
        "options": []
    },
    "editable": true,
    "type": "Number:Dimensionless",
    "name": "Netatmo_LivingRoom_CO2",
    "label": "CO2",
    "tags": [],
    "groupNames": []
}

As you can see the state has the ppm included:

"state": "1230 ppm"

This is the rule I created:

{
        "status": {
            "status": "IDLE",
            "statusDetail": "NONE"
        },
        "triggers": [
            {
                "id": "4",
                "label": "an item state changes",
                "description": "This triggers the rule if an item state has changed.",
                "configuration": {
                    "itemName": "Netatmo_LivingRoom_CO2"
                },
                "type": "core.ItemStateChangeTrigger"
            }
        ],
        "conditions": [
            {
                "inputs": {},
                "id": "5",
                "label": "an item has a given state",
                "description": "Compares the item state with the given value",
                "configuration": {
                    "itemName": "Netatmo_LivingRoom_CO2",
                    "state": "500",
                    "operator": ">"
                },
                "type": "core.ItemStateCondition"
            }
        ],
        "actions": [
            {
                "inputs": {},
                "id": "3",
                "label": "send a command",
                "description": "Sends a command to a specified item.",
                "configuration": {
                    "itemName": "LaMetric_Netatmo_red_Running",
                    "command": "ON"
                },
                "type": "core.ItemCommandAction"
            }
        ],
        "configuration": {},
        "configDescriptions": [],
        "uid": "d7eaaa2c-0bec-492d-a8e2-ed56ecc07216",
        "name": "LaMetricRed",
        "tags": [],
        "visibility": "VISIBLE"
    }

How can I use the operators <,>,= etc in this case? This is not working:

"state": "500",
"operator": ">"

Thanks
Chris

The state that you are comparing to will also need to have the unit. Here is an example of a Condition with a temperature comparison…

Seems not to work. This rule should trigger if CO2 is below 800 ppm. But it’s not triggered.

In the log I can see the rules are triggered when the item state changes. Triggering the rules manually runs the command. So I’m sure it’s issue is the update condition.

IIRC there is a bug when saving a UI rule more than once. I’ll try searching for it later. Try deleting the rule and build it from scratch to see if that works… I think that was the work around.

Rebuilding the rules from scratch in “one shot” did not make it work.

I solved my problem by changing the item to be a number without units.

I dug a bit and it’s not a bug, but by design. Currently, only DecimalTypes can be compared in rule Conditions, unless you use a ScriptedCondition. Using “a given script evaliuated to true”, you can compare QuantityTypes using your preferred scripting language.