Smart TTS for Home Assistant

v1.3.2
How it works:

If nothing is playing, TTS will say the message and set the previous volume.

If the speaker is playing something from URL, Spotify or YouTube music, TTS will say message, set the previous volume, and resume playing.

Known issues:

  • If you are playing a playlist directly from YouTube music instead of ytube music player integration, then after TTS playlist is searched by name. Because of that resumed playlist may be different than played before. Also, Smart TTS has to skip music in the album until it finds the right one. That may take some time.

  • If you are sending TTS for a group, and speakers of this group aren't in this same state, then it causes unwanted behavior

If you know how to fix any of those issues please contact me ([email protected]).

Requirements:

How to start:

  1. Double click on Smart TTS node under subflows
  2. Setup your config
  3. Done!

Input variables:

  • msg.speaker (required) ID of Google Cast-enabled speaker.

  • msg.message (required) TTS message.

  • msg.volume (optional) The volume of TTS messages. If the variable is not provided volume is not changing.

  • msg.tts_service (optional) TTS service used to say message. If variable is not provided default TTS service is used.

  • msg.tts_language (optional) Language of Google TTS message.

[{"id":"3e3d3934.fbc6d6","type":"subflow","name":"Smart TTS","info":"If nothing is playing, TTS will say the message and set the previous volume.\nIf the speaker is playing something from URL, Spotify or YouTube music, TTS will \nsay message, set the previous volume, and resume playing. (More info in README)","category":"","in":[{"x":60,"y":380,"wires":[{"id":"55925a42891ddc78"}]}],"out":[{"x":3800,"y":480,"wires":[{"id":"3f076a8f58dfc56c","port":0},{"id":"f6ec512f88c189e6","port":1}]}],"env":[],"meta":{"module":"Smart TTS","type":"TTS","version":"1.3.2","author":"IT freak Jake","keywords":"HomeAssistant, TTS, GoogleHome, Alexa"},"color":"#A6BBCF","icon":"node-red-contrib-cast/google-home-mini2.svg"},{"id":"066ed1257cae1df0","type":"group","z":"3e3d3934.fbc6d6","name":"Smart TTS","style":{"stroke":"#7f7f7f","fill":"#7f7f7f","fill-opacity":"0.5","label":true,"color":"#ffffff"},"nodes":["44f34f0c52659971","847406122c07558c","53c66210e71ee4fb","d27cceb034ea5156","48aa78022ed8ef2f","85dd8af450d1d465","df70af48a130f602","3fdd5edae3cb1dc1","e4ea572d031d0ba7","435032474aed39f5","5f09360362d5e81b","8db273c85d1f4b04","b9c102631689476d","4b9d74d12cbd3e65","df3d995d84e9d2a1","3d1bdae6980660a7","dc7b3b8c84ba81b3","fa06bc39f6a3f587","3e90927058b554e0","dfd8f33cae6112f1","3590e95af2d21f2c","ccce48ede06eeda2","ada76fa963fee4ed","bcd643f0e193fe4e","196f5883c69ffb1e","7cc5fccec52efc64","611f9592df4a7d25","20b7451f116d3791","d3313c39d645fe24","d665a3a0666d9e73","95f172bb955def93","19f6cb1090b213ef"],"x":14,"y":173,"w":3798,"h":494},{"id":"847406122c07558c","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Input","style":{"label":true},"nodes":["dd5da2561d1da094","1e37368a3df1025c","d2c059c5008bde4f","112de556e278482b","9b19615010b376bd","80f20973bdbdf8bc","55925a42891ddc78"],"x":74,"y":319,"w":342,"h":122},{"id":"53c66210e71ee4fb","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Finding right spotify account","style":{"label":true},"nodes":["e78353045515f7ac","6c5f41b09fec25df","e7ef777a7a6a045b","f7a3183be871f7be","26b786bbb49ad3b6"],"x":2054,"y":239,"w":182,"h":122},{"id":"d27cceb034ea5156","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Google TTS","style":{"label":true},"nodes":["a17e2b9756663274","702dffcc5490bb73","f888d49f2f9eb005","b0873892e8b7fbbb","6cd4dde5d62342ab","f546cb88fbc5731f","7f8df83e9505adc9","ac42a053329cbf4b","b97e31754ff057e1","7d62342f06f99e2b","50e99bb2719a3f28"],"x":2274,"y":396.5,"w":482,"h":127},{"id":"48aa78022ed8ef2f","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Initial media type recognition","style":{"label":true},"nodes":["d319ad189b10e442","3ffd12db55d5d4f7","2c7d414d5ce3d55c","6c22ee606bc18a49","1fc09ff71f8b284f","3da46c7a5d14a695","b1f786f11d3f4ff5","8d38296d8f450521"],"x":1394,"y":279,"w":242,"h":222},{"id":"85dd8af450d1d465","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Output","style":{"label":true},"nodes":["f6ec512f88c189e6","3f076a8f58dfc56c","e43cf9554a16153f","b6057a717aeabcef"],"x":3644,"y":399,"w":142,"h":142},{"id":"df70af48a130f602","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"URL resume","style":{"label":true},"nodes":["7bf2cba7be12c0ab","f71c9d143e44c939","37c3dc3962fdb048","906d9044423b3c0e","455aa9ada539bd23","4964d71cfc6a381d","c10fa6b314c14cb3"],"x":2814,"y":519,"w":322,"h":102},{"id":"3fdd5edae3cb1dc1","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Getting spotify accounts data","style":{"label":true},"nodes":["336644d9ab890862","b7ba3ae27f20c256","c787d03aa23c1bed","bc998ed765619c8d","28bc62ecb9f97b72","edf546d8678d9d28"],"x":1654,"y":259,"w":282,"h":102},{"id":"e4ea572d031d0ba7","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Spotify resume","style":{"label":true},"nodes":["e2e2dbece34f204a","d1c952b1829de173","d3f6b9920ed775bf"],"x":2814,"y":199,"w":122,"h":122},{"id":"435032474aed39f5","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Getting ytube accounts data","style":{"label":true},"nodes":["b1ed0e4502e868ec","d62a6e8382a0b0d9","a061f5aa036aac7a","4ce1843a0dc41d2c","1589c198804e476a","fb5d263edc8e1fa5"],"x":1654,"y":379,"w":282,"h":102},{"id":"5f09360362d5e81b","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Finding right ytube account","style":{"label":true},"nodes":["e873bc062009dd69","e6e24ed61aa7f756","b7fafb402587e5ed","21bebf04e67eea85","23a123904807c5dc"],"x":1954,"y":399,"w":262,"h":122},{"id":"8db273c85d1f4b04","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"YouTube music resume","style":{"label":true},"nodes":["df4dbbbcd9bab984","2ea3596d48999a54","c881ccccfc5106c8","96e41efe7416310d","8c17ed997d95b47c","946db825271e70fe","90f3d929d2f350a9","adf6e8ed5be0fba7","d60a7a0a3eaaea02","e98edb9d16d9aec5","80aee4a5ef699baa","ad9d18aec39e64cc","ca2f33b9fc9572a9","15300a68068466ab","3523cef71a9021b4","dd1c235495fa7fb2","7d4d1af7df388375","3a4ed97ef4f4f5ec","39c90d67cd0f5cea","136cbc037a0be9e3","28d6a3cd2a0a567d","d532d5a452af51ea"],"x":2814,"y":339,"w":792,"h":162},{"id":"b9c102631689476d","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Alexa TTS","style":{"label":true},"nodes":["3b512a399c7eacb5","b6176ac0e7c4a21c","9b8c5c2b1e178dc9","356ad3b53d40d040","2d5633eadb576528","77c5dbb579d16c3e","a74b0f14555de090","2505628870b85dc6","99d22c066760b088","999e89fc46396b94"],"x":2314,"y":539,"w":442,"h":102},{"id":"4b9d74d12cbd3e65","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Error handling","style":{"label":true},"nodes":["ab7ec373068e28fb","1e1403ab211b4b11"],"x":114,"y":459,"w":222,"h":82},{"id":"df3d995d84e9d2a1","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Getting speakers groups info","style":{"label":true},"nodes":["5a1f368902eb0a12","439642d88e48109f","b1a1f2a6ba107ea2","bb833fe4ab6e20b8","c7a912d0f8fc62de","b1c761bed53d40a9"],"x":434,"y":299,"w":282,"h":102},{"id":"3d1bdae6980660a7","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Is it group","style":{"label":true},"nodes":["743515015cc33efd","514a87c252fde2f9","76e6aa845545b821"],"x":734,"y":299,"w":162,"h":102},{"id":"dc7b3b8c84ba81b3","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Get group speakers info","style":{"label":true},"nodes":["384861426abe5b8c","31d6ba68118e9ac6","d294aed085fda93c","7dd78dad790672e8","addaed19b41880be","fcae45f78aa92775"],"x":914,"y":299,"w":282,"h":102},{"id":"fa06bc39f6a3f587","type":"group","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"Get more speaker info","style":{"label":true},"nodes":["6f211fb74b2b7f49","c43eca78f57978d1","4a4e522685dc3866"],"x":1214,"y":299,"w":162,"h":82},{"id":"3e90927058b554e0","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":520,"y":480,"wires":[["3590e95af2d21f2c"]]},{"id":"dfd8f33cae6112f1","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":520,"y":600,"wires":[["99d22c066760b088"]]},{"id":"3590e95af2d21f2c","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":1160,"y":480,"wires":[["6f211fb74b2b7f49"]]},{"id":"ccce48ede06eeda2","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":1160,"y":420,"wires":[["6f211fb74b2b7f49"]]},{"id":"ada76fa963fee4ed","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":940,"y":420,"wires":[["ccce48ede06eeda2"]]},{"id":"bcd643f0e193fe4e","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":2220,"y":560,"wires":[["50e99bb2719a3f28"]]},{"id":"196f5883c69ffb1e","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":1680,"y":560,"wires":[["bcd643f0e193fe4e"]]},{"id":"7cc5fccec52efc64","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":2220,"y":460,"wires":[["50e99bb2719a3f28"]]},{"id":"26b786bbb49ad3b6","type":"junction","z":"3e3d3934.fbc6d6","g":"53c66210e71ee4fb","x":2210,"y":300,"wires":[["50e99bb2719a3f28"]]},{"id":"20b7451f116d3791","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":3580,"y":260,"wires":[["b6057a717aeabcef"]]},{"id":"611f9592df4a7d25","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":2920,"y":260,"wires":[["20b7451f116d3791"]]},{"id":"d532d5a452af51ea","type":"junction","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","x":3580,"y":380,"wires":[["b6057a717aeabcef"]]},{"id":"d3313c39d645fe24","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":3580,"y":640,"wires":[["b6057a717aeabcef"]]},{"id":"d665a3a0666d9e73","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":2820,"y":640,"wires":[["d3313c39d645fe24"]]},{"id":"95f172bb955def93","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":3580,"y":580,"wires":[["b6057a717aeabcef"]]},{"id":"19f6cb1090b213ef","type":"junction","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","x":3120,"y":580,"wires":[["95f172bb955def93"]]},{"id":"b6057a717aeabcef","type":"junction","z":"3e3d3934.fbc6d6","g":"85dd8af450d1d465","x":3670,"y":480,"wires":[["e43cf9554a16153f","f6ec512f88c189e6"]]},{"id":"3801c35.52e3d3c","type":"inject","z":"3e3d3934.fbc6d6","name":"Minimal configuration example","props":[{"p":"speaker","v":"media_player.google_home_speaker","vt":"str"},{"p":"message","v":"Smart TTS test","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":160,"y":20,"wires":[[]]},{"id":"9d48e8b2.66b518","type":"inject","z":"3e3d3934.fbc6d6","name":"Full configuration example","props":[{"p":"speaker","v":"media_player.google_home_speaker","vt":"str"},{"p":"message","v":"Smart TTS test","vt":"str"},{"p":"volume","v":"0.6","vt":"num"},{"p":"tts_service","v":"google_translate_say","vt":"str"},{"p":"tts_language","v":"en","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":60,"wires":[[]]},{"id":"a92ec755.7755c8","type":"inject","z":"3e3d3934.fbc6d6","name":"Full configuration example (Alexa)","props":[{"p":"speaker","v":"media_player.alexa_speaker","vt":"str"},{"p":"message","v":"Smart TTS test","vt":"str"},{"p":"volume","v":"0.6","vt":"num"},{"p":"tts_service","v":"alexa_default","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":180,"y":140,"wires":[[]]},{"id":"d0df345.46a3dc8","type":"inject","z":"3e3d3934.fbc6d6","name":"Minimal configuration example (Alexa)","props":[{"p":"speaker","v":"media_player.alexa_speaker","vt":"str"},{"p":"message","v":"Smart TTS test","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":190,"y":100,"wires":[[]]},{"id":"44f34f0c52659971","type":"comment","z":"3e3d3934.fbc6d6","g":"066ed1257cae1df0","name":"README","info":"How it works:\n\nIf nothing is playing, TTS will say the message and set the previous volume.\n\nIf the speaker is playing something from URL, Spotify or YouTube music, TTS will \nsay message, set the previous volume, and resume playing.\n\nKnown issues: \n- If you are playing a playlist directly from YouTube music instead of ytube music player integration,\nthen after TTS playlist is searched by name. Because of that resumed playlist may be different than played before.\nAlso, Smart TTS has to skip music in the album until it finds the right one. That may take some time.\n\n- If you are sending TTS for a group, and speakers of this group aren't in this same state, \nthen it causes unwanted behavior\n\nIf you know how to fix any of those issues please contact me ([email protected]).\n\nRequirements:\n- Home Assistant\nIf you are using Google Home speakers\n- Official Home Assistant Google Cast integration\nIf you are using Alexa speakers\n- Custom Home Assistant Alexa Media Player integration (https://github.com/custom-components/alexa_media_player)\nIf you are using Spotify: \n- Official Home Assistant Spotify integration\n- Custom Home Assistant Spotcast integration (https://github.com/fondberg/spotcast)\nIf you are using YouTube music:\n- Custom Home Assistant ytube music player integration (https://github.com/KoljaWindeler/ytube_music_player)\n- Enable \"Create sensor that provide extra information\" in ytube music player integration\n\nHow to start:\n1. Double click on Smart TTS node under subflows\n2. Setup your config\n3. Done!\n\nInput variables:\n\n- msg.speaker (required)\n    ID of Google Cast-enabled speaker.\n\n- msg.message (required)\n    TTS message.\n\n- msg.volume (optional)\n    The volume of TTS messages. If the variable is not \n    provided volume is not changing.\n    \n- msg.tts_service (optional)\n    TTS service used to say message. If variable \n    is not provided default TTS service is used.\n\n- msg.tts_language (optional)\n    Language of Google TTS message.\n","x":100,"y":280,"wires":[]},{"id":"dd5da2561d1da094","type":"switch","z":"3e3d3934.fbc6d6","g":"847406122c07558c","name":"TTS type","property":"tts_service","propertyType":"msg","rules":[{"t":"neq","v":"alexa_default","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":255,"y":380,"wires":[["9b19615010b376bd"],["d2c059c5008bde4f"]],"l":false},{"id":"1e37368a3df1025c","type":"api-current-state","z":"3e3d3934.fbc6d6","g":"847406122c07558c","name":"Speaker current state","server":"918a63e0.1bc5a","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"{{speaker}}","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"speaker_data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":375,"y":400,"wires":[["dfd8f33cae6112f1"]],"l":false},{"id":"d2c059c5008bde4f","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"847406122c07558c","name":"Turn on speaker","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"turn_on","areaId":[],"deviceId":[],"entityId":["{{speaker}}"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":295,"y":400,"wires":[["112de556e278482b"]],"l":false},{"id":"112de556e278482b","type":"ha-wait-until","z":"3e3d3934.fbc6d6","g":"847406122c07558c","name":"Wait for turning on","server":"918a63e0.1bc5a","version":1,"outputs":2,"entityId":"{{speaker}}","entityIdFilterType":"exact","property":"state","comparator":"is_not","value":"off","valueType":"str","timeout":"5","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":335,"y":400,"wires":[["1e37368a3df1025c"],["1e37368a3df1025c"]],"l":false},{"id":"9b19615010b376bd","type":"api-current-state","z":"3e3d3934.fbc6d6","g":"847406122c07558c","name":"Speaker current state","server":"918a63e0.1bc5a","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"{{speaker}}","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"speaker_data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":315,"y":360,"wires":[["80f20973bdbdf8bc"]],"l":false},{"id":"e78353045515f7ac","type":"function","z":"3e3d3934.fbc6d6","g":"53c66210e71ee4fb","name":"Find right spotify account","func":"let playing_accounts_count=0,\nplaying_spotify_account,\nplaying_spotcast_account,\nspeaker_media_content_id,\nspeaker_friendly_name;\n\nif(msg.is_it_speaker_in_group===true)\n{\n    speaker_media_content_id=msg.speakers_groups_data[msg.which_speaker_group].attributes.media_content_id;\n    speaker_friendly_name=msg.speakers_groups_data[msg.which_speaker_group].attributes.friendly_name;\n}\nelse\n{\n    speaker_media_content_id=msg.speaker_data.attributes.media_content_id;\n    speaker_friendly_name=msg.speaker_data.attributes.friendly_name;\n}\n\nfor(i=0;i<msg.spotify_accounts_count;i++)\n{\n    if(!msg.compare_spotify_speaker_name)\n    {\n        if(msg.spotify_accounts_data[i].state===\"playing\")\n        {\n            playing_accounts_count++;\n            playing_spotify_account=msg.spotify_accounts[i];\n            playing_spotcast_account=msg.spotcast_accounts[i];\n        }\n        if(speaker_media_content_id===msg.spotify_accounts_data[i].attributes.media_content_id)\n        {\n            msg.is_right_account_found=true;\n            msg.right_spotify_account=msg.spotify_accounts[i];\n            msg.right_spotify_account_source=msg.spotify_accounts_data[i].attributes.source;\n            msg.right_spotcast_account=msg.spotcast_accounts[i];\n            break;\n        }\n    }\n    else\n    {\n        if(speaker_friendly_name===msg.spotify_accounts_data[i].attributes.source)\n        {\n            msg.is_right_account_found=true;\n            msg.right_spotify_account=msg.spotify_accounts[i];\n            msg.right_spotify_account_source=msg.spotify_accounts_data[i].attributes.source;\n            msg.right_spotcast_account=msg.spotcast_accounts[i];\n            break;\n        }\n    }\n}\nif(!msg.compare_spotify_speaker_name && !msg.spotify_require_identical_media_content_id && !msg.is_right_account_found && playing_accounts_count==1)\n{\n    msg.is_right_account_found=true;\n    msg.right_spotify_account=playing_spotify_account;\n    msg.right_spotcast_account=playing_spotcast_account;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2095,"y":300,"wires":[["6c5f41b09fec25df"]],"l":false},{"id":"6c5f41b09fec25df","type":"switch","z":"3e3d3934.fbc6d6","g":"53c66210e71ee4fb","name":"Is right account found","property":"is_right_account_found","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":2135,"y":300,"wires":[["f7a3183be871f7be"],["e7ef777a7a6a045b"]],"l":false},{"id":"e7ef777a7a6a045b","type":"change","z":"3e3d3934.fbc6d6","g":"53c66210e71ee4fb","name":"Set media type to NONE","rules":[{"t":"set","p":"media_type","pt":"msg","to":"NONE","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2175,"y":320,"wires":[["26b786bbb49ad3b6"]],"l":false},{"id":"f7a3183be871f7be","type":"change","z":"3e3d3934.fbc6d6","g":"53c66210e71ee4fb","name":"Set media type to spotify","rules":[{"t":"set","p":"media_type","pt":"msg","to":"spotify","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2175,"y":280,"wires":[["26b786bbb49ad3b6"]],"l":false},{"id":"a17e2b9756663274","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"Pause speaker","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"media_pause","areaId":[],"deviceId":[],"entityId":["{{resume_speaker}}"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2355,"y":460,"wires":[["ac42a053329cbf4b"]],"l":false},{"id":"702dffcc5490bb73","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"TTS","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"tts","service":"{{tts_service}}","areaId":[],"deviceId":[],"entityId":["{{tts_speaker}}"],"data":"{\"message\":\"{{message}}\",\"language\":\"{{tts_language}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2475,"y":460,"wires":[["7d62342f06f99e2b"]],"l":false},{"id":"f888d49f2f9eb005","type":"ha-wait-until","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"Wait for start","server":"918a63e0.1bc5a","version":1,"outputs":2,"entityId":"{{tts_speaker}}","entityIdFilterType":"exact","property":"state","comparator":"is","value":"playing","valueType":"str","timeout":"25","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":2555,"y":460,"wires":[["b0873892e8b7fbbb"],[]],"l":false},{"id":"b0873892e8b7fbbb","type":"delay","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2595,"y":460,"wires":[["6cd4dde5d62342ab"]],"l":false},{"id":"6cd4dde5d62342ab","type":"ha-wait-until","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"Wait for end","server":"918a63e0.1bc5a","version":1,"outputs":2,"entityId":"{{tts_speaker}}","entityIdFilterType":"exact","property":"state","comparator":"is_not","value":"playing","valueType":"str","timeout":"5","timeoutType":"num","timeoutUnits":"minutes","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":2635,"y":460,"wires":[["7f8df83e9505adc9"],[]],"l":false},{"id":"f546cb88fbc5731f","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"Set volume","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"volume_set","areaId":[],"deviceId":[],"entityId":["{{tts_speaker}}"],"data":"{\"volume_level\":\"{{volume}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2435,"y":440,"wires":[["702dffcc5490bb73"]],"l":false},{"id":"7f8df83e9505adc9","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"Set volume","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"volume_set","areaId":[],"deviceId":[],"entityId":["{{tts_speaker}}"],"data":"{\"volume_level\":\"{{speaker_data.attributes.volume_level}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2675,"y":460,"wires":[["b97e31754ff057e1"]],"l":false},{"id":"ac42a053329cbf4b","type":"switch","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"","property":"volume","propertyType":"msg","rules":[{"t":"btwn","v":"0","vt":"num","v2":"1","v2t":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":2395,"y":460,"wires":[["f546cb88fbc5731f"],["702dffcc5490bb73"]],"l":false},{"id":"b97e31754ff057e1","type":"switch","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"Which media type resume","property":"media_type","propertyType":"msg","rules":[{"t":"eq","v":"spotify","vt":"str"},{"t":"eq","v":"ytube","vt":"str"},{"t":"eq","v":"YouTube","vt":"str"},{"t":"eq","v":"url","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":5,"x":2715,"y":460,"wires":[["d1c952b1829de173"],["df4dbbbcd9bab984"],["136cbc037a0be9e3"],["f71c9d143e44c939"],["d665a3a0666d9e73"]],"l":false},{"id":"7d62342f06f99e2b","type":"delay","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"","pauseType":"delay","timeout":"700","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":2515,"y":460,"wires":[["f888d49f2f9eb005"]],"l":false},{"id":"50e99bb2719a3f28","type":"function","z":"3e3d3934.fbc6d6","g":"d27cceb034ea5156","name":"Setup IDs","func":"if(msg.is_it_speaker_in_group===true)\n{\n    msg.tts_speaker=msg.speaker;\n    msg.resume_speaker=msg.speakers_groups[msg.which_speaker_group];\n    msg.resume_speaker_friendly_name=msg.speakers_groups_data[msg.which_speaker_group].attributes.friendly_name;\n}\nelse\n{\n    msg.tts_speaker=msg.speaker;\n    msg.resume_speaker=msg.speaker;\n    msg.resume_speaker_friendly_name=msg.speaker_data.attributes.friendly_name;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2315,"y":460,"wires":[["a17e2b9756663274"]],"l":false},{"id":"d319ad189b10e442","type":"switch","z":"3e3d3934.fbc6d6","g":"48aa78022ed8ef2f","name":"Is it Spotify?","property":"speaker_data.attributes.app_name","propertyType":"msg","rules":[{"t":"eq","v":"Spotify","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":1475,"y":340,"wires":[["3da46c7a5d14a695"],["1fc09ff71f8b284f"]],"l":false},{"id":"3ffd12db55d5d4f7","type":"change","z":"3e3d3934.fbc6d6","g":"48aa78022ed8ef2f","name":"Set media type to NONE","rules":[{"t":"set","p":"media_type","pt":"msg","to":"NONE","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1475,"y":460,"wires":[["196f5883c69ffb1e"]],"l":false},{"id":"2c7d414d5ce3d55c","type":"change","z":"3e3d3934.fbc6d6","g":"48aa78022ed8ef2f","name":"Set media type to url","rules":[{"t":"set","p":"media_type","pt":"msg","to":"url","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1595,"y":460,"wires":[["196f5883c69ffb1e"]],"l":false},{"id":"6c22ee606bc18a49","type":"switch","z":"3e3d3934.fbc6d6","g":"48aa78022ed8ef2f","name":"Is it YT?","property":"media_type","propertyType":"msg","rules":[{"t":"eq","v":"YouTube","vt":"str"},{"t":"eq","v":"ytube","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":3,"x":1555,"y":400,"wires":[["b1f786f11d3f4ff5"],["b1f786f11d3f4ff5"],["2c7d414d5ce3d55c"]],"l":false},{"id":"1fc09ff71f8b284f","type":"function","z":"3e3d3934.fbc6d6","g":"48aa78022ed8ef2f","name":"Is it YT?","func":"msg.media_type=msg.speaker_data.attributes.app_name==\"YouTube Music\" ? \"YouTube\":\"url\";\nif(msg.media_type==\"url\")msg.media_type=msg.speaker_data.attributes.media_content_id.includes(\".googlevideo.\") ? \"ytube\":\"url\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1515,"y":400,"wires":[["6c22ee606bc18a49"]],"l":false},{"id":"3da46c7a5d14a695","type":"switch","z":"3e3d3934.fbc6d6","g":"48aa78022ed8ef2f","name":"Use Spotify?","property":"spotify_accounts_count","propertyType":"msg","rules":[{"t":"gte","v":"1","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":1515,"y":320,"wires":[["edf546d8678d9d28"],["3ffd12db55d5d4f7"]],"l":false},{"id":"b1f786f11d3f4ff5","type":"switch","z":"3e3d3934.fbc6d6","g":"48aa78022ed8ef2f","name":"Use YT?","property":"ytube_accounts_count","propertyType":"msg","rules":[{"t":"gte","v":"1","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":1595,"y":400,"wires":[["fb5d263edc8e1fa5"],["3ffd12db55d5d4f7"]],"l":false},{"id":"8d38296d8f450521","type":"switch","z":"3e3d3934.fbc6d6","g":"48aa78022ed8ef2f","name":"Is speaker playing","property":"speaker_data.state","propertyType":"msg","rules":[{"t":"eq","v":"playing","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":1435,"y":360,"wires":[["d319ad189b10e442"],["3ffd12db55d5d4f7"]],"l":false},{"id":"f6ec512f88c189e6","type":"switch","z":"3e3d3934.fbc6d6","g":"85dd8af450d1d465","name":"Delete variables?","property":"delete_variables","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"false","repair":false,"outputs":2,"x":3705,"y":460,"wires":[["3f076a8f58dfc56c"],[]],"l":false},{"id":"3f076a8f58dfc56c","type":"change","z":"3e3d3934.fbc6d6","g":"85dd8af450d1d465","name":"Delete variables","rules":[{"t":"delete","p":"speaker","pt":"msg"},{"t":"delete","p":"message","pt":"msg"},{"t":"delete","p":"volume","pt":"msg"},{"t":"delete","p":"tts_service","pt":"msg"},{"t":"delete","p":"spotify_accounts","pt":"msg"},{"t":"delete","p":"spotcast_accounts","pt":"msg"},{"t":"delete","p":"spotify_require_identical_media_content_id","pt":"msg"},{"t":"delete","p":"compare_spotify_speaker_name","pt":"msg"},{"t":"delete","p":"use_spotify_speaker_name","pt":"msg"},{"t":"delete","p":"debug","pt":"msg"},{"t":"delete","p":"spotify_accounts_count","pt":"msg"},{"t":"delete","p":"spotify_accounts_data","pt":"msg"},{"t":"delete","p":"speaker_data","pt":"msg"},{"t":"delete","p":"is_right_account_found","pt":"msg"},{"t":"delete","p":"right_spotify_account","pt":"msg"},{"t":"delete","p":"right_spotify_account_source","pt":"msg"},{"t":"delete","p":"right_spotcast_account","pt":"msg"},{"t":"delete","p":"media_type","pt":"msg"},{"t":"delete","p":"media_position","pt":"msg"},{"t":"delete","p":"delete_variables","pt":"msg"},{"t":"delete","p":"ytube_accounts","pt":"msg"},{"t":"delete","p":"ytube_accounts_count","pt":"msg"},{"t":"delete","p":"ytube_accounts_data","pt":"msg"},{"t":"delete","p":"right_ytube_account","pt":"msg"},{"t":"delete","p":"alexa_notify_service","pt":"msg"},{"t":"delete","p":"delay","pt":"msg"},{"t":"delete","p":"alexa_pause_on_tts","pt":"msg"},{"t":"delete","p":"speakers_groups","pt":"msg"},{"t":"delete","p":"speakers_inside_groups","pt":"msg"},{"t":"delete","p":"is_it_group","pt":"msg"},{"t":"delete","p":"is_it_speaker_in_group","pt":"msg"},{"t":"delete","p":"speakers_groups_count","pt":"msg"},{"t":"delete","p":"speakers_groups_data","pt":"msg"},{"t":"delete","p":"tts_speaker","pt":"msg"},{"t":"delete","p":"resume_speaker","pt":"msg"},{"t":"delete","p":"resume_speaker_friendly_name","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":3745,"y":440,"wires":[[]],"l":false},{"id":"e43cf9554a16153f","type":"function","z":"3e3d3934.fbc6d6","g":"85dd8af450d1d465","name":"Debug","func":"if(msg.debug)\n{\n    node.warn(\"Smart TTS debug output: \");\n    node.warn(msg);\n}","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":3705,"y":500,"wires":[],"icon":"font-awesome/fa-bug","l":false},{"id":"7bf2cba7be12c0ab","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"df70af48a130f602","name":"URL resume","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"play_media","areaId":[],"deviceId":[],"entityId":["{{resume_speaker}}"],"data":"{    \"media_content_id\": \"\"& speaker_data.attributes.media_content_id &\"\",    \"media_content_type\": \"audio/mp3\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2895,"y":580,"wires":[["37c3dc3962fdb048"]],"l":false},{"id":"f71c9d143e44c939","type":"delay","z":"3e3d3934.fbc6d6","g":"df70af48a130f602","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":2855,"y":580,"wires":[["7bf2cba7be12c0ab"]],"l":false},{"id":"37c3dc3962fdb048","type":"switch","z":"3e3d3934.fbc6d6","g":"df70af48a130f602","name":"Has duration?","property":"speaker_data.attributes.media_position","propertyType":"msg","rules":[{"t":"istype","v":"number","vt":"number"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":2935,"y":580,"wires":[["906d9044423b3c0e"],["19f6cb1090b213ef"]],"l":false},{"id":"906d9044423b3c0e","type":"function","z":"3e3d3934.fbc6d6","g":"df70af48a130f602","name":"Float to int","func":"msg.media_position=Math.floor(msg.speaker_data.attributes.media_position);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2975,"y":560,"wires":[["455aa9ada539bd23"]],"l":false},{"id":"455aa9ada539bd23","type":"delay","z":"3e3d3934.fbc6d6","g":"df70af48a130f602","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":3015,"y":560,"wires":[["4964d71cfc6a381d"]],"l":false},{"id":"4964d71cfc6a381d","type":"ha-wait-until","z":"3e3d3934.fbc6d6","g":"df70af48a130f602","name":"Wait for start","server":"918a63e0.1bc5a","version":1,"outputs":2,"entityId":"{{resume_speaker}}","entityIdFilterType":"exact","property":"state","comparator":"is","value":"playing","valueType":"str","timeout":"25","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":3055,"y":560,"wires":[["c10fa6b314c14cb3"],["c10fa6b314c14cb3"]],"l":false},{"id":"c10fa6b314c14cb3","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"df70af48a130f602","name":"URL set position","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"media_seek","areaId":[],"deviceId":[],"entityId":["{{resume_speaker}}"],"data":"{    \"seek_position\": {{media_position}}}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3095,"y":560,"wires":[["19f6cb1090b213ef"]],"l":false},{"id":"336644d9ab890862","type":"api-current-state","z":"3e3d3934.fbc6d6","g":"3fdd5edae3cb1dc1","name":"Spotify current state","server":"918a63e0.1bc5a","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"{{spotify_temporary_account}}","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"spotify_temporary_data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1775,"y":320,"wires":[["b7ba3ae27f20c256"]],"l":false},{"id":"b7ba3ae27f20c256","type":"function","z":"3e3d3934.fbc6d6","g":"3fdd5edae3cb1dc1","name":"Set data","func":"msg.spotify_accounts_data[msg.counter]=msg.spotify_temporary_data;\nmsg.counter++;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1815,"y":320,"wires":[["c787d03aa23c1bed"]],"l":false},{"id":"c787d03aa23c1bed","type":"switch","z":"3e3d3934.fbc6d6","g":"3fdd5edae3cb1dc1","name":"Is it all?","property":"counter","propertyType":"msg","rules":[{"t":"gte","v":"spotify_accounts_count","vt":"msg"},{"t":"lt","v":"spotify_accounts_count","vt":"msg"}],"checkall":"false","repair":false,"outputs":2,"x":1855,"y":320,"wires":[["28bc62ecb9f97b72"],["bc998ed765619c8d"]],"l":false},{"id":"bc998ed765619c8d","type":"function","z":"3e3d3934.fbc6d6","g":"3fdd5edae3cb1dc1","name":"Set account","func":"msg.spotify_temporary_account=msg.spotify_accounts[msg.counter];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1735,"y":320,"wires":[["336644d9ab890862"]],"l":false},{"id":"28bc62ecb9f97b72","type":"change","z":"3e3d3934.fbc6d6","g":"3fdd5edae3cb1dc1","name":"Delete","rules":[{"t":"delete","p":"spotify_temporary_account","pt":"msg"},{"t":"delete","p":"spotify_temporary_data","pt":"msg"},{"t":"delete","p":"counter","pt":"msg"},{"t":"set","p":"is_right_account_found","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1895,"y":300,"wires":[["e78353045515f7ac"]],"l":false},{"id":"edf546d8678d9d28","type":"change","z":"3e3d3934.fbc6d6","g":"3fdd5edae3cb1dc1","name":"Set counter","rules":[{"t":"set","p":"counter","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1695,"y":300,"wires":[["bc998ed765619c8d"]],"l":false},{"id":"e2e2dbece34f204a","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"e4ea572d031d0ba7","name":"Select source with ID","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"spotcast","service":"start","areaId":[],"deviceId":[],"entityId":["{{resume_speaker}}"],"data":"{\"account\":\"{{right_spotcast_account}}\",\"force_playback\":true}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2895,"y":280,"wires":[["611f9592df4a7d25"]],"l":false},{"id":"d1c952b1829de173","type":"switch","z":"3e3d3934.fbc6d6","g":"e4ea572d031d0ba7","name":"Use speaker name?","property":"use_spotify_speaker_name","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":2855,"y":260,"wires":[["d3f6b9920ed775bf"],["e2e2dbece34f204a"]],"l":false},{"id":"d3f6b9920ed775bf","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"e4ea572d031d0ba7","name":"Select source with name","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"spotcast","service":"start","areaId":[],"deviceId":[],"entityId":[],"data":"{\"account\":right_spotcast_account,\"device_name\":right_spotify_account_source,\"force_playback\":true}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2895,"y":240,"wires":[["611f9592df4a7d25"]],"l":false},{"id":"b1ed0e4502e868ec","type":"api-current-state","z":"3e3d3934.fbc6d6","g":"435032474aed39f5","name":"Ytube current state","server":"918a63e0.1bc5a","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"{{ytube_temporary_account}}","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"ytube_temporary_data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1775,"y":440,"wires":[["d62a6e8382a0b0d9"]],"l":false},{"id":"d62a6e8382a0b0d9","type":"function","z":"3e3d3934.fbc6d6","g":"435032474aed39f5","name":"Set data","func":"msg.ytube_accounts_data[msg.counter]=msg.ytube_temporary_data;\nmsg.counter++;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1815,"y":440,"wires":[["a061f5aa036aac7a"]],"l":false},{"id":"a061f5aa036aac7a","type":"switch","z":"3e3d3934.fbc6d6","g":"435032474aed39f5","name":"Is it all?","property":"counter","propertyType":"msg","rules":[{"t":"gte","v":"ytube_accounts_count","vt":"msg"},{"t":"lt","v":"ytube_accounts_count","vt":"msg"}],"checkall":"false","repair":false,"outputs":2,"x":1855,"y":440,"wires":[["1589c198804e476a"],["4ce1843a0dc41d2c"]],"l":false},{"id":"4ce1843a0dc41d2c","type":"function","z":"3e3d3934.fbc6d6","g":"435032474aed39f5","name":"Set account","func":"msg.ytube_temporary_account=msg.ytube_accounts[msg.counter];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1735,"y":440,"wires":[["b1ed0e4502e868ec"]],"l":false},{"id":"1589c198804e476a","type":"change","z":"3e3d3934.fbc6d6","g":"435032474aed39f5","name":"Delete","rules":[{"t":"delete","p":"ytube_temporary_account","pt":"msg"},{"t":"delete","p":"ytube_temporary_data","pt":"msg"},{"t":"delete","p":"counter","pt":"msg"},{"t":"set","p":"is_right_account_found","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1895,"y":420,"wires":[["e873bc062009dd69"]],"l":false},{"id":"fb5d263edc8e1fa5","type":"change","z":"3e3d3934.fbc6d6","g":"435032474aed39f5","name":"Set counter","rules":[{"t":"set","p":"counter","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1695,"y":420,"wires":[["4ce1843a0dc41d2c"]],"l":false},{"id":"e873bc062009dd69","type":"function","z":"3e3d3934.fbc6d6","g":"5f09360362d5e81b","name":"Find right ytube account","func":"let speaker_id=msg.speaker;\n\nif(msg.is_it_speaker_in_group===true)\n{\n    speaker_id=msg.speakers_groups_data[msg.which_speaker_group].entity_id;\n}\n\nfor(i=0;i<msg.ytube_accounts_count;i++)\n{\n    if(msg.media_type==\"ytube\" && speaker_id==msg.ytube_accounts_data[i].attributes.remote_player_id && msg.ytube_accounts_data[i].state===\"playing\")\n    {\n        msg.is_right_account_found=true;\n        msg.right_ytube_account=msg.ytube_accounts[i];\n        break;\n    }\n    else if(msg.media_type==\"YouTube\" && speaker_id==msg.ytube_accounts_data[i].attributes.remote_player_id)\n    {\n        msg.is_right_account_found=true;\n        msg.right_ytube_account=msg.ytube_accounts[i];\n        break;\n    }\n}\nif(msg.media_type==\"YouTube\" && !msg.is_right_account_found)\n{\n    for(i=0;i<msg.ytube_accounts_count;i++)\n    {\n        if(msg.media_type==\"YouTube\" && msg.ytube_accounts_data[i].state!==\"playing\")\n        {\n            msg.is_right_account_found=true;\n            msg.right_ytube_account=msg.ytube_accounts[i];\n            break;\n        }\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1995,"y":460,"wires":[["e6e24ed61aa7f756"]],"l":false},{"id":"e6e24ed61aa7f756","type":"switch","z":"3e3d3934.fbc6d6","g":"5f09360362d5e81b","name":"Is right account found","property":"is_right_account_found","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":2035,"y":460,"wires":[["21bebf04e67eea85"],["b7fafb402587e5ed"]],"l":false},{"id":"b7fafb402587e5ed","type":"change","z":"3e3d3934.fbc6d6","g":"5f09360362d5e81b","name":"Set media type to NONE","rules":[{"t":"set","p":"media_type","pt":"msg","to":"NONE","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2075,"y":480,"wires":[["7cc5fccec52efc64"]],"l":false},{"id":"21bebf04e67eea85","type":"switch","z":"3e3d3934.fbc6d6","g":"5f09360362d5e81b","name":"Is it YT?","property":"media_type","propertyType":"msg","rules":[{"t":"eq","v":"ytube","vt":"str"},{"t":"eq","v":"YouTube","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":2115,"y":460,"wires":[["23a123904807c5dc"],["7cc5fccec52efc64"]],"l":false},{"id":"23a123904807c5dc","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"5f09360362d5e81b","name":"Interrupt start","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"ytube_music_player","service":"call_method","areaId":[],"deviceId":[],"entityId":["{{right_ytube_account}}"],"data":"{\"command\":\"interrupt_start\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2175,"y":440,"wires":[["7cc5fccec52efc64"]],"l":false},{"id":"df4dbbbcd9bab984","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Interrupt resume","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"ytube_music_player","service":"call_method","areaId":[],"deviceId":[],"entityId":["{{right_ytube_account}}"],"data":"{\"command\":\"interrupt_resume\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2855,"y":380,"wires":[["d532d5a452af51ea"]],"l":false},{"id":"2ea3596d48999a54","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Set output","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"select_source","areaId":[],"deviceId":[],"entityId":["media_player.ytube_music_player"],"data":"{\"source\": resume_speaker_friendly_name}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2975,"y":420,"wires":[["90f3d929d2f350a9"]],"l":false},{"id":"c881ccccfc5106c8","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Play media","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"play_media","areaId":[],"deviceId":[],"entityId":["media_player.ytube_music_player"],"data":"{\"media_content_id\": ytube_extra_data.attributes.search[0].id, \"media_content_type\": \"album\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3055,"y":420,"wires":[["adf6e8ed5be0fba7"]],"l":false},{"id":"96e41efe7416310d","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"URL set position","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"media_seek","areaId":[],"deviceId":[],"entityId":["{{resume_speaker}}"],"data":"{\"seek_position\": {{media_position}}}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3505,"y":400,"wires":[["28d6a3cd2a0a567d"]],"l":false},{"id":"8c17ed997d95b47c","type":"ha-wait-until","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Wait for start","server":"918a63e0.1bc5a","version":1,"outputs":2,"entityId":"{{resume_speaker}}","entityIdFilterType":"exact","property":"state","comparator":"is","value":"playing","valueType":"str","timeout":"10","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":3175,"y":420,"wires":[["ad9d18aec39e64cc"],["ad9d18aec39e64cc"]],"l":false},{"id":"946db825271e70fe","type":"delay","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":3135,"y":420,"wires":[["8c17ed997d95b47c"]],"l":false},{"id":"90f3d929d2f350a9","type":"delay","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":3015,"y":420,"wires":[["c881ccccfc5106c8"]],"l":false},{"id":"adf6e8ed5be0fba7","type":"function","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Float to int","func":"msg.media_position=Math.floor(msg.speaker_data.attributes.media_position);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":3095,"y":420,"wires":[["946db825271e70fe"]],"l":false},{"id":"3b512a399c7eacb5","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"b9c102631689476d","name":"Pause speaker","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"media_pause","areaId":[],"deviceId":[],"entityId":["{{speaker}}"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2395,"y":580,"wires":[["2d5633eadb576528"]],"l":false},{"id":"b6176ac0e7c4a21c","type":"delay","z":"3e3d3934.fbc6d6","g":"b9c102631689476d","name":"delay","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2595,"y":600,"wires":[["356ad3b53d40d040"]],"l":false},{"id":"9b8c5c2b1e178dc9","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"b9c102631689476d","name":"Set volume","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"volume_set","areaId":[],"deviceId":[],"entityId":["{{speaker}}"],"data":"{\"volume_level\":\"{{volume}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2475,"y":580,"wires":[["77c5dbb579d16c3e"]],"l":false},{"id":"356ad3b53d40d040","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"b9c102631689476d","name":"Set volume","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"volume_set","areaId":[],"deviceId":[],"entityId":["{{speaker}}"],"data":"{\"volume_level\":\"{{speaker_data.attributes.volume_level}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2635,"y":600,"wires":[["999e89fc46396b94"]],"l":false},{"id":"2d5633eadb576528","type":"switch","z":"3e3d3934.fbc6d6","g":"b9c102631689476d","name":"","property":"volume","propertyType":"msg","rules":[{"t":"btwn","v":"0","vt":"num","v2":"1","v2t":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":2435,"y":600,"wires":[["9b8c5c2b1e178dc9"],["77c5dbb579d16c3e"]],"l":false},{"id":"77c5dbb579d16c3e","type":"function","z":"3e3d3934.fbc6d6","g":"b9c102631689476d","name":"Setup alexa notify service and calculate time","func":"msg.alexa_notify_service=\"alexa_media_\"+msg.speaker.substring(13);\nmsg.delay=((msg.message.length/12)+1)*1000;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2515,"y":600,"wires":[["a74b0f14555de090"]],"l":false},{"id":"a74b0f14555de090","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"b9c102631689476d","name":"Alexa TTS","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"notify","service":"{{alexa_notify_service}}","areaId":[],"deviceId":[],"entityId":[],"data":"{\"message\":\"{{message}}\",\"data\":{\"type\":\"tts\"}}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2555,"y":600,"wires":[["b6176ac0e7c4a21c"]],"l":false},{"id":"2505628870b85dc6","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"b9c102631689476d","name":"Play speaker","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"media_play","areaId":[],"deviceId":[],"entityId":["{{speaker}}"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2715,"y":600,"wires":[["d665a3a0666d9e73"]],"l":false},{"id":"99d22c066760b088","type":"switch","z":"3e3d3934.fbc6d6","g":"b9c102631689476d","name":"Pause?","property":"alexa_pause_on_tts","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":2355,"y":600,"wires":[["3b512a399c7eacb5"],["2d5633eadb576528"]],"l":false},{"id":"999e89fc46396b94","type":"delay","z":"3e3d3934.fbc6d6","g":"b9c102631689476d","name":"delay","pauseType":"delayv","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2675,"y":600,"wires":[["2505628870b85dc6"]],"l":false},{"id":"ab7ec373068e28fb","type":"function","z":"3e3d3934.fbc6d6","g":"4b9d74d12cbd3e65","name":"Debug","func":"node.warn(\"Smart TTS ERROR: \");\nnode.warn(msg.error);\nif(msg.debug)\n{\n    node.warn(\"Smart TTS debug output: \");\n    node.warn(msg);\n}","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":295,"y":500,"wires":[],"icon":"font-awesome/fa-bug","l":false},{"id":"5a1f368902eb0a12","type":"api-current-state","z":"3e3d3934.fbc6d6","g":"df3d995d84e9d2a1","name":"Group current state","server":"918a63e0.1bc5a","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"{{speakers_temporary_group}}","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"speakers_group_temporary_data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":555,"y":360,"wires":[["439642d88e48109f"]],"l":false},{"id":"439642d88e48109f","type":"function","z":"3e3d3934.fbc6d6","g":"df3d995d84e9d2a1","name":"Set data","func":"msg.speakers_groups_data[msg.counter]=msg.speakers_group_temporary_data;\nmsg.counter++;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":595,"y":360,"wires":[["b1a1f2a6ba107ea2"]],"l":false},{"id":"b1a1f2a6ba107ea2","type":"switch","z":"3e3d3934.fbc6d6","g":"df3d995d84e9d2a1","name":"Is it all?","property":"counter","propertyType":"msg","rules":[{"t":"gte","v":"speakers_groups_count","vt":"msg"},{"t":"lt","v":"speakers_groups_count","vt":"msg"}],"checkall":"false","repair":false,"outputs":2,"x":635,"y":360,"wires":[["c7a912d0f8fc62de"],["bb833fe4ab6e20b8"]],"l":false},{"id":"bb833fe4ab6e20b8","type":"function","z":"3e3d3934.fbc6d6","g":"df3d995d84e9d2a1","name":"Set group","func":"msg.speakers_temporary_group=msg.speakers_groups[msg.counter];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":515,"y":360,"wires":[["5a1f368902eb0a12"]],"l":false},{"id":"c7a912d0f8fc62de","type":"change","z":"3e3d3934.fbc6d6","g":"df3d995d84e9d2a1","name":"Delete","rules":[{"t":"delete","p":"speakers_temporary_group","pt":"msg"},{"t":"delete","p":"speakers_group_temporary_data","pt":"msg"},{"t":"delete","p":"counter","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":675,"y":340,"wires":[["743515015cc33efd"]],"l":false},{"id":"b1c761bed53d40a9","type":"change","z":"3e3d3934.fbc6d6","g":"df3d995d84e9d2a1","name":"Set counter","rules":[{"t":"set","p":"counter","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":475,"y":340,"wires":[["bb833fe4ab6e20b8"]],"l":false},{"id":"743515015cc33efd","type":"function","z":"3e3d3934.fbc6d6","g":"3d1bdae6980660a7","name":"Group type rocognition","func":"for(i=0;i<msg.speakers_groups_count;i++)\n{\n    if(msg.speaker===msg.speakers_groups[i])\n    {\n        msg.is_it_group=true;\n        msg.is_it_speaker_in_group=false;\n        msg.which_speaker_group=i;\n        break;\n    }\n}\nif(msg.is_it_group===false)\n{\n    for(i=0;i<msg.speakers_groups_count;i++)\n    {\n        if(msg.speaker_data.state===msg.speakers_groups_data[i].state && \n        msg.speaker_data.attributes.media_content_id===msg.speakers_groups_data[i].attributes.media_content_id && \n        msg.speakers_groups_data[i].state===\"playing\")\n        {\n            for(j=0;j<msg.speakers_inside_groups[i].length;j++)\n            {\n                if(msg.speaker===msg.speakers_inside_groups[i][j])\n                {\n                    msg.is_it_group=false;\n                    msg.is_it_speaker_in_group=true;\n                    msg.which_speaker_group=i;\n                    break;\n                }\n            }\n        }\n        if(msg.is_it_speaker_in_group===true)break;\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":775,"y":360,"wires":[["514a87c252fde2f9"]],"l":false},{"id":"514a87c252fde2f9","type":"switch","z":"3e3d3934.fbc6d6","g":"3d1bdae6980660a7","name":"Is it group?","property":"is_it_group","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":815,"y":360,"wires":[["76e6aa845545b821"],["ada76fa963fee4ed"]],"l":false},{"id":"76e6aa845545b821","type":"switch","z":"3e3d3934.fbc6d6","g":"3d1bdae6980660a7","name":"Is group not playing?","property":"speaker_data.state","propertyType":"msg","rules":[{"t":"neq","v":"playing","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":855,"y":340,"wires":[["fcae45f78aa92775"],["ada76fa963fee4ed"]],"l":false},{"id":"384861426abe5b8c","type":"api-current-state","z":"3e3d3934.fbc6d6","g":"dc7b3b8c84ba81b3","name":"Speaker current state","server":"918a63e0.1bc5a","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"{{speaker_temporary}}","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"speaker_temporary_state","propertyType":"msg","value":"","valueType":"entityState"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1035,"y":360,"wires":[["31d6ba68118e9ac6"]],"l":false},{"id":"31d6ba68118e9ac6","type":"function","z":"3e3d3934.fbc6d6","g":"dc7b3b8c84ba81b3","name":"Set data","func":"msg.speakers_inside_group_state[msg.counter]=msg.speaker_temporary_state;\nmsg.counter++;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1075,"y":360,"wires":[["d294aed085fda93c"]],"l":false},{"id":"d294aed085fda93c","type":"switch","z":"3e3d3934.fbc6d6","g":"dc7b3b8c84ba81b3","name":"Is it all?","property":"counter","propertyType":"msg","rules":[{"t":"gte","v":"speakers_inside_group_count","vt":"msg"},{"t":"lt","v":"speakers_inside_group_count","vt":"msg"}],"checkall":"false","repair":false,"outputs":2,"x":1115,"y":360,"wires":[["addaed19b41880be"],["7dd78dad790672e8"]],"l":false},{"id":"7dd78dad790672e8","type":"function","z":"3e3d3934.fbc6d6","g":"dc7b3b8c84ba81b3","name":"Set speaker","func":"msg.speaker_temporary=msg.speakers_inside_groups[msg.which_speaker_group][msg.counter];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":995,"y":360,"wires":[["384861426abe5b8c"]],"l":false},{"id":"addaed19b41880be","type":"change","z":"3e3d3934.fbc6d6","g":"dc7b3b8c84ba81b3","name":"Delete","rules":[{"t":"delete","p":"msg.speaker_temporary","pt":"msg"},{"t":"delete","p":"msg.speaker_temporary_state","pt":"msg"},{"t":"delete","p":"counter","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1155,"y":340,"wires":[["6f211fb74b2b7f49"]],"l":false},{"id":"fcae45f78aa92775","type":"function","z":"3e3d3934.fbc6d6","g":"dc7b3b8c84ba81b3","name":"Set variables","func":"msg.counter=0;\nmsg.speakers_inside_group_count=msg.speakers_inside_groups[msg.which_speaker_group].length;\nmsg.speakers_inside_group_state=new Array(msg.speakers_inside_group_count);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":955,"y":340,"wires":[["7dd78dad790672e8"]],"icon":"node-red/swap.svg","l":false},{"id":"6f211fb74b2b7f49","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"fa06bc39f6a3f587","name":"Turn on speaker","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"turn_on","areaId":[],"deviceId":[],"entityId":["{{speaker}}"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1255,"y":340,"wires":[["c43eca78f57978d1"]],"l":false},{"id":"c43eca78f57978d1","type":"ha-wait-until","z":"3e3d3934.fbc6d6","g":"fa06bc39f6a3f587","name":"Wait for turning on","server":"918a63e0.1bc5a","version":1,"outputs":2,"entityId":"{{speaker}}","entityIdFilterType":"exact","property":"state","comparator":"is_not","value":"off","valueType":"str","timeout":"5","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":1295,"y":340,"wires":[["4a4e522685dc3866"],["4a4e522685dc3866"]],"l":false},{"id":"4a4e522685dc3866","type":"api-current-state","z":"3e3d3934.fbc6d6","g":"fa06bc39f6a3f587","name":"Speaker current state","server":"918a63e0.1bc5a","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"{{speaker}}","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"speaker_data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1335,"y":340,"wires":[["8d38296d8f450521"]],"l":false},{"id":"1e1403ab211b4b11","type":"catch","z":"3e3d3934.fbc6d6","g":"4b9d74d12cbd3e65","name":"","scope":null,"uncaught":false,"x":200,"y":500,"wires":[["ab7ec373068e28fb"]]},{"id":"80f20973bdbdf8bc","type":"switch","z":"3e3d3934.fbc6d6","g":"847406122c07558c","name":"Use groups?","property":"speakers_groups_count","propertyType":"msg","rules":[{"t":"gte","v":"1","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":375,"y":360,"wires":[["b1c761bed53d40a9"],["3e90927058b554e0"]],"l":false},{"id":"55925a42891ddc78","type":"function","z":"3e3d3934.fbc6d6","g":"847406122c07558c","name":"CONFIG","func":"// +-----------+\n// | Smart TTS |\n// +-----------+\n\n//By IT freak Jake\n//Version: 1.3.2\n//Node-RED project page link:\n//https://flows.nodered.org/flow/d0f137bc6323fd8200b8221cea22d713\n\n// +--------+\n// | Config |\n// +--------+\n\n//Media players ID's from Spotify integration\n//If you don't use spotify make it empty like example below\n//msg.spotify_accounts=[];\n//(Remember about removing example media players)\nmsg.spotify_accounts =\n    [\n        \"media_player.spotify_account_1\",\n        \"media_player.spotify_account_2\",\n        \"media_player.spotify_account_3\",\n        \"media_player.spotify_account_4\"\n    ];\n\n//Accounts from Spotcast integration\n//(In the same order as spotify accounts)\n//If you don't use spotify make it empty like example below\n//msg.spotcast_accounts=[];\n//(Remember about removing example media players)\nmsg.spotcast_accounts =\n    [\n        \"default\",\n        \"account_2\",\n        \"account_3\",\n        \"account_4\"\n    ];\n\n//Media players ID's from ytube_music_player integration \n//If you don't use YouTube music make it empty like example below\n//msg.ytube_accounts=[];\n//(Remember about removing example media players)\nmsg.ytube_accounts =\n    [\n        \"media_player.ytube_account_1\",\n        \"media_player.ytube_account_2\",\n        \"media_player.ytube_account_3\",\n        \"media_player.ytube_account_4\"\n    ];\n\n//Define dafult Google TTS language\nconst default_tts_language = \"en\"\n\n//Speakers groups ID's\n//If you don't use groups make it empty like example below\n//msg.speakers_groups=[];\n//(Remember about removing example media players)\nmsg.speakers_groups =\n    [\n        \"media_player.group1\",\n        \"media_player.group2\",\n        \"media_player.group3\"\n    ];\n\n//Speakers inside groups ID's\n//(In the same order as speakers groups ID's)\n//If you don't use groups make it empty like example below\n//msg.speakers_inside_groups=[];\n//(Remember about removing example media players)\nmsg.speakers_inside_groups =\n    [\n        [//Speakers in media_player.group1\n            \"media_player.speaker1_in_group1\",\n            \"media_player.speaker2_in_group1\",\n            \"media_player.speaker3_in_group1\"\n        ],\n        [//Speakers in media_player.group2\n            \"media_player.speaker1_in_group2\",\n            \"media_player.speaker2_in_group2\",\n            \"media_player.speaker3_in_group2\"\n        ],\n        [//Speakers in media_player.group3\n            \"media_player.speaker1_in_group3\",\n            \"media_player.speaker2_in_group3\",\n            \"media_player.speaker3_in_group3\",\n            \"media_player.speaker4_in_group3\"\n        ]\n    ];\n\n//Default TTS service like google_translate_say or reversotts_say\n//For Alexa it's alexa_default\nconst default_tts_service = \"google_translate_say\";\n\n//Pause media when calling TTS on Alexa speaker\nmsg.alexa_pause_on_tts = true;\n\n// +----------+\n// | Advanced |\n// +----------+\n\n//If the speaker and Spotify has other media ID but \n//the Spotify account is the only one playing \n//and speaker is playing from Spotify\n//smart TTS is using this Spotify account to resume.\n//Changing this variable on true will disable this function \nmsg.spotify_require_identical_media_content_id = false;\n\n//Compare speaker name with Spotify source name instead\n//of comparing media content ID when looking for Spotify\n//account which is playing on the speaker\nmsg.compare_spotify_speaker_name = true;\n\n//Use Spotify speaker name instead of HA media player ID\n//when using Spotcast service\n//(Speaker friendly name and Spotify source\n//speaker name must be this same)\nmsg.use_spotify_speaker_name = true;\n\n//Smart TTS have to skip all tracks in the album\n//from first one to the one it wants to resume.\n//This number defines how many skips it can make.\nmsg.max_ytube_track_skips = 100\n\n//Delete variables used in smart TTS on end\nmsg.delete_variables = true;\n\n//Enable debug output\nmsg.debug = false;\n\n\n// +----------------+\n// | DO NOT CHANGE! |\n// +----------------+\n\nlet is_config_ok = true,\n    spotify_exist = true,\n    spotcast_exist = true,\n    speakers_groups_exist = true,\n    speakers_inside_groups_exist = true;\nmsg.is_it_group = false;\nmsg.is_it_speaker_in_group = false;\n\nif (typeof (msg.speaker) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: Speaker ID is missing!');\n}\nif (typeof (msg.message) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: TTS message is missing!');\n}\nif (typeof (default_tts_service) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: \"default_tts_service\" is missing!');\n}\nif (typeof (msg.spotify_require_identical_media_content_id) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: \"msg.spotify_require_identical_media_id\" is missing!');\n}\nif (typeof (msg.compare_spotify_speaker_name) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: \"msg.compare_spotify_speaker_name\" is missing!');\n}\nif (typeof (msg.use_spotify_speaker_name) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: \"msg.use_spotify_speaker_name\" is missing!');\n}\nif (typeof (msg.delete_variables) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: \"msg.delete_variables\" is missing!');\n}\nif (typeof (msg.debug) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: \"msg.debug\" is missing!');\n}\nif (typeof (msg.spotify_accounts) === 'undefined') {\n    is_config_ok = false;\n    spotify_exist = false;\n    node.warn('Smart TTS: \"msg.spotify_accounts[]\" is missing!');\n}\nif (typeof (msg.spotcast_accounts) === 'undefined') {\n    is_config_ok = false;\n    spotcast_exist = false;\n    node.warn('Smart TTS: \"msg.spotcast_accounts[]\" is missing!');\n}\nif (typeof (msg.ytube_accounts) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: \"msg.ytube_accounts[]\" is missing!');\n}\nif (typeof (msg.speakers_groups) === 'undefined') {\n    is_config_ok = false;\n    speakers_groups_exist = false;\n    node.warn('Smart TTS: \"msg.speakers_groups[]\" is missing!');\n}\nif (typeof (msg.speakers_inside_groups) === 'undefined') {\n    is_config_ok = false;\n    speakers_inside_groups_exist = false;\n    node.warn('Smart TTS: \"msg.speakers_inside_groups[]\" is missing!');\n}\nif (typeof (msg.alexa_pause_on_tts) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: \"msg.alexa_pause_on_tts\" is missing!');\n}\nif (typeof (msg.max_ytube_track_skips) === 'undefined') {\n    is_config_ok = false;\n    node.warn('Smart TTS: \"msg.max_ytube_track_skips\" is missing!');\n}\n\nif (spotify_exist && spotcast_exist) {\n    if (msg.spotify_accounts.length !== msg.spotcast_accounts.length) {\n        is_config_ok = false;\n        node.warn('Smart TTS: \"msg.spotify_accounts[]\" and \"msg.spotcast_accounts[]\" must have the same number of variables!');\n    }\n}\nif (speakers_groups_exist && speakers_inside_groups_exist) {\n    if (msg.speakers_groups.length !== msg.speakers_inside_groups.length) {\n        is_config_ok = false;\n        node.warn('Smart TTS: \"msg.speakers_groups[]\" and \"msg.speakers_inside_groups[]\" must have the same number of variables!');\n    }\n}\n\n\nif (is_config_ok) {\n    msg.spotify_accounts_count = msg.spotify_accounts.length;\n    msg.spotify_accounts_data = new Array(msg.spotify_accounts_count);\n    msg.ytube_accounts_count = msg.ytube_accounts.length;\n    msg.ytube_accounts_data = new Array(msg.ytube_accounts_count);\n    msg.speakers_groups_count = msg.speakers_groups.length;\n    msg.speakers_groups_data = new Array(msg.speakers_groups_count);\n    if (typeof (msg.tts_service) === 'undefined') msg.tts_service = default_tts_service;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":160,"y":380,"wires":[["dd5da2561d1da094"]],"icon":"node-red/cog.svg"},{"id":"d60a7a0a3eaaea02","type":"api-current-state","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Ytube extra state","server":"918a63e0.1bc5a","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.ytube_music_player_extra","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"ytube_extra_data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":2935,"y":420,"wires":[["2ea3596d48999a54"]],"l":false},{"id":"e98edb9d16d9aec5","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Search","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"ytube_music_player","service":"search","areaId":[],"deviceId":[],"entityId":["{{right_ytube_account}}"],"data":"{\"limit\":1,\"filter\":\"albums\",\"query\":\"{{speaker_data.attributes.media_album_name}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2895,"y":420,"wires":[["d60a7a0a3eaaea02"]],"l":false},{"id":"80aee4a5ef699baa","type":"api-current-state","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Ytube extra state","server":"918a63e0.1bc5a","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.ytube_music_player_extra","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"ytube_extra_data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":3265,"y":420,"wires":[["ca2f33b9fc9572a9"]],"l":false},{"id":"ad9d18aec39e64cc","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Pause speaker","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"media_pause","areaId":[],"deviceId":[],"entityId":["{{resume_speaker}}"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3225,"y":420,"wires":[["80aee4a5ef699baa"]],"l":false},{"id":"ca2f33b9fc9572a9","type":"function","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Find track","func":"function find_track(tracks, index)\n{\n    let position = tracks.search(\" - \");\n    tracks = tracks.substring(position + 3, tracks.length);\n    msg.substr_tracks.push(tracks);\n    if(tracks == msg.speaker_data.attributes.media_title)\n    {\n        return index;\n    }\n}\nmsg.substr_tracks = [];\nlet track = msg.ytube_extra_data.attributes.tracks.findIndex(find_track);\nif(track > -1)\n{\n    if (track <= msg.max_ytube_track_skips)\n    {\n        msg.ytube_track_index = track;\n    }\n    else\n    {\n        msg.ytube_track_index = 0;\n        node.warn(\"Track is too far in the album\");\n    }\n    \n}\nelse\n{\n    msg.ytube_track_index = 0;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":3305,"y":420,"wires":[["39c90d67cd0f5cea"]],"l":false},{"id":"3523cef71a9021b4","type":"function","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Set data","func":"msg.counter++;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":3465,"y":460,"wires":[["dd1c235495fa7fb2"]],"l":false},{"id":"dd1c235495fa7fb2","type":"switch","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Is it all?","property":"counter","propertyType":"msg","rules":[{"t":"gte","v":"ytube_track_index","vt":"msg"},{"t":"lt","v":"ytube_track_index","vt":"msg"}],"checkall":"false","repair":false,"outputs":2,"x":3505,"y":460,"wires":[["7d4d1af7df388375"],["3a4ed97ef4f4f5ec"]],"l":false},{"id":"7d4d1af7df388375","type":"change","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Delete","rules":[{"t":"delete","p":"counter","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":3545,"y":440,"wires":[["96e41efe7416310d"]],"l":false},{"id":"15300a68068466ab","type":"change","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Set counter","rules":[{"t":"set","p":"counter","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":3385,"y":440,"wires":[["3a4ed97ef4f4f5ec"]],"l":false},{"id":"3a4ed97ef4f4f5ec","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Next track","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"media_next_track","areaId":[],"deviceId":[],"entityId":["media_player.ytube_music_player"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3425,"y":460,"wires":[["3523cef71a9021b4"]],"l":false},{"id":"39c90d67cd0f5cea","type":"switch","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Next track?","property":"ytube_track_index","propertyType":"msg","rules":[{"t":"lte","v":"0","vt":"num"},{"t":"gt","v":"0","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":3345,"y":420,"wires":[["96e41efe7416310d"],["15300a68068466ab"]],"l":false},{"id":"136cbc037a0be9e3","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Mute","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"volume_mute","areaId":[],"deviceId":[],"entityId":["{{tts_speaker}}"],"data":"{\"is_volume_muted\":\"True\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2855,"y":420,"wires":[["e98edb9d16d9aec5"]],"l":false},{"id":"28d6a3cd2a0a567d","type":"api-call-service","z":"3e3d3934.fbc6d6","g":"8db273c85d1f4b04","name":"Mute","server":"918a63e0.1bc5a","version":5,"debugenabled":false,"domain":"media_player","service":"volume_mute","areaId":[],"deviceId":[],"entityId":["{{tts_speaker}}"],"data":"{\"is_volume_muted\":\"False\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3545,"y":400,"wires":[["d532d5a452af51ea"]],"l":false},{"id":"918a63e0.1bc5a","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}]

Flow Info

Created 3 years, 8 months ago
Updated 2 years ago
Rating: 5 3

Actions

Rate:

Node Types

Core
  • catch (x1)
  • change (x15)
  • comment (x1)
  • delay (x8)
  • function (x22)
  • inject (x4)
  • switch (x25)
Other

Tags

  • HomeAssistant
  • TTS
  • GoogleHome
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option