Node-red-contrib-finite-statemachine stateChange with values

I want to watch my washing machine. I found the mentioned FSM and it seems to me a good starting point. But did I understand the usage wrong??
My first simple Statemachine will be:
grafik

I have the following JSON:

{
    "state": {
        "status": "OFF",
        "data": {
            "power": 0
        }
    },
    "transitions": {
        "OFF": {
            "power > 0": "RUNNING",
            "set": "OFF"
        },
        "RUNNING": {
            "power < 5": "OFF",
            "power > 20 ": "WASHING"
        },
        "WASHING": {
            "power < 5": "OFF"
        }
    }
}

So I thought, when I inject a new power value the FSM will go to the next state. But it doesn't. e.g. If I make:

the FSM should go from state OFF to the next state, because power > 0

My flow:

[
    {
        "id": "7a39b022.77cea8",
        "type": "finite-state-machine",
        "z": "1e09c961.5837b7",
        "name": "FSM",
        "fsmDefinition": "{\"state\":{\"status\":\"OFF\",\"data\":{\"power\":0}},\"transitions\":{\"OFF\":{\"power > 0\":\"RUNNING\",\"set\":\"OFF\"},\"RUNNING\":{\"power < 5\":\"OFF\",\"power > 20 \":\"WASHING\"},\"WASHING\":{\"power < 5\":\"OFF\"}}}",
        "sendInitialState": false,
        "sendStateWithoutChange": true,
        "showTransitionErrors": true,
        "x": 530,
        "y": 320,
        "wires": [
            [
                "cf078cc4.8ecfa"
            ]
        ]
    }
]

Welcome to the forum @Moosbueffel.

I don't see anywhere in the description of that node where it says you can perform tests like power > 0. Can you point me to where to find that please?

Edit: Or perhaps you have intended to feed in a topic containing the string "power > 0". If so then please show us in a debug node what is going into state machine node.
Did you realise you have only posted the state machine node and not the rest of the flow? You should mark all the nodes you want to export and then select Export.

Thanks @Colin for help. Perhaps I did a mistake exporting the flow. I'm very new to Node-Red. - sorry.

[
   {
       "id": "7a39b022.77cea8",
       "type": "finite-state-machine",
       "z": "1e09c961.5837b7",
       "name": "FSM",
       "fsmDefinition": "{\"state\":{\"status\":\"OFF\",\"data\":{\"power\":0}},\"transitions\":{\"OFF\":{\"power > 0\":\"RUNNING\",\"set\":\"OFF\"},\"RUNNING\":{\"power < 5\":\"OFF\",\"power > 20 \":\"WASHING\"},\"WASHING\":{\"power < 5\":\"OFF\"}}}",
       "sendInitialState": false,
       "sendStateWithoutChange": true,
       "showTransitionErrors": true,
       "x": 530,
       "y": 320,
       "wires": [
           [
               "cf078cc4.8ecfa"
           ]
       ]
   },
   {
       "id": "c13a5258.ece828",
       "type": "ui_numeric",
       "z": "1e09c961.5837b7",
       "name": "",
       "label": "Power",
       "tooltip": "User power of washer",
       "group": "ac2c8e97.2a413",
       "order": 1,
       "width": 0,
       "height": 0,
       "wrap": false,
       "passthru": true,
       "topic": "",
       "format": "{{msg.payload}}",
       "min": 0,
       "max": "100",
       "step": 1,
       "x": 90,
       "y": 220,
       "wires": [
           [
               "ee347cf6.74787"
           ]
       ]
   },
   {
       "id": "faefa1f5.ffc038",
       "type": "ui_slider",
       "z": "1e09c961.5837b7",
       "name": "",
       "label": "PowerSlider",
       "tooltip": "",
       "group": "ac2c8e97.2a413",
       "order": 4,
       "width": 0,
       "height": 0,
       "passthru": true,
       "outs": "end",
       "topic": "",
       "min": 0,
       "max": "100",
       "step": 1,
       "x": 110,
       "y": 320,
       "wires": [
           [
               "3b5e0363.c8d7bc"
           ]
       ]
   },
   {
       "id": "cf078cc4.8ecfa",
       "type": "debug",
       "z": "1e09c961.5837b7",
       "name": "",
       "active": true,
       "tosidebar": true,
       "console": false,
       "tostatus": false,
       "complete": "false",
       "statusVal": "",
       "statusType": "auto",
       "x": 690,
       "y": 320,
       "wires": []
   },
   {
       "id": "ee347cf6.74787",
       "type": "change",
       "z": "1e09c961.5837b7",
       "name": "",
       "rules": [
           {
               "t": "set",
               "p": "{power}",
               "pt": "msg",
               "to": "payload",
               "tot": "msg"
           },
           {
               "t": "set",
               "p": "topic",
               "pt": "msg",
               "to": "off",
               "tot": "str"
           }
       ],
       "action": "",
       "property": "",
       "from": "",
       "to": "",
       "reg": false,
       "x": 300,
       "y": 220,
       "wires": [
           [
               "7a39b022.77cea8",
               "3729d8ff.30db9"
           ]
       ]
   },
   {
       "id": "3b5e0363.c8d7bc",
       "type": "change",
       "z": "1e09c961.5837b7",
       "name": "",
       "rules": [
           {
               "t": "set",
               "p": "power",
               "pt": "msg",
               "to": "payload",
               "tot": "msg"
           },
           {
               "t": "set",
               "p": "topic",
               "pt": "msg",
               "to": "set",
               "tot": "str"
           }
       ],
       "action": "",
       "property": "",
       "from": "",
       "to": "",
       "reg": false,
       "x": 300,
       "y": 320,
       "wires": [
           [
               "7a39b022.77cea8",
               "cf6e77b3.0767b8"
           ]
       ]
   },
   {
       "id": "cf6e77b3.0767b8",
       "type": "debug",
       "z": "1e09c961.5837b7",
       "name": "",
       "active": true,
       "tosidebar": true,
       "console": false,
       "tostatus": false,
       "complete": "false",
       "statusVal": "",
       "statusType": "auto",
       "x": 530,
       "y": 420,
       "wires": []
   },
   {
       "id": "3729d8ff.30db9",
       "type": "function",
       "z": "1e09c961.5837b7",
       "name": "Debug",
       "func": "// Debug V2.0, by Thorsten Willert\n// added: BigInt, JSON and Array (+ length) as \"data-types\".\n// changed: limited message-display to 50 chars.\n// changed: removed \"debug\" topic and added msg.topic to the node.status\n\nconst dtNow = new Date().toLocaleString();\n\nlet Type = typeof(msg.payload);\n\nlet sMsg = msg.payload;\n\nif (msg.payload.isArray) {\n    Type = 'array';\n    sMsg += ' (' + msg.payload.length + ')';\n}\n\nif (Type == \"string\") sMsg = msg.payload.substring(0, 50) + ' ...'; \n\n\nif (Type == 'string' && IsJsonString(msg.payload) ) {\n    Type = 'JSON';\n}\n\nlet sColor = typeToColor(Type);\nlet sShape = typeToShape(Type);\nlet Topic = '';\n\nif (msg.topic !== '') {\n    Topic = msg.topic + \" | \";\n}\n\nnode.status({\n        fill: sColor ,\n        shape: sShape,\n        text: dtNow + \" | \"  + Topic + Type + \" | \" + sMsg\n});\n\nreturn msg;\n\n// -------------------------------------------------------\n\nfunction typeToColor(value) {\n    switch(value){\n        case \"boolean\":\n            return \"red\";\n        case \"string\":\n        case \"JSON\":\n            return \"blue\";\n        case \"number\":\n            return \"yellow\";\n        case \"object\":\n        case \"array\":\n            return \"green\";\n        default:\n        return \"grey\";\n    }\n}\n\nfunction typeToShape(value) {\n    switch(value){\n        case \"boolean\":\n        case \"string\":\n        case \"number\":\n        case \"object\":\n            return \"dot\";\n        case \"bigint\":\n        case \"JSON\":\n        case \"array\":\n            return \"ring\";\n        default:\n        return \"dot\";\n    }\n}\n\nfunction IsJsonString(str) {\n    try {\n        JSON.parse(str);\n    } catch (e) {\n        return false;\n    }\n    return true;\n}",
       "outputs": 1,
       "noerr": 0,
       "initialize": "",
       "finalize": "",
       "x": 530,
       "y": 180,
       "wires": [
           []
       ],
       "inputLabels": [
           "msg.payload"
       ],
       "outputLabels": [
           "msg.payload"
       ],
       "info": "# Debug\n\nFunction node for easy debugging.\n\nSimply insert it into a connection:\n\n![screenshot](http://www.thorsten-willert.de//images/Software/Node-RED/Thorsten_H_Willert_-_Node-RED_Debug.png)\n\nThe following is displayed as the status text:\n\n- \"Data-type\" as color / shape:\n    - Boolean: Red (dot)\n    - String: Blue (dot)\n    - JSON: Blue (ring)\n    - Number: Yellow (dot)\n    - BigInt: Yellow (ring)\n    - Array: Green (ring)\n    - Object: Green (dot)\n    - Everything else: Grey (dot)\n- Date and time of the last message\n- Topic (msg.topic)\n- Data-type\n- Payload (msg.payload) limited to 50 chars"
   },
   {
       "id": "1e9abaa0.cf207d",
       "type": "inject",
       "z": "1e09c961.5837b7",
       "name": "",
       "props": [
           {
               "p": "payload"
           },
           {
               "p": "topic",
               "vt": "str"
           }
       ],
       "repeat": "",
       "crontab": "",
       "once": false,
       "onceDelay": 0.1,
       "topic": "set",
       "payload": "{\"power\" : 10}",
       "payloadType": "json",
       "x": 300,
       "y": 380,
       "wires": [
           [
               "7a39b022.77cea8"
           ]
       ]
   },
   {
       "id": "ac2c8e97.2a413",
       "type": "ui_group",
       "name": "test",
       "tab": "d92ab4b4.55f5c8",
       "order": 3,
       "disp": true,
       "width": 5,
       "collapse": true
   },
   {
       "id": "d92ab4b4.55f5c8",
       "type": "ui_tab",
       "name": "test",
       "icon": "fa-television",
       "order": 2
   }
]

Perhaps I understood the FSM wrong. That's the point I think I programmed/parametrized the state change:

grafik

I haven't used that node, but I can't see anything in the docs for the node that says you can put an expression like "power > 5" there. Can you see that or find anything similar in any of the examples? If not then it is probably not something you can do.

If you are very new to node-red then, if you haven't already done so, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain. It won't address this particular problem but should give you a lot of useful background knowledge.

Again, not directly related to the problem, but I see that in one of the change nodes you have

image

I don't know why the braces in {payload} are there, that will create a property msg.{power} which probably isn't what you meant to do.

I think that to do what you want with that node you will have to use Switch nodes to work out which band the power is in and then send a message with the appropriate topic (topic "power > 5" etc) to cause the state transition.

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