Behaviour of switch node not as expected (help needed)

Hello,

I need some help to understand how to handle the switch node/and or the syntax of the msg.
My aim is to compare Ta a temperature read from a sensor to a target temperature (Tconsigne).
Tconsigne is set via a flow variable: flow.Tc, so is a its tolerable offset flow.DTc
Depending of the difference between Tconsigne and Ta, I want to start different actions driven by different function nodes.

I spent many hours trying to check the correct syntax to specify the test conditions, but I failed to make it work for all cases. In the following example, if Ta largely below Tc, I cannot trigger the first case (opening the valve).

image

Here is the corresponding code for these nodes:

[
    {
        "id": "a6ed9ff7addd787e",
        "type": "server-state-changed",
        "z": "3ce7ee343e713b47",
        "name": "Sensor Rdc ATC",
        "server": "4f31a1c534ee3e6c",
        "version": 5,
        "outputs": 1,
        "exposeAsEntityConfig": "",
        "entityId": "sensor.atc_e2d7_temperature",
        "entityIdType": "exact",
        "outputInitially": false,
        "stateType": "num",
        "ifState": "",
        "ifStateType": "str",
        "ifStateOperator": "is",
        "outputOnlyOnStateChange": false,
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": true,
        "ignoreCurrentStateUnavailable": true,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            }
        ],
        "x": 142,
        "y": 661.0000305175781,
        "wires": [
            [
                "e54955cd1e7213ca",
                "3f57dfb8b8d9446f",
                "3a9f1eb0df8eff81",
                "632bf24df66e5acf",
                "6f63e40d38fbef80"
            ]
        ]
    },
    {
        "id": "e54955cd1e7213ca",
        "type": "switch",
        "z": "3ce7ee343e713b47",
        "name": "Comparaison Ă  Tc",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "lt",
                "v": "flow.Tc() - flow.DTc()",
                "vt": "jsonata"
            },
            {
                "t": "lt",
                "v": "Tc",
                "vt": "flow"
            },
            {
                "t": "gt",
                "v": "flow.Tc() + flow.DTc()",
                "vt": "jsonata"
            },
            {
                "t": "gte",
                "v": "Tc",
                "vt": "flow"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 5,
        "x": 390,
        "y": 660,
        "wires": [
            [
                "344d448a6c51e098",
                "7c3cff876ad370ed"
            ],
            [
                "684906a150819e97",
                "142992456b3246e0"
            ],
            [
                "300ea75091713cf6",
                "b4e1aeca822535c2"
            ],
            [
                "2265afe0c55736d7",
                "300ea75091713cf6"
            ],
            [
                "6362cec2f9435841"
            ]
        ],
        "inputLabels": [
            "Temp"
        ],
        "outputLabels": [
            "< Tc-DTc",
            "< Tc",
            "in betwen Tc Tc+ DTc",
            ">=Tc",
            "else"
        ]
    },
    {
        "id": "7c3cff876ad370ed",
        "type": "function",
        "z": "3ce7ee343e713b47",
        "name": "Signal Open Vanne",
        "func": "var msg1={payload:1};\nvar msg2 = { payload: 0 };\nreturn [msg1,msg2];",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 800,
        "y": 540,
        "wires": [
            [
                "03c26c2e62201f45",
                "1db56187faf4c92b"
            ],
            [
                "a4e30eb95197c137",
                "4e5453014490d3d5"
            ]
        ]
    },
    {
        "id": "142992456b3246e0",
        "type": "function",
        "z": "3ce7ee343e713b47",
        "name": "Signal Standby Vanne",
        "func": "var msg1={ payload: 0 };\nvar msg2 = { payload: 0 };\nreturn [ msg1,msg2 ];",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 780,
        "y": 660,
        "wires": [
            [
                "6d22e32e921bcb7d",
                "1db56187faf4c92b"
            ],
            [
                "33b44b957e262585",
                "4e5453014490d3d5"
            ]
        ]
    },
    {
        "id": "300ea75091713cf6",
        "type": "function",
        "z": "3ce7ee343e713b47",
        "name": "Signal Close Vanne",
        "func": "var msg1={payload:0};\nvar msg2 = { payload: 1 };\nreturn [msg1,msg2];",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 780,
        "y": 780,
        "wires": [
            [
                "76e30cfb9b85619e",
                "1db56187faf4c92b"
            ],
            [
                "0fb7b9e94adea266",
                "4e5453014490d3d5"
            ]
        ]
    },
    {
        "id": "6362cec2f9435841",
        "type": "debug",
        "z": "3ce7ee343e713b47",
        "name": "Cas non traité",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 620,
        "y": 960,
        "wires": []
    },
    {
        "id": "4f31a1c534ee3e6c",
        "type": "server",
        "name": "Home Assistant",
        "version": 5,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": "30",
        "areaSelector": "friendlyName",
        "deviceSelector": "friendlyName",
        "entitySelector": "friendlyName",
        "statusSeparator": ": ",
        "statusYear": "hidden",
        "statusMonth": "short",
        "statusDay": "numeric",
        "statusHourCycle": "default",
        "statusTimeFormat": "h:m",
        "enableGlobalContextStore": false
    }
]

The message issued from the HA-node which carries Ta looks like:

{"payload":18.28,"data":{"entity_id":"sensor.atc_temperature","old_state":{"entity_id":"sensor.atc_temperature","state":18.29,"attributes":{"state_class":"measurement","unit_of_measurement":"°C","device_class":"temperature","friendly_name":"ATC E2D7 Temperature"},"last_changed":"2024-01-31T09:15:24.987036+00:00","last_updated":"2024-01-31T09:15:24.987036+00:00","context":{"id":"01HNFCTEXVEJTF4WX177GMC4GA","parent_id":null,"user_id":null},"original_state":"18.29"},"new_state":{"entity_id":"sensor.atc_temperature","state":18.28,"attributes":{"state_class":"measurement","unit_of_measurement":"°C","device_class":"temperature","friendly_name":"ATC  Temperature"},"last_changed":"2024-01-31T09:18:01.886325+00:00","last_updated":"2024-01-31T09:18:01.886325+00:00","context":{"id":"01HNFCZ84Y6QXTR546ERM2S76T","parent_id":null,"user_id":null},"original_state":"18.28","timeSinceChangedMs":40}},"topic":"sensor.atc_temperature","_msgid":"a4ddc61bd032b9a6"}

(node-RED v3.0.2)

The syntax for JSONata context is
$flowContext("Tc")
Which is available in the JSONata editor function reference

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.