element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Upcycle It
  • Challenges & Projects
  • Design Challenges
  • Upcycle It
  • More
  • Cancel
Upcycle It
Blog PDU #12-3 - Another Improvement in Function Consolidation [Upcycle It Challege]
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: jasonwier92
  • Date Created: 23 May 2017 10:23 PM Date Created
  • Views 729 views
  • Likes 3 likes
  • Comments 3 comments
  • node-red
  • upcycle it
  • upcycled_pdu
Related
Recommended

PDU #12-3 - Another Improvement in Function Consolidation [Upcycle It Challege]

jasonwier92
jasonwier92
23 May 2017

My outlet control is next on the list to fix up.

 

What it was: (can you say nasty)

image

 

What I need is a way to tell which kind of message came in.  Lets see what they look like.

image

 

So if I can test for the existence of msg._session and msg.topic, I should be able to know which message came in and where to send out out.  So I took my existing function and added a new section, based off the fact if it is a MQTT or WS message.  Here is my function code now.

 

if (msg._session) {
    var jMsg = JSON.parse(msg.payload);
    console.log(jMsg);
    var nMsg = { payload: "0" }
    if (jMsg.state == "on")
        nMsg.payload = "1";
        
    if (jMsg.outlet == "outlet1") {
        return [null,null,null,null,null,null,null,nMsg];
    }
    if (jMsg.outlet == "outlet2") {
        return [null,null,null,null,null,null,nMsg,null];
    }
    if (jMsg.outlet == "outlet3") {
        return [null,null,null,null,null,nMsg,null,null];
    }
    if (jMsg.outlet == "outlet4") {
        return [null,null,null,null,nMsg,null,null,null];
    }
    if (jMsg.outlet == "outlet5") {
        return [null,null,null,nMsg,null,null,null,null];
    }
    if (jMsg.outlet == "outlet6") {
        return [null,null,nMsg,null,null,null,null,null];
    }
    if (jMsg.outlet == "outlet7") {
        return [null,nMsg,null,null,null,null,null,null];
    }
    if (jMsg.outlet == "outlet8") {
        return [nMsg,null,null,null,null,null,null,null];
    }
}


if (msg.topic) {
    var topic = msg.topic;
    
    var nMsg = { payload: "0" }
    if (msg.payload == "ON")
        nMsg.payload = "1";
        
    if (topic == "tower/outlet0001/control") {
        return [null,null,null,null,null,null,null,nMsg];
    }
    if (topic == "tower/outlet0002/control") {
        return [null,null,null,null,null,null,nMsg,null];
    }
    if (topic == "tower/outlet0003/control") {
        return [null,null,null,null,null,nMsg,null,null];
    }
    if (topic == "tower/outlet0004/control") {
        return [null,null,null,null,nMsg,null,null,null];
    }
    if (topic == "tower/outlet0005/control") {
        return [null,null,null,nMsg,null,null,null,null];
    }
    if (topic == "tower/outlet0006/control") {
        return [null,null,nMsg,null,null,null,null,null];
    }
    if (topic == "tower/outlet0007/control") {
        return [null,nMsg,null,null,null,null,null,null];
    }
    if (topic == "tower/outlet0008/control") {
        return [nMsg,null,null,null,null,null,null,null];
    }
}

 

 

 

And now it looks like this.

 

image

 

 

So much better. In 12-2 I wondered if HASS would do MQTT JSON switches and the answer is no. Also Home Assistant can use Web Sockets, so you could use Node-Red for your Home Assistant automation.  This is nice, because HASS's automation is clunky, limited, and you have to reload the config after each change.

  • Sign in to reply

Top Comments

  • Workshopshed
    Workshopshed over 8 years ago +1
    Node-Red is powerful but a side effect of that is that there are many ways to solve any problems.
  • DAB
    DAB over 8 years ago +1
    One of the nice benefits to learning Object Oriented Analysis is that it trains your mind to think of these types of consolidation before you begin coding. Saves a lot of time in development and testing…
  • DAB
    DAB over 8 years ago

    One of the nice benefits to learning Object Oriented Analysis is that it trains your mind to think of these types of consolidation before you begin coding.

     

    Saves a lot of time in development and testing.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • konstantinoskonstas
    konstantinoskonstas over 8 years ago in reply to Workshopshed

    Cool stuff any way.

    But I was sure about it.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Workshopshed
    Workshopshed over 8 years ago

    Node-Red is powerful but a side effect of that is that there are many ways to solve any problems.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube