Announcement

Collapse
No announcement yet.

Learning Node-Red and MQTT

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Learning Node-Red and MQTT

    I'm trying to write my first node-red to learn how to do it. I would like to, based on Blue Iris MQTT, turn on or off the studio light. I put some examples there, not sure how to do that, any help, would be great.
    Attached Files

    #2
    At the bottom right of your screenshot, be sure to choose the root device, then you should see the feature in the dropdown...
    HS4Pro on a Raspberry Pi4
    54 Z-Wave Nodes / 21 Zigbee Devices / 108 Events / 767 Devices
    Plugins: Z-Wave / Zigbee Plus / EasyTrigger / AK Weather / OMNI

    HSTouch Clients: 1 Android

    Comment


      #3
      Thanks for the fast reply. The light Studio Light came from HS3, it has its on root.
      In regards to MQTT, how do I link it to homeSeer so I can turn the light on or off based on BlueIris?
      Thanks
      Attached Files

      Comment


        #4
        Add two Change Nodes and edit properties to trigger/update the HSNode, Image below (example) :


        Click image for larger version  Name:	Change Nodes.png Views:	0 Size:	84.8 KB ID:	1521310

        More on how to configure the HSNodes : https://github.com/HomeSeer/node-red-contrib-homeseer

        Note : The Switch Node can contain multiple states you wish to trigger (in your case, Off and On). So whatever your desire or what you wish to see in HomeSeer is edited in the change Node which updates the HSNode...


        The above is for dealing with general devices but if they are cameras you are dealing with you can skip all that and use OnVif : https://forums.homeseer.com/forum/ho...08#post1433108



        Eman.
        TinkerLand : Life's Choices,"No One Size Fits All"

        Comment


          #5
          Thanks for your help. please be patient with me, it is all new to me. I had some exposure to it but making small progress. This is what I have so far. The goal for now is for BlueIris to trigger via MQTT the door to open or close. I appreciate any help you can give.
          Attached Files

          Comment


            #6
            I would not use JSON for setting the payload. JSON is best used to share variables between programs. The Node-RED message msg is an Object, which is a series of named variables, including other Objects, contained inside a single variable. The problem becomes when msg.payload is not an object, but a different variable type. In this case you need to first either Delete msg.payload or Move it to a new name. I Move it to a new name so that I still have the old data. In the case of an MQTT message, the output message from an MQTT node has msg.topic and msg.payload defined, as shown in the image below showing the complete message from a Debug node. msg.qos and msg.retain are also set.
            Click image for larger version

Name:	HS1.png
Views:	380
Size:	12.8 KB
ID:	1521576



            Since msg.payload is set to a String (as noted with the quotes around the value) I move msg.payload to msg.mqtt.payload and can then use that farther down the thread. This can be done in each of your Change nodes. It is important to note here that msg.payload is subject to change by any node, so any data which you may want later is best moved out of this location. msg,topic is the same, so I also save that to msg.mqtt.topic.

            In your last image in post #5, you are setting msg.payload.trigger to a String containing the number 0. You are also changing msg.topic to the string update. These are both done in the image below, as well as the above steps to Move the MQTT data.
            Click image for larger version

Name:	HS2.png
Views:	499
Size:	28.6 KB
ID:	1521575



            This then sets the message going out of the Change to the values shown in the image below. Inj the image note that:
            • msg.payload is now an Object with one key item, trigger
            • msg.payload.trigger is set to a String with a value of 0
            • msg.topic is set to a String with a value of update
            • msg.mqtt is a new Object with 2 key items
            • msg.mqtt.payload has the old MQTT message's payload
            • msg.mqtt.topic has the MQTT messages topic

            Click image for larger version

Name:	HS3.png
Views:	373
Size:	15.3 KB
ID:	1521577




            NOTE that I do not have locks, but I believe to Lock or Unlock the door, you need to pass a value to msg.payload.status equal to the appropriate value for the Status of the HomeSeer device, OR set msg.payload.value to the numeric HomeSeer device Value. So if the HomeSeer Value is 1 or 0 and the MQTT payload is either 1 or 0, then you do not need a Switch node, but you would use these to set msg.payload.value. msg.payload.value can be a string or a number, so no conversion is necessary.

            All of this would change your flow to have 3 nodes. A fourth is you wish to include a Debug node after the HomeSeer node, but it is not necessary. In the flow below you could use the Change node as shown below and replace the Debug node with your HomeSeer device. Note that, depending on the version of Node-RED you have installed, you may not have the Deep copy value option as shown in the Change node image. If you have the luxury of setting the MQTT message, just be sure to set the MQTT payload to the Value HomeSeer uses for the lock. We then use this to define msg.payload.value and you do not need the Switch node.
            Click image for larger version

Name:	HS4.png
Views:	375
Size:	9.5 KB
ID:	1521578

            Click image for larger version

