Template into Node-Red

I have a simple template that looks to see which access point the phone tracker is connected too. I have many many automations to identify the phone and display who & where of that phone. Its simple and works really well. The problem is that I’m tracking 15 people over 15 AP’s, which make a ton of automations. I’m struggling to keep them organized. i really like Node-Red and use it for a majority of my controls. The visual layout really works for me. so i want to migrate all those automation into Node-red.

the template returns a true/false

{{ state_attr('device_tracker.unifi_f2_d8_4a_3f_fd_a3_default', 'ap_mac') == '78:8a:20:56:ab:12'}}

I copied the template into a Template node in NR and the debug shows true/false, perfect. Thought that was simple, done. Problem is I cant figure out how to get the next node, be it an entity node, state node or switch node to recognize the T/F output. Consequently i cant make anything happen based on the T/F. I’m clearly missing what msg to send/receive so I’m stuck. i am close, just need a little assistant from someone that know a tad more than me, which is most of you. thanks

I thought I would add the Node-Red flow

[{"id":"93c731c2259a2e5c","type":"api-call-service","z":"9bec33e3bc2ab894","name":"","server":"86158645.d19e98","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.loft_conference_globes"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":840,"y":2140,"wires":[[]]},{"id":"a4693217c7b77767","type":"inject","z":"9bec33e3bc2ab894","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":370,"y":2140,"wires":[["3fef1446a1247e2b"]]},{"id":"3fef1446a1247e2b","type":"api-render-template","z":"9bec33e3bc2ab894","name":"Where","server":"86158645.d19e98","version":0,"template":"platform: template\nvalue_template: >-\n  {{ state_attr('device_tracker.unifi_f2_d8_4a_3f_fd_a3_default', 'ap_mac') ==\n  '78:8a:20:56:b5:ba'}}","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"","templateLocationType":"none","x":520,"y":2140,"wires":[["63a6d377bcf83ccd"]]},{"id":"63a6d377bcf83ccd","type":"switch","z":"9bec33e3bc2ab894","name":"True?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":680,"y":2140,"wires":[["93c731c2259a2e5c"]]},{"id":"86158645.d19e98","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

A value_template won’t work like that, you’ll need to define each state.

{% if is_state_attr('device_tracker.esphome', 'ap_mac', 'f4:92:bf:6c:84:83') %}true
{%-else %}false{% endif %}

Thanks for the feedback Mike. Your version sends back the exact same result that I was getting. The problem is that I don’t know how to get the next node to do anything with the result whether its true or false. i played around with all kinds of msg outputs but nothing gets the T/F through

Then possibly your entity is wrong or the mac

image

$entity().attributes.ap_mac = "78:8a:20:56:ab:12"

Mike, thanks for the response. I don’t think I explained myself very well. it returns the correct response, I was just at another computer that is covered by a different AP so it returned false. My difficulty is that when it is true, i cant get the next node to act based on the true.

Kermit, thanks for the suggestion. i set up your suggestion and will try it out in the morning.

Mike, our 2 versions return the exact same result, which is correct.

Kermit, your suggestion returns ‘home’ which is fine but its too general. I want to know which phone is at which AP. So, i can that Johnny is near the AP in the the basement, Jane is pinging in the loft and Tom is at the shop. They are all home.

When Johnny’s phone = the basement AP, I set 'Basement" to a Jonny-Helper entity. if Johnny goes to the shop, the helper will change to ‘Shop’ and so on. I have all this set up in Automations right now and it works very well. I’m trying to mimic it in Node-Red. My initial NR flow almost works. i have my statement (or Mike’s statement) in a template node and it returns ‘true’ when Johnny is in basement, perfect. I need the next node, say a ‘call service’ to set his helper to basement and then to email me, as an example. I must have the output properties incorrect. Been using msg.payload and trying all kinds of other things.

[{"id":"27dcbd6696b144e3","type":"api-call-service","z":"9bec33e3bc2ab894","name":"Buick","server":"86158645.d19e98","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.loft_buick_globes"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1120,"y":1940,"wires":[["3476710e3da070c0"]]},{"id":"b2a9bd69e8268424","type":"api-render-template","z":"9bec33e3bc2ab894","name":"LOFT","server":"86158645.d19e98","version":0,"template":"platform: template\nvalue_template: >-\n  {{ state_attr('device_tracker.unifi_f2_d8_4a_3f_fd_a3_default', 'ap_mac') ==\n  '78:8a:20:56:b5:ba'}}","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"","templateLocationType":"none","x":720,"y":1990,"wires":[["fa065144a8e0780e","acf7a4f6e6483448"]]},{"id":"320adf2d1fc24e4d","type":"inject","z":"9bec33e3bc2ab894","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":510,"y":1990,"wires":[["b2a9bd69e8268424"]]},{"id":"319c881995cbf438","type":"debug","z":"9bec33e3bc2ab894","name":"2C","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1340,"y":1990,"wires":[]},{"id":"2377a3071dcc241d","type":"api-render-template","z":"9bec33e3bc2ab894","name":"Basement","server":"86158645.d19e98","version":0,"template":"platform: template\nvalue_template: >-\n  {{ state_attr('device_tracker.unifi_f2_d8_4a_3f_fd_a3_default', 'ap_mac') ==\n  '68:d7:9a:28:ea:b5'}}","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"","templateLocationType":"none","x":730,"y":1940,"wires":[["69ea57e3109eba0b","ea5d5165f7fa5b3d"]]},{"id":"0fa63ca3c6640e72","type":"api-call-service","z":"9bec33e3bc2ab894","name":"conference","server":"86158645.d19e98","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.loft_conference_globes"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1140,"y":1990,"wires":[["319c881995cbf438"]]},{"id":"0cd91a9baef67b5f","type":"inject","z":"9bec33e3bc2ab894","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":510,"y":1940,"wires":[["2377a3071dcc241d"]]},{"id":"3476710e3da070c0","type":"debug","z":"9bec33e3bc2ab894","name":"1C","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1340,"y":1940,"wires":[]},{"id":"69ea57e3109eba0b","type":"switch","z":"9bec33e3bc2ab894","name":"True?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":1940,"wires":[["27dcbd6696b144e3","ad5b9cecf36e5f69"]]},{"id":"fa065144a8e0780e","type":"switch","z":"9bec33e3bc2ab894","name":"True?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":1990,"wires":[["0fa63ca3c6640e72","87d3ea9315b4adc6"]]},{"id":"ad5b9cecf36e5f69","type":"debug","z":"9bec33e3bc2ab894","name":"1B","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1120,"y":1890,"wires":[]},{"id":"87d3ea9315b4adc6","type":"debug","z":"9bec33e3bc2ab894","name":"2B","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1120,"y":2050,"wires":[]},{"id":"ea5d5165f7fa5b3d","type":"debug","z":"9bec33e3bc2ab894","name":"1A","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":1890,"wires":[]},{"id":"acf7a4f6e6483448","type":"debug","z":"9bec33e3bc2ab894","name":"2A","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":2050,"wires":[]},{"id":"86158645.d19e98","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

Debug 1A returns "platform: template↵value_template: >-↵ False"
Debug 2A returns "platform: template↵value_template: >-↵ True"

1A,1C,2A,2C return nothing

I’m just turning on lights as a test, because I can quickly tell if its working right. Mike, your statement returns the exact same thing.

thanks for any feedback folks

Correction, 1B,1C and 2B, 2C return nothing

I assume that when you used my template you did not change True to true in the switch node. Using the value template the payload is convoluted. If you want to use that use the regex option to isolate True

image

Btw @Kermit’s version is out putting home only when the ap mac is what is set in the jsonata field. On the bottom, the output options dictate what message is sent when the condition is true. It can be set to anything.

image

image

[{"id":"d821ddd38a674309","type":"server-state-changed","z":"e14abe49e3c782e6","name":"trackers","server":"","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":["device_tracker.johnny","device_tracker.jane","device_tracker.tom"],"entityidfiltertype":"list","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":86,"y":256,"wires":[["bdd70e9e741d9cb9"]]},{"id":"c43bb6a4d965c2ca","type":"api-call-service","z":"e14abe49e3c782e6","name":"set helper","server":"","version":5,"debugenabled":false,"domain":"input_select","service":"select_option","areaId":[],"deviceId":[],"entityId":["{{topic}}"],"data":"{\"option\": location}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":588,"y":256,"wires":[[]]},{"id":"bdd70e9e741d9cb9","type":"change","z":"e14abe49e3c782e6","name":"set location","rules":[{"t":"set","p":"location","pt":"msg","to":"data.new_state.attributes.ap_mac","tot":"msg"},{"t":"change","p":"location","pt":"msg","from":"00:00:00:01","fromt":"str","to":"basement","tot":"str"},{"t":"change","p":"location","pt":"msg","from":"00:00:00:02","fromt":"str","to":"shop","tot":"str"},{"t":"change","p":"location","pt":"msg","from":"00:00:00:03","fromt":"str","to":"converence","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":234,"y":256,"wires":[["703134dbadaa9731"]]},{"id":"703134dbadaa9731","type":"change","z":"e14abe49e3c782e6","name":"tracker to helper","rules":[{"t":"change","p":"topic","pt":"msg","from":"device_tracker.johnny","fromt":"str","to":"input_select.johnny","tot":"str"},{"t":"change","p":"topic","pt":"msg","from":"device_tracker.jane","fromt":"str","to":"input_select.jane","tot":"str"},{"t":"change","p":"topic","pt":"msg","from":"device_tracker.tom","fromt":"str","to":"input_select.tom","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":416,"y":256,"wires":[["c43bb6a4d965c2ca"]]}]

You guys are too good for me. it will take me a bit to understand your feedback. i need a little noodle time. I’m amazed at how deep this stuff goes. You can do anything and there are a 1,00 ways to do it. I really appreciate the feedback, even if its above my head, for now. thanks

I agree, the payload is convoluted. i have no idea how to use the regex, from my research, lots of people have difficulty with that. The Template node - Switch node - Call service node is so close and seems seems like such a simple/clean solution. There must be a way to just get that “true” response out.

regex is really simple. It’s short for regular expression. It looks for an exact match, like using find on a webpage.

Where problems arise it’s indiscriminate, meaning if I set a filter for true and inside the message was the phrase untrue , it would return that message because it contains untrue.

You are using true and false so it will work fine.

image

1 Like

Thats it!! it does exactly what i was looking for it todo. thanks so much Mike.
I think i can use your flow example as well. you guys have been a big help

Now I can start working on what i want HA to do when it sees folks at certain places. it gets so frustrating when you spend hours trying to get something to work, and it makes the reward that much better when you finally get it.

, thanks again.