Vega-Lite: Adding "All" option in select which doesn't filter the chart - vega-lite

I am trying to implement a drop-down select where if the user selects "All" it would simply not filter and show all data. So far, I have added the select options and the labels.
Here is my code (simplified for visibility)
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"config": {
"bar": {
"height": 30,
"tooltip":true
}
},
"width": 1000,
"height": 600,
"background":"#dddddd",
"title":"Top 10 Richest by Industry",
"data": {
"url": "https://raw.githubusercontent.com/edenfrey/FIT3179_Visualisation2_Globe500/main/data/Billionaires_October_13_2022.csv"
},
"params": [
{
"name": "industry_sel",
"value": "Technology",
"bind": {
"input": "select",
"options": [
null,
"Technology",
"Industrial",
"Diversified",
"Finance",
"Consumer",
"Retail",
"Food & Beverage",
"Energy",
"Real Estate",
"Health Care",
"Commodities",
"Media & Telecom",
"Entertainment",
"Services"
],
"labels": [
"All",
"Technology",
"Industrial",
"Diversified",
"Finance",
"Consumer",
"Retail",
"Food & Beverage",
"Energy",
"Real Estate",
"Health Care",
"Commodities",
"Media & Telecom",
"Entertainment",
"Services"],
"name": "Industry: "
}
}
],
"layer": [
{
"mark": "bar"
}
],
"encoding": {
"y": {"field": "Name", "type": "nominal","sort": {"op": "sum", "field": "Net Worth", "order":"descending"}},
"x": {"field": "Net Worth", "type": "quantitative", "title": "Net Worth in Billions"}
},
"transform": [
{
"filter": {"field": "Industry", "equal": {"expr": "industry_sel"}}
}
]
}
So far, when the user selects "All" it will simply show nothing (because there is no Industry with null as a value. Furthermore, I wish to only add and change things in this code rather than add other files or hacks (This is an assignment. The criteria is quite strict)

Do it with a ternary operator and make sure you enclose null in quotes.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"config": {
"bar": {
"height": 30,
"tooltip":true
}
},
"width": 1000,
"height": 600,
"background":"#dddddd",
"title":"Top 10 Richest by Industry",
"data": {
"url": "https://raw.githubusercontent.com/edenfrey/FIT3179_Visualisation2_Globe500/main/data/Billionaires_October_13_2022.csv"
},
"params": [
{
"name": "industry_sel",
"value": "Technology",
"bind": {
"input": "select",
"options": [
"null",
"Technology",
"Industrial",
"Diversified",
"Finance",
"Consumer",
"Retail",
"Food & Beverage",
"Energy",
"Real Estate",
"Health Care",
"Commodities",
"Media & Telecom",
"Entertainment",
"Services"
],
"labels": [
"All",
"Technology",
"Industrial",
"Diversified",
"Finance",
"Consumer",
"Retail",
"Food & Beverage",
"Energy",
"Real Estate",
"Health Care",
"Commodities",
"Media & Telecom",
"Entertainment",
"Services"],
"name": "Industry: "
}
}
],
"layer": [
{
"mark": "bar"
}
],
"encoding": {
"y": {"field": "Name", "type": "nominal","sort": {"op": "sum", "field": "Net Worth", "order":"descending"}},
"x": {"field": "Net Worth", "type": "quantitative", "title": "Net Worth in Billions"}
},
"transform": [
{
"filter": "industry_sel=='null'?industry_sel:datum.Industry == industry_sel"
}
]
}

After looking at David's solution and discovering a set back on a functionality that after choosing a specific industry and going back to "All" would not change back, I have come up with a similar solution to David's.
Code is as follows:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"config": {
"bar": {
"height": 30,
"tooltip":true
}
},
"width": 1000,
"height": 600,
"background":"#dddddd",
"title":"Top 10 Richest by Industry",
"data": {
"url": "https://raw.githubusercontent.com/edenfrey/FIT3179_Visualisation2_Globe500/main/data/Billionaires_October_13_2022.csv"
},
"params": [
{
"name": "industry_sel",
"value": "Technology",
"bind": {
"input": "select",
"options": [
"null",
"Technology",
"Industrial",
"Diversified",
"Finance",
"Consumer",
"Retail",
"Food & Beverage",
"Energy",
"Real Estate",
"Health Care",
"Commodities",
"Media & Telecom",
"Entertainment",
"Services"
],
"labels": [
"All",
"Technology",
"Industrial",
"Diversified",
"Finance",
"Consumer",
"Retail",
"Food & Beverage",
"Energy",
"Real Estate",
"Health Care",
"Commodities",
"Media & Telecom",
"Entertainment",
"Services"],
"name": "Industry: "
}
}
],
"layer": [
{
"mark": "bar"
}
],
"encoding": {
"y": {"field": "Name", "type": "nominal","sort": {"op": "sum", "field": "Net Worth", "order":"descending"}},
"x": {"field": "Net Worth", "type": "quantitative", "title": "Net Worth in Billions"}
},
"transform": [
{
"window": [{"op": "rank", "as" : "rank"}]
},
{
"filter": "industry_sel=='null'?datum.rank <= 10:datum.Industry == industry_sel"
}
]
}
From my understanding on how it works is that it will show the top 10 of the (sorted) data no matter the industry choice. This code now works. For the full code and context you can check it out here:
https://raw.githubusercontent.com/edenfrey/FIT3179_Visualisation2_Globe500/main/js/Top_10_Billionaires.vg.json

Related

Vegalite Keep Most Recent Data (Filter Transform)

I have the following dashboard:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "Domain Breakout Breakout",
"width": "container",
"title": {
"text": ["Build number by service name"],
"align": "center",
"dy": -10,
"fontWeight": "bold",
"color": "#4f597a",
"fontSize": 13,
"font": "Montserrat,sans-serif"
},
"config": {"axis": {"grid": true, "tickBand": "extent"}},
"data": {
"values": [
{
"service": "service1",
"build": 5555,
"env": "dev"
},
{
"service": "service2",
"build": 5555,
"env": "test"
},
{
"service": "service3",
"build": 5555,
"env": "staging"
},
{
"service": "service4",
"build": 5555,
"env": "prod"
},
{
"service": "service4",
"build": 5225,
"env": "prod"
}
]
},
"transform": [
{"pivot": "env", "value": "build", "groupby": ["env", "service","build"]}
],
"mark": "text",
"encoding": {
"x": {
"field": "env",
"type": "ordinal",
"sort": "descending",
"axis": {
"title": null,
"labelAngle": 0,
"labelFontWeight": "bold",
"labelColor": "#4f597a",
"labelFontSize": 20,
"labelPadding": 20,
"orient": "top"
}
},
"y": {
"field": "service",
"type": "ordinal",
"sort": {"field": "service", "order": "descending", "op": "sum"},
"axis": {
"title": null,
"labelAngle": 0,
"labelFontWeight": "bold",
"labelColor": "#4f597a",
"labelFontSize": 10,
"labelPadding": 5
}
}
},
"layer": [
{
"mark": "rect",
"width": 1000,
"encoding": {
"fill": {
"legend": null,
"field": "build",
"type": "quantitative",
"scale": {"range": ["#ecf9ff", "#c6efff", "#7ad9ff", "#42caff"]}
}
}
},
{
"mark": {
"type": "text",
"fontSize": 8,
"font": "Montserrat,sans-serif",
"align": "center"
},
"encoding": {"text": {"field": "build"}}
}
]
}
I have a use case in which new data is inserted with the same service name and env name but with a different build number. In this case:
{
"service": "service4",
"build": 5555,
"env": "prod"
},
{
"service": "service4",
"build": 5558,
"env": "prod"
}
The issue is the cell [service4][prod] has to layer the "old" value and the new value.
Is there an option in vega to get the last inseted value or something similar?
Link the dash: Link
You can use a combination of joinaggregate and filter transform to get the max of build number. i.e. it will filter out the lower values. e.g.
{
"joinaggregate": [{"op": "max", "field": "build", "as": "MaxBuild"}],
"groupby": ["service"]
},
{
"filter":"datum.build >= datum.MaxBuild"
}
Editor

Slider for chloropeth map Vega-lite

I am trying to add a slider for my chloropeth map of Europe in vega-lite, to filter the data by year. I currently have a map which just shows data from 2019 (colour coded), and I am trying to make a slider so I can change years and see how the colours have changed over time.
Here is my code so far:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": 300,
"height": 300,
"data": {
"url": "https://raw.githubusercontent.com/leakyMirror/map-of-europe/master/TopoJSON/europe.topojson",
"format": {"type": "topojson", "feature": "europe"}
},
"transform": [
{
"lookup": "properties.NAME",
"from": {
"data": {
"url": "https://raw.githubusercontent.com//jamesjeffery77/jamesjeffery77.github.io/main/share-electricity-low-carbon_fullDataset.csv"
},
"key": "country",
"fields": ["percentage"]
}
}
],
"params": [
{
"name": "year",
"value": 2019,
"bind": {
"input": "range",
"min": 1985,
"max": 2019,
"step": 1,
"name": "Select the year:"
}
}
],
"projection": {"type": "naturalEarth1"},
"mark": "geoshape",
"encoding": {
"color": {
"field": "percentage",
"type": "quantitative"},
"tooltip": [
{"field": "properties.NAME", "type": "nominal", "title": "country"},
{"field": "percentage", "type": "quantitative"}
]
}
}
I have been able to do make a bar chart using the same data which updates as I move the slider.The {"filter": "datum.year==year"} is what makes my bar chart able to do this, however it does not work on my chloropeth map (I have tried to add this within the "transform" array in both, with success for my bar chart). Here is the code for my bar chart in case that helps.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "",
"title": {
"text": "Thisisatest",
"subtitle":
"hellohello Source: OurWorldInData",
"subtitleFontStyle": "italic",
"subtitleFontSize": 10,
"anchor": "start",
"color": "black"
},
"data": {
"url": "https://raw.githubusercontent.com//jamesjeffery77/jamesjeffery77.github.io/main/share-electricity-low-carbon_fullDataset.csv"
},
"height": 300,
"width": 350,
"mark": {"type": "bar", "color": "skyblue"},
"transform": [
{"filter": "datum.year==year"},
{"filter": {
"field": "country",
"oneOf": [
"United Kingdom", "Spain", "France", "Netherlands", "Portugal", "Italy", "Poland", "Albania", "Germany", "Belgium", "Austria", "Denmark"]}
}
],
"params": [
{
"name": "year",
"value": 2019,
"bind": {
"input": "range",
"min": 1985,
"max": 2019,
"step": 1,
"name": "Select the year:"
}
}
],
"encoding": {
"y": {
"field": "percentage",
"type": "quantitative",
"title": "Percentage of low carbon energy",
"axis": {"grid": false}
},
"x": {
"field": "country",
"type": "nominal",
"title": "",
"axis": {"grid": false, "labelAngle": 20},
"sort": "-y"
},
"tooltip": [
{"field": "country", "title": "Country"},
{"field": "percentage", "title": "percentage of low carbon energy"}
]
}
}
What am I am doing wrong? Would appreciate any help! :)
Thanks
It was almost correct but in your lookup transform you need to provide the fields which you are going to be required in your final data like year field. Since there was no year field the filter transform was not working.
Below is the modified config or refer editor:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": 300,
"height": 300,
"data": {
"url": "https://raw.githubusercontent.com//jamesjeffery77/jamesjeffery77.github.io/main/share-electricity-low-carbon_fullDataset.csv"
},
"transform": [
{
"lookup": "country",
"from": {
"data": {
"url": "https://raw.githubusercontent.com/leakyMirror/map-of-europe/master/TopoJSON/europe.topojson",
"format": {"type": "topojson", "feature": "europe"}
},
"key": "properties.NAME",
"fields": ["properties", "type", "geometry"]
}
},
{"filter": "datum.year==year"}
],
"params": [
{
"name": "year",
"value": 2019,
"bind": {
"input": "range",
"min": 1985,
"max": 2030,
"step": 1,
"name": "Select the year:"
}
}
],
"projection": {"type": "naturalEarth1"},
"mark": "geoshape",
"encoding": {
"color": {"field": "percentage", "type": "quantitative"},
"tooltip": [
{"field": "properties.NAME", "type": "nominal", "title": "country"},
{"field": "percentage", "type": "quantitative"}
]
}
}
Edit
I have inverted the main data and lookup data, which seems to bring all the years for your countries. Let me know if this works.

How to change Shape and Color of points in Vega Scatter Plot?

I have a scatter plot generated with contours code in Vega.
Plot looks like
Based on 3rd field, differentiated the points with colour as Blue and Green Dots, but couldn't see the difference very clearly.
Is it possible to Change the Shape and Colour (atleast one) of the points to make the difference more visible
Code
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"title": {
"text": "Outlier Distribution between Duration Vs Age",
"anchor": "middle",
"fontSize": 16,
"frame": "group",
"offset": 4
},
"signals": [
{
"name": "bandwidth", "value": 0.5,
"bind": {"input": "range", "min": -1, "max": 1, "step": 0.1}
}
],
"data": [
{
"name": "source",
"url" : {
"index": "tenant_id.model_training_artefact",
"body": {
"size":10000,
"_source": ["duration", "credit_amount", "asnm", "age", "outlier"]
}
}
"format": {"property": "hits.hits"},
},
{
"name": "density",
"source": "source",
"transform": [
{
"type": "kde2d",
"groupby": ["_source.outlier"],
"size": [{"signal": "width"}, {"signal": "height"}],
"x": {"expr": "scale('x', datum.duration)"},
"y": {"expr": "scale('y', datum.age)"},
"bandwidth": {"signal": "[bandwidth, bandwidth]"}
}
]
},
{
"name": "contours",
"source": "density",
"transform": [
{
"type": "isocontour",
"field": "grid",
"levels": 4
}
]
}
],
"scales": [
{
"name": "x",
"type": "linear",
"round": true,
"nice": true,
"zero": true,
"domain": {"data": "source", "field": "_source.duration"},
"range": "width"
},
{
"name": "y",
"type": "linear",
"round": true,
"nice": true,
"zero": true,
"domain": {"data": "source", "field": "_source.age"},
"range": "height"
},
{
"name": "color",
"type": "ordinal",
"domain": {
"data": "source", "field": "_source.outlier",
"sort": {"order": "descending"}
},
"range": "category"
}
],
"axes": [
{
"scale": "x",
"grid": true,
"domain": false,
"orient": "bottom",
"tickCount": 5,
"title": "Duration"
},
{
"scale": "y",
"grid": true,
"domain": false,
"orient": "left",
"titlePadding": 5,
"title": "Age"
}
],
"legends": [
{"stroke": "color", "symbolType": "stroke"}
],
"marks": [
{
"name": "marks",
"type": "symbol",
"from": {"data": "source"},
"encode": {
"update": {
"x": {"scale": "x", "field": "_source.duration"},
"y": {"scale": "y", "field": "_source.age"},
"size": {"value": 50},
"fill": {"scale": "color" , "field": "_source.outlier"}
}
}
},
{
"type": "image",
"from": {"data": "density"},
"encode": {
"update": {
"x": {"value": 0},
"y": {"value": 0},
"width": {"signal": "width"},
"height": {"signal": "height"},
"aspect": {"value": false}
}
},
"transform": [
{
"type": "heatmap",
"field": "datum.grid",
"color": {"expr": "scale('color', datum.datum.outlier)"}
}
]
},
{
"type": "path",
"clip": true,
"from": {"data": "contours"},
"encode": {
"enter": {
"strokeWidth": {"value": 1},
"strokeOpacity": {"value": 1},
"stroke": {"scale": "color", "field": "outlier"}
}
},
"transform": [
{ "type": "geopath", "field": "datum.contour" }
]
}
]
}
Try defining scales for mark symbol color, shape and size.
For example, assuming your field outlier has 3 possible values:
"scales": [
...
{
"name": "scale_symbol_color",
"type": "ordinal",
"domain": {
"data": "source", "field": "_source.outlier",
"sort": {"order": "descending"}
},
"range": ["red", "orange", "blue"]
},
{
"name": "scale_symbol_shape",
"type": "ordinal",
"domain": {
"data": "source", "field": "_source.outlier",
"sort": {"order": "descending"}
},
"range": ["triangle", "square", "circle"]
},
{
"name": "scale_symbol_size",
"type": "ordinal",
"domain": {
"data": "source", "field": "_source.outlier",
"sort": {"order": "descending"}
},
"range": [400, 300, 200]
}
],
...
"marks": [
{
"name": "marks",
"type": "symbol",
"from": {"data": "source"},
"encode": {
"update": {
"x": {"scale": "x", "field": "_source.duration"},
"y": {"scale": "y", "field": "_source.age"},
"size": {"value": 50},
"fill": {"scale": "scale_symbol_color" , "field": "_source.outlier"},
"shape": {"scale": "scale_symbol_shape" , "field": "_source.outlier"},
"size": {"scale": "scale_symbol_size" , "field": "_source.outlier"},
}
}
},
...
Vega docs:
https://vega.github.io/vega/docs/scales/#ordinal
https://vega.github.io/vega/docs/marks/symbol/

Adaptive cards version 1.3

Can anyone tell me how can we use adaptive card version 1.3, When I'm using any card with version 1.3 and viewing it in-app studio it says?
Tried an adaptive card of version 1.3 both in App Studio and MS Teams and it worked fine.Could you please try upgrading your version and check if it works. Attaching the json below which i tested:
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.3",
"body": [
{
"speak": "Tom's Pie is a pizza restaurant which is rated 9.3 by customers.",
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": 2,
"items": [
{
"type": "TextBlock",
"text": "PIZZA"
},
{
"type": "TextBlock",
"text": "Tom's Pie",
"weight": "bolder",
"size": "extraLarge",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "4.2 ★★★☆ (93) · $$",
"isSubtle": true,
"spacing": "none"
},
{
"type": "TextBlock",
"text": "**Matt H. said** \"I'm compelled to give this place 5 stars due to the number of times I've chosen to eat here this past year!\"",
"size": "small",
"wrap": true
},
{
"type": "Input.ChoiceSet",
"label": "Select a user",
"isMultiSelect": false,
"choices": [
{
"title": "User 1",
"value": "User1"
},
{
"title": "User 2",
"value": "User2"
},
{
"title": "User 3",
"value": "User3"
},
{
"title": "User 4",
"value": "User4"
},
{
"title": "User 5",
"value": "User5"
}
],
"style": "filtered"
}
]
},
{
"type": "Column",
"width": 1,
"items": [
{
"type": "Image",
"url": "https://picsum.photos/300?image=882",
"size": "auto"
}
]
}
]
}
],
"actions": [
{
"type": "Action.OpenUrl",
"title": "More Info",
"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
}
]
}

Node-RED: Interpreting REST service URL

I am currently using the IBM Bluemix IoT Quickstart for some experiments. For those of you who don't know this, it is an IoT Sensor simulator that can be used to control temperature and humidity values.
I am looking to use a web service connector to look for the temperature reading. How would I go about interpreting the Service URL for this?
Also (but not as important!) I see that the website /test/ fails to display the temperature reading. Any ideas as to why?
Below is the json to be used by web service to display temperature
[
{
"id": "e42c6f27.46cca",
"type": "function",
"z": "3ed49b1f.4c1164",
"name": "temp",
"func": "return {payload:msg.payload.d.temp};",
"outputs": 1,
"noerr": 0,
"x": 422.5,
"y": 775,
"wires": [
[
"c76cd036.bc2ba"
]
]
},
{
"id": "f47256cd.a18b78",
"type": "template",
"z": "3ed49b1f.4c1164",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "The temperature is\n{{payload}}\n",
"output": "str",
"x": 775,
"y": 953,
"wires": [
[
"22b54d74.028ae2"
]
]
},
{
"id": "22b54d74.028ae2",
"type": "http response",
"z": "3ed49b1f.4c1164",
"name": "",
"statusCode": "",
"headers": {
},
"x": 1064,
"y": 955,
"wires": [
]
},
{
"id": "243d218a.fe14ae",
"type": "http in",
"z": "3ed49b1f.4c1164",
"name": "",
"url": "test",
"method": "get",
"upload": false,
"swaggerDoc": "",
"x": 432,
"y": 993,
"wires": [
[
"f47256cd.a18b78"
]
]
},
{
"id": "429ffb9e.7ed404",
"type": "ibmiot in",
"z": "3ed49b1f.4c1164",
"authentication": "quickstart",
"apiKey": "",
"inputType": "evt",
"deviceId": "3bd1c8df5ed2",
"applicationId": "",
"deviceType": "iotqs-sensor",
"eventType": "+",
"commandType": "",
"format": "json",
"name": "IBM IoT App In",
"service": "quickstart",
"allDevices": false,
"allApplications": false,
"allDeviceTypes": true,
"allEvents": true,
"allCommands": false,
"allFormats": false,
"x": 269,
"y": 550,
"wires": [
[
"55064d15.005804",
"c34674a2.7ea0d8",
"2356b867.1c3d68",
"c7ee16c4.493fc8",
"e42c6f27.46cca"
]
]
},
{
"id": "55064d15.005804",
"type": "debug",
"z": "3ed49b1f.4c1164",
"name": "name",
"active": true,
"complete": "payload.d.name",
"x": 717.5,
"y": 551,
"wires": [
]
},
{
"id": "c34674a2.7ea0d8",
"type": "debug",
"z": "3ed49b1f.4c1164",
"name": "Temperature",
"active": true,
"complete": "payload.d.temp",
"x": 717.5,
"y": 651,
"wires": [
]
},
{
"id": "2356b867.1c3d68",
"type": "debug",
"z": "3ed49b1f.4c1164",
"name": "Humidity",
"active": true,
"complete": "payload.d.humidity",
"x": 717.5,
"y": 751,
"wires": [
]
},
{
"id": "c7ee16c4.493fc8",
"type": "debug",
"z": "3ed49b1f.4c1164",
"name": "Object Temperature",
"active": true,
"complete": "payload.d.objectTemp",
"x": 717.5,
"y": 851,
"wires": [
]
},
{
"id": "c76cd036.bc2ba",
"type": "json",
"z": "3ed49b1f.4c1164",
"name": "",
"pretty": true,
"x": 531,
"y": 894,
"wires": [
[
"f47256cd.a18b78"
]
]
}
]
There are a few issues with this flow:
the JSON node is not necessary, since the msg.payload is already json
the output readings from the iot node were being sent to the http response node, which is only needed when the flow is returning results from the API call
the output from the http in node contains no msg.payload, so the template node has nothing to format
Here is a different approach, that I believe gives you what you need:
store the output object msg.payload.d into a flow context variable called "lastReadings"
use the http in node with url /last/:type as the api endpoint to retrieve the last value for the given sensor type (if you have node-red-node-swagger installed, you can test the endpoint directly from the right-hand sidebar)
use a switch node to route the api call, based on the sensor type, to retrieve the last reading and return the output string through the http response node
A couple things to note: if your settings.js includes either an httpRoot or httpNodeRoot url prefix, this will preceed your api url -- this is probably why you got no results when trying to access the url "http://localhost:1880/test/". Check the notes on the http in node for something like this:
The url will be relative to /red.
in which case you would need to use the api url "http://localhost:1880/red/test/"
Here is the flow I created, along with an inject node for testing your values (since I don't have the ibmiot input node available):
[
{
"id": "5ebe7dff.780724",
"type": "template",
"z": "7299298e.a9f058",
"name": "output value",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "Last {{sensor}} reading:\n{{payload}}\n",
"output": "str",
"x": 630,
"y": 720,
"wires": [
[
"cb13c651.0ef458",
"efabaec0.9bd7a"
]
]
},
{
"id": "cb13c651.0ef458",
"type": "http response",
"z": "7299298e.a9f058",
"name": "",
"statusCode": "",
"headers": {},
"x": 790,
"y": 760,
"wires": []
},
{
"id": "361c51b4.f519ae",
"type": "http in",
"z": "7299298e.a9f058",
"name": "",
"url": "/last/:type",
"method": "get",
"upload": false,
"swaggerDoc": "59b72c5b.bc0fb4",
"x": 150,
"y": 640,
"wires": [
[
"c2fb1169.b76e8",
"61234f00.7faf1"
]
]
},
{
"id": "884f3eb9.e22be",
"type": "debug",
"z": "7299298e.a9f058",
"name": "payload.d values",
"active": true,
"console": "false",
"complete": "payload.d",
"x": 470,
"y": 480,
"wires": []
},
{
"id": "fef6db49.919728",
"type": "comment",
"z": "7299298e.a9f058",
"name": "Save the last readings to flow context",
"info": "",
"x": 210,
"y": 440,
"wires": []
},
{
"id": "81bfb514.a63348",
"type": "change",
"z": "7299298e.a9f058",
"name": "save readings",
"rules": [
{
"t": "set",
"p": "lastReadings",
"pt": "flow",
"to": "payload.d",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 460,
"y": 520,
"wires": [
[]
]
},
{
"id": "9573eab5.84e548",
"type": "comment",
"z": "7299298e.a9f058",
"name": "API endpoints to retrieve sensor values",
"info": "",
"x": 210,
"y": 600,
"wires": []
},
{
"id": "c2fb1169.b76e8",
"type": "switch",
"z": "7299298e.a9f058",
"name": "sensor type",
"property": "req.params.type",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "temp",
"vt": "str"
},
{
"t": "eq",
"v": "humidity",
"vt": "str"
},
{
"t": "eq",
"v": "objectTemp",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "true",
"outputs": 4,
"x": 250,
"y": 700,
"wires": [
[
"4eb4657b.cd0b0c"
],
[
"801c0b28.320308"
],
[
"a7833d28.f58f"
],
[
"e30f26ce.a084b8"
]
]
},
{
"id": "4eb4657b.cd0b0c",
"type": "change",
"z": "7299298e.a9f058",
"name": "Temperature",
"rules": [
{
"t": "set",
"p": "sensor",
"pt": "msg",
"to": "Temperature",
"tot": "str"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "lastReadings.temp",
"tot": "flow"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 450,
"y": 680,
"wires": [
[
"5ebe7dff.780724"
]
]
},
{
"id": "801c0b28.320308",
"type": "change",
"z": "7299298e.a9f058",
"name": "Humidity",
"rules": [
{
"t": "set",
"p": "sensor",
"pt": "msg",
"to": "Humidity",
"tot": "str"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "lastReadings.humidity",
"tot": "flow"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 440,
"y": 720,
"wires": [
[
"5ebe7dff.780724"
]
]
},
{
"id": "a7833d28.f58f",
"type": "change",
"z": "7299298e.a9f058",
"name": "Object Temp",
"rules": [
{
"t": "set",
"p": "sensor",
"pt": "msg",
"to": "Object Temp",
"tot": "str"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "lastReadings.objectTemp",
"tot": "flow"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 450,
"y": 760,
"wires": [
[
"5ebe7dff.780724"
]
]
},
{
"id": "61234f00.7faf1",
"type": "debug",
"z": "7299298e.a9f058",
"name": "API params",
"active": false,
"console": "false",
"complete": "true",
"x": 450,
"y": 640,
"wires": []
},
{
"id": "efabaec0.9bd7a",
"type": "debug",
"z": "7299298e.a9f058",
"name": "API out",
"active": true,
"console": "false",
"complete": "payload",
"x": 800,
"y": 720,
"wires": []
},
{
"id": "b019dd18.1806e",
"type": "inject",
"z": "7299298e.a9f058",
"name": "test values",
"topic": "",
"payload": "{\"d\":{\"temp\":21,\"humidity\":69,\"objectTemp\":\"NA\"}}",
"payloadType": "json",
"repeat": "",
"crontab": "",
"once": false,
"x": 150,
"y": 520,
"wires": [
[
"81bfb514.a63348",
"884f3eb9.e22be"
]
]
},
{
"id": "e30f26ce.a084b8",
"type": "change",
"z": "7299298e.a9f058",
"name": "invalid type",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "Error! Unknown sensor type...",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 450,
"y": 800,
"wires": [
[
"cb13c651.0ef458"
]
]
},
{
"id": "e02d2f38.842d8",
"type": "ibmiot in",
"z": "7299298e.a9f058",
"authentication": "quickstart",
"apiKey": "",
"inputType": "evt",
"deviceId": "3bd1c8df5ed2",
"applicationId": "",
"deviceType": "iotqs-sensor",
"eventType": "+",
"commandType": "",
"format": "json",
"name": "IBM IoT App In",
"service": "quickstart",
"allDevices": false,
"allApplications": false,
"allDeviceTypes": true,
"allEvents": true,
"allCommands": false,
"allFormats": false,
"x": 150,
"y": 480,
"wires": [
[
"884f3eb9.e22be",
"81bfb514.a63348"
]
]
},
{
"id": "59b72c5b.bc0fb4",
"type": "swagger-doc",
"z": "",
"summary": "Retrieve last sensor reading",
"description": "",
"tags": "",
"consumes": "",
"produces": "",
"parameters": [
{
"name": "type",
"in": "path",
"description": "Sensor type",
"required": true,
"type": "string"
}
],
"responses": {},
"deprecated": false
}
]
Please note that I changed the api url to use /last/{type} syntax... where {type} is replaced by the name of the field in your msg.payload.d object.