Created
November 15, 2020 17:06
-
-
Save ronaldheft/ddc2097d9d305f21188277922f93e09f to your computer and use it in GitHub Desktop.
Node-RED Flow for Creating WMATA Station Sensors in Home Assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": "39464082.632718", | |
"type": "tab", | |
"label": "WMATA", | |
"disabled": false, | |
"info": "" | |
}, | |
{ | |
"id": "a7d5bd51.dd77c8", | |
"type": "http request", | |
"z": "39464082.632718", | |
"name": "Request Trains", | |
"method": "GET", | |
"ret": "obj", | |
"paytoqs": false, | |
"url": "", | |
"tls": "", | |
"persist": false, | |
"proxy": "", | |
"authType": "", | |
"x": 760, | |
"y": 180, | |
"wires": [ | |
[ | |
"f4ae06d0.a124f", | |
"4e1b3a66.af511c" | |
] | |
] | |
}, | |
{ | |
"id": "1a4269cf.600ea6", | |
"type": "inject", | |
"z": "39464082.632718", | |
"name": "Request Stations", | |
"props": [ | |
{ | |
"p": "payload" | |
}, | |
{ | |
"p": "topic", | |
"vt": "str" | |
} | |
], | |
"repeat": "18", | |
"crontab": "", | |
"once": false, | |
"onceDelay": 0.1, | |
"topic": "", | |
"payload": "A02,C03,D04,C01,F01,A01,B01", | |
"payloadType": "str", | |
"x": 130, | |
"y": 100, | |
"wires": [ | |
[ | |
"e35fea6f.a86e8" | |
] | |
], | |
"info": "C01 - Metro Center\nA01 - Metro Center (Red)\nF01 - Gallery Place\nB01 - Gallery Place (Red)" | |
}, | |
{ | |
"id": "7ab73c9e.b020e4", | |
"type": "change", | |
"z": "39464082.632718", | |
"name": "Set Request Properties", | |
"rules": [ | |
{ | |
"t": "set", | |
"p": "url", | |
"pt": "msg", | |
"to": "'https://api.wmata.com/StationPrediction.svc/json/GetPrediction/' & payload", | |
"tot": "jsonata" | |
}, | |
{ | |
"t": "set", | |
"p": "headers.api_key", | |
"pt": "msg", | |
"to": "<your_key_here>", | |
"tot": "str" | |
}, | |
{ | |
"t": "set", | |
"p": "station", | |
"pt": "msg", | |
"to": "payload", | |
"tot": "msg" | |
} | |
], | |
"action": "", | |
"property": "", | |
"from": "", | |
"to": "", | |
"reg": false, | |
"x": 550, | |
"y": 180, | |
"wires": [ | |
[ | |
"a7d5bd51.dd77c8" | |
] | |
] | |
}, | |
{ | |
"id": "76dc3b3.b194044", | |
"type": "ha-api", | |
"z": "39464082.632718", | |
"name": "Update Sensor", | |
"server": "d36b6a2c.167578", | |
"protocol": "http", | |
"method": "post", | |
"path": "", | |
"data": "", | |
"dataType": "json", | |
"location": "payload", | |
"locationType": "msg", | |
"responseType": "json", | |
"x": 1540, | |
"y": 100, | |
"wires": [ | |
[ | |
"a3481fd2.93e618" | |
] | |
] | |
}, | |
{ | |
"id": "a3481fd2.93e618", | |
"type": "debug", | |
"z": "39464082.632718", | |
"name": "Updated Sensor", | |
"active": false, | |
"tosidebar": true, | |
"console": false, | |
"tostatus": false, | |
"complete": "payload", | |
"targetType": "msg", | |
"x": 1680, | |
"y": 200, | |
"wires": [] | |
}, | |
{ | |
"id": "f4ae06d0.a124f", | |
"type": "change", | |
"z": "39464082.632718", | |
"name": "Return Trains", | |
"rules": [ | |
{ | |
"t": "set", | |
"p": "payload", | |
"pt": "msg", | |
"to": "payload.Trains", | |
"tot": "msg" | |
} | |
], | |
"action": "", | |
"property": "", | |
"from": "", | |
"to": "", | |
"reg": false, | |
"x": 930, | |
"y": 180, | |
"wires": [ | |
[ | |
"aad9e438.6f563" | |
] | |
] | |
}, | |
{ | |
"id": "8284f7ae.7e394", | |
"type": "change", | |
"z": "39464082.632718", | |
"name": "Format as Sensor", | |
"rules": [ | |
{ | |
"t": "move", | |
"p": "payload", | |
"pt": "msg", | |
"to": "data", | |
"tot": "msg" | |
}, | |
{ | |
"t": "set", | |
"p": "payload", | |
"pt": "msg", | |
"to": "{\t \"method\": \"post\",\t \"path\": \"/states/sensor.wmata_\" & data[0].LocationCode & \"_\" & data[0].Group,\t \"data\": {\t \"state\": data[0].Min != \"\" ? data[0].Min : \"DLY\",\t \"attributes\": {\t \"friendly_name\": data[0].LocationName & \" - Track \" & data[0].Group,\t \"icon\": \"mdi:train\",\t \"Trains\": $count(data)\t }\t }\t}", | |
"tot": "jsonata" | |
}, | |
{ | |
"t": "set", | |
"p": "payload.data.attributes", | |
"pt": "msg", | |
"to": "$merge([\t payload.data.attributes,\t $merge($map(data, function($train, $i) {\t $merge($each($train, function($value, $name) {\t {\"Train_\" & $i & \"_\" & $name: ($name = \"Min\" and $value = \"\") ? \"DLY\" : $value}\t }))\t }))\t])", | |
"tot": "jsonata" | |
}, | |
{ | |
"t": "set", | |
"p": "payload.data.attributes.secondary_info", | |
"pt": "msg", | |
"to": "\"<table>\"\t &\t $join($map(data, function($train, $i) {\t \"<tr>\"\t &\t \"<td>\" & ($train.Line != \"No\" ? $train.Line : \"-\") & \"</td>\" &\t \"<td>\" & ($trim($train.Car != null ? $train.Car : \"null\") != \"null\" and $train.Car != \"-\" ? ($train.Car != \" \" ? $train.Car & \" Car\" : $train.Car) : \"-\") & \"</td>\" &\t \"<td>\" & ($train.Line != \"No\" ? $train.Destination : \"No Passengers\") & \"</td>\" &\t \"<td>\" & ($train.Min != \"\" ? $train.Min : \"DLY\") & \"</td>\"\t &\t \"</tr>\"\t }))\t &\t\"</table>\"", | |
"tot": "jsonata" | |
} | |
], | |
"action": "", | |
"property": "", | |
"from": "", | |
"to": "", | |
"reg": false, | |
"x": 1330, | |
"y": 100, | |
"wires": [ | |
[ | |
"76dc3b3.b194044", | |
"97f17d94.c3a27" | |
] | |
] | |
}, | |
{ | |
"id": "aad9e438.6f563", | |
"type": "change", | |
"z": "39464082.632718", | |
"name": "Group by Track", | |
"rules": [ | |
{ | |
"t": "set", | |
"p": "payload", | |
"pt": "msg", | |
"to": "payload{`Group`: [$]}", | |
"tot": "jsonata" | |
}, | |
{ | |
"t": "set", | |
"p": "payload", | |
"pt": "msg", | |
"to": "{\t \"1\": ($exists(payload.\"1\") ? payload.\"1\" : [{\t \"Car\": \" \",\t \"Destination\": \" \",\t \"DestinationCode\": \"A00\",\t \"Group\": \"1\",\t \"Line\": \"No Trains\",\t \"LocationCode\": station,\t \"LocationName\": \"Closed\",\t \"Min\": \" \"\t }]),\t \"2\": ($exists(payload.\"2\") ? payload.\"2\" : [{\t \"Car\": \" \",\t \"Destination\": \" \",\t \"DestinationCode\": \"A00\",\t \"Group\": \"2\",\t \"Line\": \"No Trains\",\t \"LocationCode\": station,\t \"LocationName\": \"Closed\",\t \"Min\": \" \"\t }])\t}", | |
"tot": "jsonata" | |
} | |
], | |
"action": "", | |
"property": "", | |
"from": "", | |
"to": "", | |
"reg": false, | |
"x": 940, | |
"y": 100, | |
"wires": [ | |
[ | |
"bc363296.00602", | |
"71a7ae1e.91f988" | |
] | |
] | |
}, | |
{ | |
"id": "bc363296.00602", | |
"type": "split", | |
"z": "39464082.632718", | |
"name": "Split by Track", | |
"splt": "\\n", | |
"spltType": "str", | |
"arraySplt": 1, | |
"arraySpltType": "len", | |
"stream": false, | |
"addname": "", | |
"x": 1120, | |
"y": 100, | |
"wires": [ | |
[ | |
"8284f7ae.7e394", | |
"f77d775e.200d1" | |
] | |
] | |
}, | |
{ | |
"id": "42013261.7e61c4", | |
"type": "split", | |
"z": "39464082.632718", | |
"name": "Split by Station", | |
"splt": ",", | |
"spltType": "str", | |
"arraySplt": 1, | |
"arraySpltType": "len", | |
"stream": false, | |
"addname": "", | |
"x": 480, | |
"y": 100, | |
"wires": [ | |
[ | |
"681674d8.c62df4" | |
] | |
] | |
}, | |
{ | |
"id": "97f17d94.c3a27", | |
"type": "debug", | |
"z": "39464082.632718", | |
"name": "Formatted Sensor", | |
"active": false, | |
"tosidebar": true, | |
"console": false, | |
"tostatus": false, | |
"complete": "payload", | |
"targetType": "msg", | |
"x": 1460, | |
"y": 200, | |
"wires": [] | |
}, | |
{ | |
"id": "89f38a6b.8d22d8", | |
"type": "inject", | |
"z": "39464082.632718", | |
"name": "Inject Union Station", | |
"repeat": "", | |
"crontab": "", | |
"once": false, | |
"onceDelay": 0.1, | |
"topic": "", | |
"payload": "B03", | |
"payloadType": "str", | |
"x": 130, | |
"y": 60, | |
"wires": [ | |
[ | |
"42013261.7e61c4" | |
] | |
] | |
}, | |
{ | |
"id": "e35fea6f.a86e8", | |
"type": "time-range-switch", | |
"z": "39464082.632718", | |
"name": "Don't Check When Not in Service", | |
"lat": "40.67597", | |
"lon": "-75.09138", | |
"startTime": "01:30", | |
"endTime": "05:00", | |
"startOffset": 0, | |
"endOffset": 0, | |
"x": 220, | |
"y": 180, | |
"wires": [ | |
[], | |
[ | |
"42013261.7e61c4" | |
] | |
], | |
"outputLabels": [ | |
"Not In Service", | |
"In Service" | |
] | |
}, | |
{ | |
"id": "4e1b3a66.af511c", | |
"type": "debug", | |
"z": "39464082.632718", | |
"name": "Debug Request", | |
"active": false, | |
"tosidebar": true, | |
"console": false, | |
"tostatus": false, | |
"complete": "true", | |
"targetType": "full", | |
"x": 940, | |
"y": 220, | |
"wires": [] | |
}, | |
{ | |
"id": "681674d8.c62df4", | |
"type": "delay", | |
"z": "39464082.632718", | |
"name": "Spread Out Requests", | |
"pauseType": "rate", | |
"timeout": "1", | |
"timeoutUnits": "seconds", | |
"rate": "1", | |
"nbRateUnits": "1", | |
"rateUnits": "second", | |
"randomFirst": "1", | |
"randomLast": "5", | |
"randomUnits": "seconds", | |
"drop": false, | |
"x": 680, | |
"y": 100, | |
"wires": [ | |
[ | |
"7ab73c9e.b020e4" | |
] | |
] | |
}, | |
{ | |
"id": "71a7ae1e.91f988", | |
"type": "debug", | |
"z": "39464082.632718", | |
"name": "Debug Group", | |
"active": false, | |
"tosidebar": true, | |
"console": false, | |
"tostatus": false, | |
"complete": "true", | |
"targetType": "full", | |
"x": 1120, | |
"y": 140, | |
"wires": [] | |
}, | |
{ | |
"id": "f77d775e.200d1", | |
"type": "debug", | |
"z": "39464082.632718", | |
"name": "Track", | |
"active": false, | |
"tosidebar": true, | |
"console": false, | |
"tostatus": false, | |
"complete": "true", | |
"targetType": "full", | |
"x": 1250, | |
"y": 200, | |
"wires": [] | |
}, | |
{ | |
"id": "d36b6a2c.167578", | |
"type": "server", | |
"name": "Home Assistant", | |
"legacy": false, | |
"rejectUnauthorizedCerts": true, | |
"ha_boolean": "y|yes|true|on|home|open", | |
"connectionDelay": true | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment