[SOLVED] Passing Variable from Trigger

First post - new to HA! Loving it so much at this point as it’s so much more flexible than my Wink Hub2!

In any event, I’m looking to use this existing flow in node-red which does work just fine but route it to the person who triggered it. Use case being that it should notify the last person to leave the home they forgot to shut off the lights.

So in the first two trigger nodes, I’d like to output something like the entity name and then switch it to the registered name for notifications. It would of course need to make it past all the other nodes and be consumed in the Android Notify node.

Hope that makes sense!

Thanks!

node-red code:

[{"id":"4339fc91.cf8304","type":"tab","label":"Notifications","disabled":false,"info":""},{"id":"7a589892.605ac8","type":"trigger-state","z":"4339fc91.cf8304","name":"Tom Left Home","server":"5265857.3021e7c","entityid":"device_tracker.toms_pixel_2","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"9i2c9sz7d3e","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"pnytw6yb6e","targetType":"entity_id","targetValue":"device_tracker.emilys_pixel_2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"mluovtrmf0h","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":100,"y":40,"wires":[["e92fe981.dcbb28"],[]]},{"id":"cb7a0dda.a8ef3","type":"template","z":"4339fc91.cf8304","name":"Format Friendly Name","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.attributes.friendly_name}}","output":"str","x":580,"y":80,"wires":[["1f2f0511.7cb41b"]]},{"id":"1f2f0511.7cb41b","type":"join","z":"4339fc91.cf8304","name":"Join Entities","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":", ","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":790,"y":80,"wires":[["8f2f19f8.bf9c98"]]},{"id":"8f2f19f8.bf9c98","type":"api-call-service","z":"4339fc91.cf8304","name":"Notify Android","server":"5265857.3021e7c","service_domain":"notify","service":"android","data":"{\"message\":\"The light(s): {{payload}} is still on.\",\"target\":\"Toms_Pixel_2\"}","mergecontext":"","output_location":"","output_location_type":"none","x":980,"y":80,"wires":[[]]},{"id":"e92fe981.dcbb28","type":"ha-get-entities","z":"4339fc91.cf8304","server":"5265857.3021e7c","name":"Get Light/TV States","rules":[{"property":"entity_id","logic":"includes","value":"light.dining_room_recessed_lights,light.sengled_e11_g13_03027d89_1,light.emily_night_stand,light.kitchen_pendants,light.kitchen_pendants,light.living_room_recessed_lights,switch.entertainment_center,switch.tv_outlet","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":350,"y":80,"wires":[["cb7a0dda.a8ef3"]]},{"id":"417e8650.f039c8","type":"trigger-state","z":"4339fc91.cf8304","name":"Emily Left Home","server":"5265857.3021e7c","entityid":"device_tracker.emilys_pixel_2","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"9i2c9sz7d3e","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"whjpxt6hf3","targetType":"entity_id","targetValue":"device_tracker.toms_pixel_2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"y1b0arkmr5","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":100,"y":120,"wires":[["e92fe981.dcbb28"],[]]},{"id":"474ac35d.fd35fc","type":"inject","z":"4339fc91.cf8304","name":"Simulate Away","topic":"","payload":"{\"message\":\"not_home\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":200,"wires":[["e92fe981.dcbb28"]]},{"id":"5265857.3021e7c","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

msg.topic will contain device_tracker.emilys_pixel_2 or device_tracker.toms_pixel_2 depending on which trigger-node fired. So the information you need is already available in the msg object. There are many ways of going about translating that information into a valid target string.

A possible solution could be to use a change node before the call-service node and use search/replace to change the entity_id to a valid target string and then reference that in the call-service node.

edit:
You can also change this down to one trigger-node and get the same results by changing the entity_id field to a comma delimited string, device_tracker.emilys_pixel_2, device_tracker.toms_pixel_2. Then changing the entity filter from exact to substring. You then need to add another constraint to check if both people are not_home.

[{"id":"76698599.58cd7c","type":"trigger-state","z":"b934b747.780d08","name":"Notify who left the lights on","server":"8d00ebbc.99a928","entityid":"device_tracker.emilys_pixel_2,device_tracker.toms_pixel_2 ","entityidfiltertype":"substring","debugenabled":false,"constraints":[{"id":"9i2c9sz7d3e","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"whjpxt6hf3","targetType":"entity_id","targetValue":"device_tracker.toms_pixel_2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"y1b0arkmr5","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"uluv61x342","targetType":"entity_id","targetValue":"device_tracker.emilys_pixel_2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":520,"y":352,"wires":[["25741b46.ca03e4"],[]]}]

Thanks Kermit! I will give this a shot. Really appreciate the quick response!

I think I’m pretty much there but I can’t seem to call the entity_id in the data. What’s the proper format? I’ve tried a multitude of things but I figure it should be {{ data.entity_id }}. Clearly I’m wrong but can’t seem to find it anywhere.

Just thinking about how i would have approached it with only 2 persons:

Events State: Person 1 leaving --> Current State: Is Person 2 Still Home? --> Only send notification if not.
Same for the other way around. Results in 2 Flows though.

It may work… not as elegant as you mentioned but at this point just looking for functionality. :slight_smile:

I may give that a try.

I ended up figuring this out!

I changed the message topic to the known device name and in the call service, I called {{topic}}.

Thanks for help all!

Could you please post the code of your final solution? :slight_smile:

Sure thing!

[{"id":"be6a0274.3c219","type":"tab","label":"Notifications","disabled":false,"info":""},{"id":"68ae04dd.593e9c","type":"trigger-state","z":"be6a0274.3c219","name":"Tom Left Home","server":"5265857.3021e7c","entityid":"device_tracker.toms_pixel_2","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"9i2c9sz7d3e","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"pnytw6yb6e","targetType":"entity_id","targetValue":"device_tracker.emilys_pixel_2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"mluovtrmf0h","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":100,"y":40,"wires":[["357efe4f.d3a6e2"],[]]},{"id":"da6fba14.efdb08","type":"template","z":"be6a0274.3c219","name":"Format Friendly Name","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.attributes.friendly_name}}","output":"str","x":580,"y":80,"wires":[["884e2475.9739a8"]]},{"id":"884e2475.9739a8","type":"join","z":"be6a0274.3c219","name":"Join Entities","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":", ","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":790,"y":80,"wires":[["664f01aa.41fb8"]]},{"id":"9ab44191.91394","type":"api-call-service","z":"be6a0274.3c219","name":"Notify Android","server":"5265857.3021e7c","service_domain":"notify","service":"android","data":"{\"title\":\"Items Left On When You Left\",\"message\":\"The light(s): {{payload}} is still on.\",\"target\":\"{{topic}}\"}","mergecontext":"","output_location":"","output_location_type":"none","x":1280,"y":80,"wires":[[]]},{"id":"357efe4f.d3a6e2","type":"ha-get-entities","z":"be6a0274.3c219","server":"5265857.3021e7c","name":"Get Light/TV States","rules":[{"property":"entity_id","logic":"includes","value":"light.dining_room_recessed_lights,light.sengled_e11_g13_03027d89_1,light.emily_night_stand,light.kitchen_pendants,light.kitchen_pendants,light.living_room_recessed_lights,switch.entertainment_center,switch.tv_outlet","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":350,"y":80,"wires":[["da6fba14.efdb08"]]},{"id":"bcb995ba.38de08","type":"trigger-state","z":"be6a0274.3c219","name":"Emily Left Home","server":"5265857.3021e7c","entityid":"device_tracker.emilys_pixel_2","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"9i2c9sz7d3e","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"whjpxt6hf3","targetType":"entity_id","targetValue":"device_tracker.toms_pixel_2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"y1b0arkmr5","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":100,"y":120,"wires":[["357efe4f.d3a6e2"],[]]},{"id":"664f01aa.41fb8","type":"change","z":"be6a0274.3c219","name":"Change Topic to Target Variable","rules":[{"t":"change","p":"topic","pt":"msg","from":"device_tracker.toms_pixel_2","fromt":"str","to":"Toms_Pixel_2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":80,"wires":[["9ab44191.91394"]]},{"id":"5265857.3021e7c","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

1 Like

[{"id":"bd443daf.5b59e","type":"trigger-state","z":"c0952b0f.b0dde8","name":"Someone left","server":"8d00ebbc.99a928","entityid":"device_tracker.toms_pixel_2,device_tracker.emilys_pixel_2","entityidfiltertype":"substring","debugenabled":false,"constraints":[{"id":"9i2c9sz7d3e","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"pnytw6yb6e","targetType":"entity_id","targetValue":"device_tracker.emilys_pixel_2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"mluovtrmf0h","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"4im0r6xnqhu","targetType":"entity_id","targetValue":"device_tracker.toms_pixel_2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":150,"y":80,"wires":[["e9fb71b2.c8cb"],[]]},{"id":"1be35a1a.d81e76","type":"template","z":"c0952b0f.b0dde8","name":"Format Friendly Name","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.attributes.friendly_name}}","output":"str","x":580,"y":80,"wires":[["fad3e8b8.eee4c8"]]},{"id":"fad3e8b8.eee4c8","type":"join","z":"c0952b0f.b0dde8","name":"Join Entities","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":", ","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":790,"y":80,"wires":[["58b62ca5.acad34"]]},{"id":"ca71e256.b7cec","type":"api-call-service","z":"c0952b0f.b0dde8","name":"Notify Android","server":"8d00ebbc.99a928","service_domain":"notify","service":"android","data":"{\"title\":\"Items Left On When You Left\",\"message\":\"The light(s): {{payload}} is still on.\",\"target\":\"{{topic}}\"}","mergecontext":"","output_location":"","output_location_type":"none","x":1280,"y":80,"wires":[[]]},{"id":"e9fb71b2.c8cb","type":"ha-get-entities","z":"c0952b0f.b0dde8","server":"5265857.3021e7c","name":"Get Light/TV States","rules":[{"property":"entity_id","logic":"includes","value":"light.dining_room_recessed_lights,light.sengled_e11_g13_03027d89_1,light.emily_night_stand,light.kitchen_pendants,light.kitchen_pendants,light.living_room_recessed_lights,switch.entertainment_center,switch.tv_outlet","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":350,"y":80,"wires":[["1be35a1a.d81e76"]]},{"id":"58b62ca5.acad34","type":"change","z":"c0952b0f.b0dde8","name":"Change Topic to Target Variable","rules":[{"t":"change","p":"topic","pt":"msg","from":"device_tracker.toms_pixel_2","fromt":"str","to":"Toms_Pixel_2","tot":"str"},{"t":"change","p":"topic","pt":"msg","from":"device_tracker.emilys_pixel_2","fromt":"str","to":"Emilys_Pixel_2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":80,"wires":[["ca71e256.b7cec"]]}]
1 Like