Passing Variables

Sorry for very basic question. I have a function node that generates a list of media_players that audio should be played on. I want to pass that variable onto the "group_members" attribute in the media_player service.

I know this doesn't work but it's basically what I want to do.

FUNCTION NODE

msg.speakers = "media_player.bathroom, media_player.family_room";

I've also tried an array:

msg.speakers = ["media_player.bathroom", "media_player.family_room"];

CALL SERVICE NODE

domain: media_player
service: join
Data: {"group_members":"{{ speakers }}" }

This "hard-coded" way below works but I want to be able to pass it as a variable. What do I need to do?

Data: {"group_members":["media_player.bathroom", "media_player.family_room"]}

Here is my flow:

[{"id":"41c32b4e.1038e4","type":"api-call-service","z":"d9abccab.6dc37","name":"","server":"c4f9e6a2.5aace8","version":3,"debugenabled":false,"service_domain":"media_player","service":"join","entityId":"","data":"{\"group_members\":\" {{ speakers }} \"}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2300,"y":960,"wires":[[]]},{"id":"48319116.f1a9e","type":"inject","z":"d9abccab.6dc37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1910,"y":940,"wires":[["4832002c.ec4f5"]]},{"id":"4832002c.ec4f5","type":"function","z":"d9abccab.6dc37","name":"","func":"msg.speakers = [\"media_player.family_room\", \"media_player.guest_bathroom\"];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2090,"y":960,"wires":[["41c32b4e.1038e4"]]},{"id":"c4f9e6a2.5aace8","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

group_members is an array not a string
msg.speakers = ["media_player.bathroom", "media_player.family_room"]; should work

[edit] fixed typo.

I should have mentioned that I also tried that and it did not work either. I just double checked again and I got error:
"Call-service error. Entity ID media_player.family_room,media_player.guest_bathroom is an invalid entity ID @ data['group_members'][0]"

You should not have let or var on the front of msg.speakers. you are not creating a new variable, just assigning to to a new property of msg. I have no idea what your code will do or whether this is part of the problem.

The n try

msg.speakers = '"media_player.bathroom", "media_player.family_room"'

and in HA node

{Data: {"group_members":[{{ speakers }}] }}

or use JSONata, select J: in HA node and enter

{Data: {"group_members": $$.speakers } }

and in function use

msg.speakers = ["media_player.bathroom", "media_player.family_room"];

please check capitalisation of property names to.

JSONata example edited by hand as do not have HA installed, may be errors.

[{"id":"4832002c.ec4f5","type":"function","z":"20bb0f7f9fab47a8","name":"","func":"msg.speakers = [\"media_player.family_room\", \"media_player.guest_bathroom\"];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":1560,"wires":[["41c32b4e.1038e4"]]},{"id":"48319116.f1a9e","type":"inject","z":"20bb0f7f9fab47a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":1540,"wires":[["4832002c.ec4f5"]]},{"id":"41c32b4e.1038e4","type":"api-call-service","z":"20bb0f7f9fab47a8","name":"","server":"c4f9e6a2.5aace8","version":3,"debugenabled":false,"service_domain":"media_player","service":"join","entityId":"","data":"{\"group_members\": $$.speakers }}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":520,"y":1560,"wires":[[]]}]

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.