tmparisi
tmparisi

Reputation: 111

Looking for assistance with Node-Red trigger node as a dynamic timer

Killing myself over this one and hoping someone can help.

The issue: In the impending JSON node-red flow I have created an automation for my fireplace. Everything seems to work correctly with the exception of an auto-off timer I have implemented. The timer gets a value from 'input_number.auto_off_fireplace_timing' (set by the user). This number is then sent as msg.auto_off_timing = entity_state. A function node received this and runs the following JS against it:

const timingMinutes = Number(msg.auto_off_timing);

if (isNaN(timingMinutes) || timingMinutes <= 0) {
    node.error("Invalid timing value", msg);
    return null;
}

const timingMilliseconds = timingMinutes * 60 * 1000;

msg.delay = timingMilliseconds;
delete msg.reset;
return msg;

It then sends this message to the Auto-off Timer Trigger Node, which seems to start (blue circle present) but does not abide by the math outlined. In fact, I can't get it to continue on from the trigger and given the node type, there is no good way to monitor the current status of its countdown.

At a lost here and hoping someone can help. here is my JSON (some of the other safety checks, thermostat, auto-off after 2 hours, etc... have been removed to focus on the issue):

[
    {
        "id": "109ae81398e4db33",
        "type": "subflow",
        "name": "Automation Gate (5) (5) (2) (3)",
        "info": "This subflow checks the status of input_boolean.nodered_enable and allows automations to proceed only if it is 'on'.",
        "category": "",
        "in": [
            {
                "x": 60,
                "y": 80,
                "wires": [
                    {
                        "id": "6a0e54df8bebfb35"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 460,
                "y": 80,
                "wires": [
                    {
                        "id": "6a0e54df8bebfb35",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "color": "#DDAA99",
        "icon": "node-red/switch.svg",
        "status": {
            "x": 460,
            "y": 140,
            "wires": [
                {
                    "id": "6a0e54df8bebfb35",
                    "port": 1
                }
            ]
        }
    },
    {
        "id": "6a0e54df8bebfb35",
        "type": "api-current-state",
        "z": "109ae81398e4db33",
        "name": "Check nodered_enable",
        "server": "home_assistant",
        "version": 3,
        "outputs": 2,
        "halt_if": "on",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "input_boolean.nodered_enable",
        "state_type": "str",
        "outputProperties": [
            {
                "property": "nodered_enable",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            }
        ],
        "for": 0,
        "forType": "num",
        "forUnits": "minutes",
        "x": 260,
        "y": 80,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "home_assistant",
        "type": "server",
        "name": "Home Assistant",
        "addon": true
    },
    {
        "id": "e2022f3797213292",
        "type": "tab",
        "label": "Fireplace Automation",
        "disabled": false,
        "info": ""
    },
    {
        "id": "18112a371a5730af",
        "type": "function",
        "z": "e2022f3797213292",
        "name": "Start or Reset Timers",
        "func": "if (msg.payload === 'on') {\n    // Fireplace turned on\n    return [msg, null]; // Start timers\n} else {\n    // Fireplace turned off\n    return [null, msg]; // Reset timers\n}",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1140,
        "y": 100,
        "wires": [
            [
                "26de0bc92b3c1fa2"
            ],
            [
                "49c2de739512f63e"
            ]
        ]
    },
    {
        "id": "26de0bc92b3c1fa2",
        "type": "link out",
        "z": "e2022f3797213292",
        "name": "Start Timers",
        "links": [
            "9bb3cef0f93bc9ca",
            "10192ba7e96e6de6"
        ],
        "x": 1305,
        "y": 60,
        "wires": []
    },
    {
        "id": "49c2de739512f63e",
        "type": "link out",
        "z": "e2022f3797213292",
        "name": "Reset Timers",
        "links": [
            "96384ada3ddffe94",
            "9f9c274ad7a7daf4"
        ],
        "x": 1305,
        "y": 120,
        "wires": []
    },
    {
        "id": "9bb3cef0f93bc9ca",
        "type": "link in",
        "z": "e2022f3797213292",
        "name": "Auto-Off Timer In",
        "links": [
            "26de0bc92b3c1fa2",
            "3aaa3b66d3c867c9"
        ],
        "x": 605,
        "y": 340,
        "wires": [
            [
                "07dd056055f5c8d0"
            ]
        ]
    },
    {
        "id": "07dd056055f5c8d0",
        "type": "api-current-state",
        "z": "e2022f3797213292",
        "name": "Is Auto-Off Enabled?",
        "server": "d262b87e.b0ac48",
        "version": 3,
        "outputs": 2,
        "halt_if": "on",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "input_boolean.auto_off_fireplace",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "auto_off",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            }
        ],
        "for": 0,
        "forType": "num",
        "forUnits": "minutes",
        "x": 740,
        "y": 340,
        "wires": [
            [
                "d77b4e28dd6bb6b7"
            ],
            []
        ]
    },
    {
        "id": "d77b4e28dd6bb6b7",
        "type": "api-current-state",
        "z": "e2022f3797213292",
        "name": "Get Auto-Off Timing",
        "server": "d262b87e.b0ac48",
        "version": 3,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "num",
        "halt_if_compare": "is",
        "entity_id": "input_number.auto_off_fireplace_timing",
        "state_type": "num",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "auto_off_timing",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            }
        ],
        "for": 0,
        "forType": "num",
        "forUnits": "minutes",
        "x": 960,
        "y": 340,
        "wires": [
            [
                "be97b45c9eff76da"
            ]
        ]
    },
    {
        "id": "be97b45c9eff76da",
        "type": "function",
        "z": "e2022f3797213292",
        "name": "Set Auto-Off Timer",
        "func": "const timingMinutes = Number(msg.auto_off_timing);\n\nif (isNaN(timingMinutes) || timingMinutes <= 0) {\n    node.error(\"Invalid timing value\", msg);\n    return null;\n}\n\nconst timingMilliseconds = timingMinutes * 60 * 1000;\n\nmsg.delay = timingMilliseconds;\ndelete msg.reset;\nreturn msg;\n",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1170,
        "y": 340,
        "wires": [
            [
                "d747581c05875140"
            ]
        ]
    },
    {
        "id": "d747581c05875140",
        "type": "trigger",
        "z": "e2022f3797213292",
        "name": "Auto-Off Timer",
        "op1": "",
        "op2": "",
        "op1type": "nul",
        "op2type": "pay",
        "duration": "0",
        "extend": false,
        "overrideDelay": false,
        "units": "ms",
        "reset": "stop",
        "bytopic": "all",
        "topic": "delay",
        "outputs": 1,
        "x": 1420,
        "y": 340,
        "wires": [
            [
                "f1a9129f98b8ffe6"
            ]
        ]
    },
    {
        "id": "96384ada3ddffe94",
        "type": "link in",
        "z": "e2022f3797213292",
        "name": "Auto-Off Timer Reset",
        "links": [
            "49c2de739512f63e"
        ],
        "x": 605,
        "y": 400,
        "wires": [
            [
                "76c598a7f58e7ef2"
            ]
        ]
    },
    {
        "id": "76c598a7f58e7ef2",
        "type": "change",
        "z": "e2022f3797213292",
        "name": "Reset Auto-Off Timer",
        "rules": [
            {
                "t": "set",
                "p": "reset",
                "pt": "msg",
                "to": "stop",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1160,
        "y": 400,
        "wires": [
            [
                "d747581c05875140"
            ]
        ]
    },
    {
        "id": "3aaa3b66d3c867c9",
        "type": "server-state-changed",
        "z": "e2022f3797213292",
        "name": "Auto-Off Timing Changed",
        "server": "d262b87e.b0ac48",
        "version": 6,
        "outputs": 1,
        "exposeAsEntityConfig": "",
        "entities": {
            "entity": [
                "input_number.auto_off_fireplace_timing"
            ],
            "substring": [],
            "regex": []
        },
        "outputInitially": false,
        "stateType": "str",
        "ifState": "",
        "ifStateType": "str",
        "ifStateOperator": "is",
        "outputOnlyOnStateChange": true,
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            }
        ],
        "x": 150,
        "y": 360,
        "wires": [
            [
                "0598fdc1b02c8e8a"
            ]
        ]
    },
    {
        "id": "6aed46c49381ad76",
        "type": "server-state-changed",
        "z": "e2022f3797213292",
        "name": "Switch Fireplace Pressed",
        "server": "d262b87e.b0ac48",
        "version": 6,
        "outputs": 1,
        "exposeAsEntityConfig": "",
        "entities": {
            "entity": [
                "switch.fireplace"
            ],
            "substring": [],
            "regex": []
        },
        "outputInitially": false,
        "stateType": "str",
        "ifState": "",
        "ifStateType": "str",
        "ifStateOperator": "is",
        "outputOnlyOnStateChange": true,
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            }
        ],
        "x": 150,
        "y": 80,
        "wires": [
            [
                "a1d29d4c48c3b409"
            ]
        ]
    },
    {
        "id": "bcf26e334a507ef8",
        "type": "switch",
        "z": "e2022f3797213292",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "on",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "off",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 570,
        "y": 80,
        "wires": [
            [
                "ae08ca697b2675c4"
            ],
            [
                "e27da748b8161e01"
            ]
        ],
        "outputLabels": [
            "on",
            "off"
        ]
    },
    {
        "id": "a1d29d4c48c3b409",
        "type": "api-current-state",
        "z": "e2022f3797213292",
        "name": "Check Fireplace State",
        "server": "d262b87e.b0ac48",
        "version": 3,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "input_boolean.fireplace_onoff",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "fireplaceState",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            }
        ],
        "for": 0,
        "forType": "num",
        "forUnits": "minutes",
        "x": 400,
        "y": 80,
        "wires": [
            [
                "bcf26e334a507ef8"
            ]
        ]
    },
    {
        "id": "ae08ca697b2675c4",
        "type": "api-call-service",
        "z": "e2022f3797213292",
        "name": "Set Fireplace On",
        "server": "d262b87e.b0ac48",
        "version": 7,
        "debugenabled": false,
        "action": "input_boolean.turn_on",
        "floorId": [],
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "input_boolean.fireplace_onoff"
        ],
        "labelId": [],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "blockInputOverrides": false,
        "domain": "input_boolean",
        "service": "turn_on",
        "output_location": "",
        "output_location_type": "none",
        "x": 830,
        "y": 40,
        "wires": [
            [
                "18112a371a5730af"
            ]
        ]
    },
    {
        "id": "e27da748b8161e01",
        "type": "api-call-service",
        "z": "e2022f3797213292",
        "name": "Set Fireplace Off",
        "server": "d262b87e.b0ac48",
        "version": 7,
        "debugenabled": false,
        "action": "input_boolean.turn_off",
        "floorId": [],
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "input_boolean.fireplace_onoff"
        ],
        "labelId": [],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "blockInputOverrides": false,
        "domain": "input_boolean",
        "service": "turn_off",
        "output_location": "",
        "output_location_type": "none",
        "x": 830,
        "y": 120,
        "wires": [
            [
                "18112a371a5730af"
            ]
        ]
    },
    {
        "id": "f1a9129f98b8ffe6",
        "type": "api-call-service",
        "z": "e2022f3797213292",
        "name": "Turn off Fireplace",
        "server": "d262b87e.b0ac48",
        "version": 7,
        "debugenabled": false,
        "action": "switch.turn_off",
        "floorId": [],
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "switch.fireplace"
        ],
        "labelId": [],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "blockInputOverrides": true,
        "domain": "switch",
        "service": "turn_off",
        "x": 1670,
        "y": 460,
        "wires": [
            [
                "0bc53e3b2e62d06e"
            ]
        ]
    },
    {
        "id": "0598fdc1b02c8e8a",
        "type": "subflow:109ae81398e4db33",
        "z": "e2022f3797213292",
        "name": "Automation Gate",
        "x": 370,
        "y": 360,
        "wires": [
            [
                "96384ada3ddffe94",
                "9bb3cef0f93bc9ca"
            ]
        ]
    },
    {
        "id": "0bc53e3b2e62d06e",
        "type": "api-call-service",
        "z": "e2022f3797213292",
        "name": "Set Fireplace Off",
        "server": "d262b87e.b0ac48",
        "version": 7,
        "debugenabled": false,
        "action": "input_boolean.turn_off",
        "floorId": [],
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "input_boolean.fireplace_onoff"
        ],
        "labelId": [],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "blockInputOverrides": false,
        "domain": "input_boolean",
        "service": "turn_off",
        "output_location": "",
        "output_location_type": "none",
        "x": 1870,
        "y": 460,
        "wires": [
            []
        ]
    },
    {
        "id": "d262b87e.b0ac48",
        "type": "server",
        "name": "Home Assistant",
        "version": 5,
        "addon": true,
        "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": "at: ",
        "statusYear": "hidden",
        "statusMonth": "short",
        "statusDay": "numeric",
        "statusHourCycle": "h23",
        "statusTimeFormat": "h:m",
        "enableGlobalContextStore": true
    }
]

Upvotes: 0

Views: 47

Answers (1)

tmparisi
tmparisi

Reputation: 111

Using the debugging technique suggested by @hardillb I was able to get this working.

Upvotes: 0

Related Questions