Name:	HS5.png
Views:	386
Size:	30.0 KB
ID:	1521579



            Karl S
            HS4Pro on Windows 10
            1070 Devices
            56 Z-Wave Nodes
            104 Events
            HSTouch Clients: 3 Android, 1 iOS
            Google Home: 3 Mini units, 1 Pair Audios, 2 Displays

            Comment


              #7
              PS: In my post above, the final message going out of the Change node will look like this:
              Click image for larger version

Name:	HS1.png
Views:	384
Size:	15.4 KB
ID:	1521581

              Karl S
              HS4Pro on Windows 10
              1070 Devices
              56 Z-Wave Nodes
              104 Events
              HSTouch Clients: 3 Android, 1 iOS
              Google Home: 3 Mini units, 1 Pair Audios, 2 Displays

              Comment


                #8
                This forum is incredible, I appreciate everyone help. Right now, everything looks like another language to me and confusing but I will try to re-read it and see if I can make any sense of it.

                Comment


                  #9
                  Originally posted by ksum View Post
                  PS: In my post above, the final message going out of the Change node will look like this:
                  Click image for larger version  Name:	HS1.png Views:	22 Size:	15.4 KB ID:	1521581


                  ksum ,

                  The Change Node can covert any incoming json and/or topic directly without extra processing...

                  Example in the images below :


                  Click image for larger version  Name:	Change node 1.png Views:	0 Size:	143.9 KB ID:	1522249


                  Click image for larger version

Name:	Change Node 2.png
Views:	409
Size:	104.6 KB
ID:	1522252





                  Eman.
                  TinkerLand : Life's Choices,"No One Size Fits All"

                  Comment


                    #10
                    Originally posted by Eman View Post

                    The Change Node can covert any incoming json and/or topic directly without extra processing...

                    Eman.
                    Yes, but using JSON makes it difficult to see what is happening in Node-RED without opening the JSON editor. Especially when setting many items. Additionally, you cannot use JSON to define the value of a specific key to another variable. Meaning if I want to set msg.payload.incomingtopic to the incoming value of msg.topic, I cannot do that with JSON. I have to use some other method, namely JSONata. But if you use the dot notation and set the individual parts as I have, you can use this:
                    Click image for larger version

Name:	HS1.png
Views:	407
Size:	6.9 KB
ID:	1522267

                    This keeps things consistent for hard coding a value or using another variable. Yes, IF the incoming payload is not an Object, I have to first wither Move or Delete the payload. Again, this is directly visible and fairly clean. But it remains consistent and keeps us from using the more difficult to understand JSONata to dynamically define a value. Taking this a step further, I can use the same method to dynamically define a key. If the incoming msg.topic is set to "myTopic" then a Change node with the setting shown below will set msg.payload.myTopic = "hello"
                    Click image for larger version

