Announcement

Collapse
No announcement yet.

Help concatenating values and send to a Device Status in Node Red

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

    Help concatenating values and send to a Device Status in Node Red

    I am exploring multiple routes to get the location from a iphone to my HomeRemote app. I used to work with PHLocation that is no more supported. I am now looking both the MQTT route and the node red route. as I am not a developer I am struggling a lot when comes time to do some programming so please bare with me.

    I just installed Node-red and started playing with it and it seems to be an incredible power tool. I am trying to figure out how I can merge two values from a Node message and set a device status containing this cancatenated result.

    I am able to update a device status using Node-red, I at least figured that out but my problem is to merge values. Let say I have the following Message coming out of a Node: (extracted from a debud node)

    object
    iPhone: array[1]
    0: object
    modelName: "iPhone 12 Pro"
    deviceID: "/xyz="
    displayName: "iPhone12"
    batteryLevel: 82
    batteryState: "NotCharging"
    locationInfo: object
    altitude: 0
    latitude: 45.76989884492547
    longitude: -74.11944542087066
    locationTimeStamp: "2020-11-14 19:52"



    refreshTimeStamp: "2020-11-14 19:53:42"

    what would be the best way to create a msg.payload.status containing "latitude,longitude" ?

    ****** update *****
    I found a way, may be not the best but it works, I added a function node with the following:

    var lati1 = String(msg.payload.iPhone[0].locationInfo.latitude);
    var long1 = String(msg.payload.iPhone[0].locationInfo.longitude);
    msg.payload.status = lati1 + "," + long1;
    return msg;

    #2
    Function nodes should be your last resort. In your casr you can use a Change node and set the To field to be an Expression which users JSONata. The expression should be something such as
    Code:
    msg.payload.iPhone[0].locationInfo.latitude & ',' & msg.payload.iPhone[0].locationInfo.longitude
    Use & to concatenate strings on a JSONata expression.


    The Change node will look like this. I am using the Node Red editor Android app called RED Client Editor so the expression field is cut off.
    Click image for larger version

Name:	Screenshot_20201114-173336.png
Views:	119
Size:	70.7 KB
ID:	1433457
    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


      #3
      ksum thanks for this, just curious, why should function nodes be my last resort ?

      Comment


        #4
        The Function node runs in a pseudo vm environment and is therefor slower. Anything you can do with standard nodes is faster. Most of the veteran Node Red users will tell you to avoid it if possible.
        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