Skip to content

Instantly share code, notes, and snippets.

@JamiePhonic
Last active Jun 30, 2020
Embed
What would you like to do?
Homer dashboard dynamic message

This small flow allows you to display a News RSS feed item as the message in Homer

Simply load this flow, then update your Homer config with this block:

message:
  url: "https://{Node-Red URL}/dashboard-endpoint"
  style: "is-danger"
  title: "Node Red"
  content: "Couldn't get dynamic data from Node Red!"

Now, every time you load Homer, a random news item from your chosen news feed (change it by updating the feed URL in the Get News RSS Feed node) will be displayed as the message at the top of the page, including a link to "read more"! If something goes wrong and the flow fails to return anything, the message in Homer will default to Couldn't get dynamic data from Node Red! so you know something's wrong.

Notes:

  1. The RSS feed is queried every 30 minutes and stored in flow.feed This minimises the number of requests made to the RSS feed source by essentially caching it for 30 minutes.
  2. The flow will return a random news item from the feed each time a request is made to {node-red-URL}/dashboard-endpoint
  3. If your Node-Red instance is public and secured by SSL, you will need add the Access-Control-Allow-Origin header to responces from node-red, supplying the URL of your Homer Dashboard.
  • For example, in HAProxy, add http-response set-header Access-Control-Allow-Origin https://homer.domain.tld to your Node-Red backend.
  1. If you're still having issues, check the console in your browsers developer tools for errors.
[{"id":"8ceb400f.e4da4","type":"tab","label":"Homer Dashboard","disabled":false,"info":""},{"id":"eebaee8e.d885e","type":"http in","z":"8ceb400f.e4da4","name":"","url":"/dashboard-endpoint","method":"get","upload":false,"swaggerDoc":"","x":170,"y":140,"wires":[["9bf1665f.3bb228"]]},{"id":"6c25115a.9c2d58","type":"http response","z":"8ceb400f.e4da4","name":"Return JSON Response","statusCode":"200","headers":{},"x":1190,"y":140,"wires":[]},{"id":"9c7df34e.67a9e","type":"template","z":"8ceb400f.e4da4","name":"Format Response","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n \"style\": \"{{style}}\",\n \"title\": \"{{topic}}\",\n \"content\": \"<b>{{{payload.title.0}}}</b></br>{{payload.description.0}}</br><a href={{payload.link.0}} target=_blank>Read More</a>\"\n}","output":"json","x":930,"y":140,"wires":[["6c25115a.9c2d58","73092206.fae644"]]},{"id":"a615b5ae.4e1cf8","type":"http request","z":"8ceb400f.e4da4","name":"Get News RSS Feed","method":"GET","ret":"txt","paytoqs":false,"url":"http://feeds.bbci.co.uk/news/scotland/rss.xml","tls":"","proxy":"","authType":"","x":420,"y":60,"wires":[["d883bef1.812ff"]]},{"id":"d883bef1.812ff","type":"xml","z":"8ceb400f.e4da4","name":"Convert XML to JSON","property":"payload","attr":"","chr":"","x":700,"y":60,"wires":[["41772ef5.9fc8a8"]]},{"id":"9bf1665f.3bb228","type":"change","z":"8ceb400f.e4da4","name":"Set style, Topic and Payload","rules":[{"t":"set","p":"style","pt":"msg","to":"is-success","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"feed.rss.channel[0].title[0]","tot":"flow"},{"t":"set","p":"payload","pt":"msg","to":"feed.rss.channel[0].item","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":140,"wires":[["77db7a35.7a746c"]]},{"id":"d81cae44.241e3","type":"inject","z":"8ceb400f.e4da4","name":"","topic":"Every 30 Minutes","payload":"true","payloadType":"bool","repeat":"1800","crontab":"","once":true,"onceDelay":"1","x":170,"y":60,"wires":[["a615b5ae.4e1cf8"]]},{"id":"41772ef5.9fc8a8","type":"change","z":"8ceb400f.e4da4","name":"Store in flow.feed","rules":[{"t":"move","p":"payload","pt":"msg","to":"feed","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":950,"y":60,"wires":[[]]},{"id":"73092206.fae644","type":"debug","z":"8ceb400f.e4da4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1130,"y":100,"wires":[]},{"id":"77db7a35.7a746c","type":"function","z":"8ceb400f.e4da4","name":"Pick Random Item","func":"var length = msg.payload.length\nvar random = Math.floor(Math.random() * length);\n\nmsg.random = random\nmsg.payload = msg.payload[random]\n\nreturn msg;","outputs":1,"noerr":0,"x":690,"y":140,"wires":[["9c7df34e.67a9e"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment