Hi all,
I just thought I'd post this to try and help those trying to get their heads around the new HS4 support in node red.
The HS4 documentation really doesn't give much away, and certainly doesn't help those new to JS and JSON control devices, so may be off-putting.
The HS4 device nodes do not accept a standard payload currently from what I can see. What do I mean by "standard"?
Well, take the output from the alexa remote node - if you control a light to turn on, you get:
Now, if you link that up to the device node of HS, that will be ignored, as the node doesn't handle msg.payload in that way for some reason. I've not deep dived into what the JSON is supported as there's only so much I really need to do, but it's not helpful for new people to use as the example is not very clear:
msg.payload = {} {"value":"255"}
In JS terms, what the above actually means is that HS device is expecting a msg.payload with a sub key of "value", and that has a value of "255"
or in other words:
msg.payload.value = 255
So you can do the example and literally hard code using a switch node or a trigger node, but I'd prefer if the HS4 devices were a bit more intelligent and handled on/off based on the usual msg.payload.
Alternatively, for those wanting to not have specific switch nodes, you can just use a function node. Here's an example to convert msg.payload to a value HS device nodes can work with. It's set up for clarity, not elegance:
Breakdown:
1. Set temp variable to msg.payload ("on" or "off")
2. Sets the payload variable to a JSON pair of "value":"0"
3 if temp = "on", set payload to "value":"99"
4 if temp = "off", set payload to "value":"0"
5 Send the msg object as msg.payload.value as 0 or 99
[/code]
Now, if your device needs 0 and 254, you just change the 99 to 254.
Here's the function to be cut/pasted into your node red instance:
Hope this helps, and it's a useful exercise into how the msg object is easily converted into what you need. Would welcome anyone else adding examples or tips like the above so people can really use node red to help integration with the many tech types in HA
I just thought I'd post this to try and help those trying to get their heads around the new HS4 support in node red.
The HS4 documentation really doesn't give much away, and certainly doesn't help those new to JS and JSON control devices, so may be off-putting.
The HS4 device nodes do not accept a standard payload currently from what I can see. What do I mean by "standard"?
Well, take the output from the alexa remote node - if you control a light to turn on, you get:
Code:
msg.payload = on msg.on = true msg.bri = 254 etc
msg.payload = {} {"value":"255"}
In JS terms, what the above actually means is that HS device is expecting a msg.payload with a sub key of "value", and that has a value of "255"
or in other words:
msg.payload.value = 255
So you can do the example and literally hard code using a switch node or a trigger node, but I'd prefer if the HS4 devices were a bit more intelligent and handled on/off based on the usual msg.payload.
Alternatively, for those wanting to not have specific switch nodes, you can just use a function node. Here's an example to convert msg.payload to a value HS device nodes can work with. It's set up for clarity, not elegance:
Code:
var temp = msg.payload; var payload = {value:0}; if (temp === "on") { payload = {value:99}; } if (temp === "off") { payload = {value:0}; } node.send({payload});
1. Set temp variable to msg.payload ("on" or "off")
2. Sets the payload variable to a JSON pair of "value":"0"
3 if temp = "on", set payload to "value":"99"
4 if temp = "off", set payload to "value":"0"
5 Send the msg object as msg.payload.value as 0 or 99
[/code]
Now, if your device needs 0 and 254, you just change the 99 to 254.
Here's the function to be cut/pasted into your node red instance:
Code:
[{"id":"9a59ac73.bc0e4","type":"tab","label":"Sample function for HS4 conversion","disabled":false,"info":""},{"id":"f2c31336.d0441","type":"function","z":"9a59ac73.bc0e4","name":"Convert payload to expected value","func":"var temp = msg.payload;\nvar payload = {value:0};\nif (temp === \"on\") {\n payload = {value:99};\n}\nif (temp === \"off\") {\n payload = {value:0};\n}\nnode.send({payload});","outputs":1,"noerr":0,"x":200,"y":60,"wires":[[]]}]

Comment