Name:	HS2.png
Views:	358
Size:	5.3 KB
ID:	1522268



                    There should be no performance issue by setting each Object item individually as I do in my examples. I can foresee there being a performance hit (yes, minor - not tested) in converting the JSON to the Object as there is an additional step for the conversion. Setting an object's property by defining the key as I show is the equivalent of a line of code:
                    Code:
                    objectName.key = 'value';
                    or to dynamically set them as in my examples in this message:
                    Code:
                    msg.payload.incomingtopic = msg.topic;
                    msg.payload[msg.topic] = 'hello';
                    Doing this with JSON is the equivalent of running a Function which adds additional code even though it is hidden:
                    Code:
                    objectName = JSON.parse('{"key":"value"}');

                    JSON is great for moving variables between programs, sending variables to a downloaded web page to be parsed and displayed by the local browser, or saving the variables to a file. But I avoid using them in my code, be it Node-RED, JavaScript, or otherwise.

                    Especially when learning, it is good to understand the Object's structure. The best visualization of the object is something such as shown in the Debug node's output, but the Change and other nodes to set variables do not have that. Setting each key's value in an object using the Change node as I do gives a direct view of what is being set. It keeps things consistent with regards to setting a hard coded or dynamic value, and allows you to also dynamically define the keys to an object's property.

                    Ultimately whichever method you use is up to you. That is the beauty of something such as this.
                    Karl S
                    HS4Pro on Windows 10
                    1070 Devices
                    56 Z-Wave Nodes
                    104 Events
                    HSTouch Clients: 3 Android, 1 iOS
                    Google Home: 3 Mini units, 1 Pair Audios, 2 Displays

                    Comment


                      #11
                      Don't you think that people/new eyes are thrown off when they look at how you break down that incoming message with that Change Node ?
                      To keep things really simple, that's where the Switch Node comes in...
                      • Looking at the first/on top image in my post at #9, It's the job of the Switch Node to filter/sort all incoming messages before passing them on to the Change Nodes which you then use to push/update the HSDevice Nodes
                      • The incoming messages hitting the Switch Node can come from all angles that are plain text or json (It doesn't matter). All you choose in the Switch Node is the "contains" as the filter and then pass the message on to the Change Nodes...
                      • Remember the HSDevice Node we are trying to Update will possess/acquire many topics we need to see when they reach Homeseer and thus used as the triggers for the intended events
                      The image below illustrates all types of messages but the magic bullet/filter is the "contains" in Switch Node before they can be passed on...

                      Click image for larger version  Name:	Switch Node.png Views:	0 Size:	190.7 KB ID:	1522346




                      Click image for larger version  Name:	NEEO.png Views:	0 Size:	23.2 KB ID:	1522347



                      Eman.
                      TinkerLand : Life's Choices,"No One Size Fits All"

                      Comment


                        #12
                        In the original post, the MQTT node is subscribed to a specific Topic. I believe it is set something such as shown here, although the QoS setting may be different and of course the Server will be theirs. Later the "/Status" portion is dropped, but that is also dropped in their Blue Iris setup, so they are still filtering the message:
                        Click image for larger version

Name:	HS1.png
Views:	379
Size:	22.2 KB
ID:	1522364


                        So we have already filtered the incoming items to be a specific MQTT message, the one we wish to act on. An MQTT Message Payload is only ever shown to be "0" and as a result, in my original post, I stated:
                        NOTE that I do not have locks, but I believe to Lock or Unlock the door, you need to pass a value to msg.payload.status equal to the appropriate value for the Status of the HomeSeer device, OR set msg.payload.value to the numeric HomeSeer device Value. So if the HomeSeer Value is 1 or 0 and the MQTT payload is either 1 or 0, then you do not need a Switch node, but you would use these to set msg.payload.value. msg.payload.value can be a string or a number, so no conversion is necessary.

                        So in this case we are looking at a specific/filtered message already meaning there is no need for the Switch node PROVIDED the MQTT payload is set to a Value which HomeSeer will use to control the device. IF the MQTT payload is not the Value which the HomeSeer node will use, then yes, a Switch node is needed based on the MQTT Payload to then use a Change node to hard code the Value sent to the HomeSeer node. The original poster has the ability to set this in Blue Iris so if it is not supposed to be a Value of 0, they can change that in Blue Iris. I thought I had stated that the MQTT Payload sent in my suggestion should be the same as the desired HomeSeer Device's resultant Value, but maybe I was not.

                        In Blue Iris, the OP can now send either a 0 or a 1/100/255, or whatever Value is needed to set the HomeSeer device to the desired setting and the 3 node flow will work. If they send other values, then a Switch node will be needed to set msg.payload.value to the desired the HomeSeer Device Value. But unless the MQTT node is set with a wildcard, it is only receiving these specific messages and the flow has nothing else coming in. MQTT wildcards are either # or + and there images do not show these.
                        Karl S
                        HS4Pro on Windows 10
                        1070 Devices
                        56 Z-Wave Nodes
                        104 Events
                        HSTouch Clients: 3 Android, 1 iOS
                        Google Home: 3 Mini units, 1 Pair Audios, 2 Displays

                        Comment


                          #13
                          I know I keep saying it but best people and forum ever. A love the way each one approach things different, so we can have different views. As stated in my first post, I'm doing this way so I can learn node-red, It is very unfamiliar to me, I have some nodes but mostly from other people. My objecting was to start with hello world concept. Use MQTT from blue iris, right now I use JSON without issues and it will be the way I would do stuff. Node red was a way for me to learn MQTT, passing it to homeseer and turn on a light so I can learn the process. I will read again the entire post and I'm sure I will find lot of great examples. In addition to it, any good youtube or books, videos that can help us starting with MQTT and node-red? and of course, I don't want to abuse but if you give us more example using our zero knowledge will be great. Please keep posting

                          Sent from my SM-G973U1 using Tapatalk

                          Comment


                            #14
                            After writing my last reply I was thinking that the receipt of an MQTT message and then changing a HomeSeer device was a good learning tool for Node-RED. My first reaction was "Just use one of the MQTT Plugins for HomeSeer." I still subscribe to this and the use of HomeSeer Events if possible. But as a learning too, this does work well.

                            As for tutorials on MQTT, this Beginner's Guide is better than what I used, which was a combination of a few for straight MQTT information. For use or training on Node-RED, I like the Opto Video series. Their How to Get Started with MQTT video is good as are the rest of their Node-RED videos. Note that many tutorials may show doing things with the Function Node. This is done to simplify and focus on the other parts of the tutorial and the nodes specific to that. Unfortunately this results is many people over-using the Function Node. If you can do it with another node, as in you are just changing or setting variables, i.e. the Change Node, then use the appropriate node. There are many reasons, but primarily, Function nodes are slower and more resource intensive.
                            Karl S
                            HS4Pro on Windows 10
                            1070 Devices
                            56 Z-Wave Nodes
                            104 Events
                            HSTouch Clients: 3 Android, 1 iOS
                            Google Home: 3 Mini units, 1 Pair Audios, 2 Displays

                            Comment

                            Working...
                            X