Node-Red Flow Samples/Sharing

i think msg.topic would have the name of the last device in the chain (doesn't msg.topic default to the device name?). either way, you could certainly use a msg.variable rather than a flow.variable.

1 Like

Yes, that is correct. So it would have to be assigned to a variable as you suggested.

Alternatively (my preference) to put all device states into flow variables, and then just use switch nodes in place of a device node and a switch node. In this scenario, the original msg.topic would be retained.

interesting. why do you do this? Is there a performance benefit, or is it just to make your flows more streamlined?

I suppose you just have a list of all devices used in the flow over to the side somewhere updating their variables?

I feel like I would inevitably end up with typos that would drive me nuts!

1 Like

I use device state multiple times - so it makes sense to save the desired state as a variable, and just use the variable. For devices that I use across flows, I save them as global variables. And yes, it will makes sequences slightly more streamlined.

I feel like I would inevitably end up with typos that would drive me nuts!

Copy/paste from the context data list .....

1 Like

Question for anyone using any of the Alexa nodes.

Is there a way to create a notification like amazon does when a package has been delivered or weather alert?

I tried 'Push Notification' but it didn't seem to do anything.

I've used the "Alexa Routine" node and "Announcement" setting.

That's really no different than the regular speak function with the exception of the notification sound at the beginning.

What I'm looking for is the type of notifications that are queued until you say 'Alexa read my notifications'

Like when you get a package delivery and the echo is pulsing yellow (iirc).

1 Like

Gotcha - have that disabled so don't use that functionality. I might give it try though for giggles. It does seem like the "Push Notification" thing should work though. What kind of formatting are you using? nvmd - see it's straight up text.

Can confirm - Unable to get a push notification either.

edit: I think Push Notification has something to do with pushing alerts to your phone but not sure..
edit2: I wonder if there is a way to set a reminder instead. You can give commands to Alexa via that node.

also this:

Confirmed Push goes to Alexa on phone.

I thought of reminder but its essentially an alarm.

Notify Me skill looks like it might be the solution. Looking into it now,

Thanks

Can confirm the Notify Me skill works and there is a NR node for it.

1 Like

@erktrek

What is the "simple toggle" in this flow? Could you share?

image

Sure! It's very very simple though.. but is a good example of a subflow :smile:

Simple Toggle

[{"id":"d18a2b0.2e233d8","type":"subflow","name":"Simple Toggle on/off","info":"","category":"","in":[{"x":80,"y":120,"wires":[{"id":"b8812dc3.a5b85"}]}],"out":[{"x":740,"y":80,"wires":[{"id":"fa67c9c1.a69a88","port":0}]},{"x":740,"y":160,"wires":[{"id":"6e76f259.b1f72c","port":0}]}],"env":[],"color":"#DDAA99","status":{"x":740,"y":300,"wires":[{"id":"1b03c1050aa50c9c","port":0}]}},{"id":"b8812dc3.a5b85","type":"switch","z":"d18a2b0.2e233d8","name":"Is on/off","property":"payload.value","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":240,"y":120,"wires":[["fa67c9c1.a69a88"],["6e76f259.b1f72c"]]},{"id":"fa67c9c1.a69a88","type":"change","z":"d18a2b0.2e233d8","name":"on set off","rules":[{"t":"set","p":"command","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":80,"wires":[["1b03c1050aa50c9c"]]},{"id":"6e76f259.b1f72c","type":"change","z":"d18a2b0.2e233d8","name":"off set on","rules":[{"t":"set","p":"command","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":160,"wires":[["1b03c1050aa50c9c"]]},{"id":"1f6b7b17.d4a9b5","type":"comment","z":"d18a2b0.2e233d8","name":"set command based on on/off toggle ","info":"","x":260,"y":220,"wires":[]},{"id":"1b03c1050aa50c9c","type":"change","z":"d18a2b0.2e233d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"command","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":300,"wires":[[]]},{"id":"3626ebc0facb3554","type":"subflow:d18a2b0.2e233d8","z":"af91369d.586338","name":"","x":550,"y":1320,"wires":[[],[]]}]

I have a much more sophisticated one called "advanced toggle".. but it might be overkill for stuff like this.

1 Like

I've seen subflows but haven't got to the stage in my NR rabbit hole. Are they equivalent to a sub-routine in a conventional programming language?

What's the difference between this and the advanced?

1 Like

Yes that's it exactly! Subflows are very good for standardizing your sequences. Easier to maintain and edit across all your stuff. EDIT: Maybe "function" is a better analogy.

Advanced accepts json input so you can use it for on/off, open/close and convert like "on/off" to "open/close" whatever. Also you can choose to invert "on/off" goes to "off/on" or just passthrough. It's very flexible but the parameters might be a little confusing.

Advanced Toggle

[{"id":"049dd7eab1551ac1","type":"subflow","name":"AdvancedToggle","info":"","category":"","in":[{"x":60,"y":60,"wires":[{"id":"c11d7711d06a4d51"}]}],"out":[{"x":640,"y":80,"wires":[{"id":"c11d7711d06a4d51","port":0}]}],"env":[{"name":"INPUT","type":"json","value":"{\"payload.value\":\"on/off\"}"},{"name":"OUTPUT","type":"json","value":"[{\"command\":\"on\",\"arguments\":\"\"},{\"command\":\"off\",\"arguments\":\"\"}]"},{"name":"INVERT","type":"bool","value":"false"}],"meta":{},"color":"#FDF0C2","icon":"node-red-contrib-sun-position/inputTypeRandomNumber.svg","status":{"x":640,"y":200,"wires":[{"id":"c11d7711d06a4d51","port":0}]}},{"id":"c11d7711d06a4d51","type":"function","z":"049dd7eab1551ac1","name":"Based on Input and \"invert\" convert to Output","func":"\nvar input = env.get(\"INPUT\");\nvar output = env.get(\"OUTPUT\");\nvar invert = env.get(\"INVERT\");\n\n/* uncomment if want to pass parms in via msg \nif (msg.hasOwnProperty(\"at\") ) {\n    if (msg.at.hasOwnProperty(\"input\") ) {\n        input = msg.at.hasOwnProperty(\"input\");\n    }\n    if (msg.at.hasOwnProperty(\"output\") ) {\n        output = msg.at.hasOwnProperty(\"output\");\n    }\n    if (msg.at.hasOwnProperty(\"invert\") ) {\n        invert = msg.at.hasOwnProperty(\"invert\");\n    }\n}\n*/\n\nvar retMsg;\nvar inputProp = Object.keys(input)[0];\nvar inputTypes = input[inputProp].split(\"/\");\nvar inputVal = getVal(msg,inputProp);\nvar valIndex = inputTypes.findIndex(type => type === inputVal.toString());\nvar val;\nif (valIndex != -1) {\n    //retMsg = msg;\n    var newIndex = (invert ? (valIndex == 0 ? 1 : 0) : valIndex );\n    var oOutput = output[newIndex];\n    \n    if (oOutput.hasOwnProperty(\"_actions\")){\n        \n        outputActions = oOutput[\"_actions\"];\n        //node.warn(outputActions)\n\n        for (var i=0;i<outputActions.length;i++) {\n            retMsg = {};\n            oAction = outputActions[i];\n            keys = Object.keys(oAction);\n            for (var j=0;j<keys.length;j++) {\n                val = oAction[keys[j]];\n                setVal(retMsg,keys[j],val);\n            }\n            //node.warn(retMsg);\n            node.send(retMsg);\n        }\n        \n    } else {\n        retMsg = {};\n        keys = Object.keys(oOutput);\n        for (var k=0;k<keys.length;k++) {\n            val = oOutput[keys[k]];\n            setVal(retMsg,keys[k],val);\n        }\n        node.send(retMsg);\n    }\n    node.done();\n\n}\n\n//return retMsg;\nreturn;\n\nfunction getVal(obj,propName) {\n\n    var retVal;\n    var props = propName.split(\".\");\n    var name = props[0];\n    \n    if (props.length > 1) {\n        if (obj.hasOwnProperty(name)) {\n            props.shift();\n            var newProps = props.join(\".\");\n            retVal = getVal(obj[name],newProps)\n        }\n    } else {\n        retVal = obj[name];\n    }\n    \n    return retVal;\n\n}\n\n\nfunction setVal(obj,propName,val) {\n    var props = propName.split(\".\");\n    var name = props[0];\n\n    if (props.length > 1) {\n        if (!obj.hasOwnProperty(name)) {\n            obj[name] = {};\n        }\n        \n        props.shift();\n        var newProps = props.join(\".\");\n        setVal(obj[name],newProps,val)\n    } else {\n        obj[name] = val;\n    }\n    \n    return;\n\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":140,"wires":[[]]},{"id":"ce9f98597e16b102","type":"subflow:049dd7eab1551ac1","z":"e6807da71ee5477b","name":"","x":1010,"y":740,"wires":[[]]}]

I used "subroutine" as I didn't want to confuse it with the actual "function" node.

I'm confused by this part of the subflow. Is it simply for monitoring the status of the subflow or does it send the on/off command to the HE device command node?

image

It's simply for monitoring status.. it appears underneath the subflow node. Very handy for troubleshooting and reading state without having to edit the subflow itself.

So for advanced toggle can I just feed it open/close events like from a door sensor or do I have to change the input payload.value to open/close and output command to same?

For simple toggle, why do you have two outputs in the sub-flow ? Why not a single output; after all both the commands go to the same command node?

I don't for the advanced toggle? Maybe you are seeing the status output? This was something I threw together so everyone's mileage may vary!

You should be able to do that - just define what you want in the input json and the output. PM me if you want!