How to cast BBC Sounds radio station in Node-Red as part of a flow

Hello

I am using Google Home as a doorbell chime, linked to an Aqara wireless switch in a Node-red flow. The flow grabs the media player current state so it can later restore the volume and also decide whether to restart the radio stream. The chime works fine and after playing, the home returns the volume to its prior state. The problem I am having is how to reliably return the home to the radio station that was playing. For now, I am happy to hardcode to 1 station (BBC Radio 2) - so if it was playing then I want it to simply start it up again after the chime. In the future I’d like to make this a bit more dynamic but one step at a time for now.

I have pasted my flow below (with inject buttons for testing). The “Play” button works great, Radio 2 starts up every time although note that I have to call play_media twice for some reason. The first simply turns the Home from off to idle then the second one starts playback. Replacing the first call with turn_on did not work reliably. When this part of the flow is triggered from the Test button after playing the chime, the radio doesn’t play. In the Google Home Android app, it shows as “playing” with the name of the station showing but there is no sound and no pause button. All I can do in the app is change the volume or stop casting.

I’m not sure if this problem is unique to BBC Sounds or my chime nodes are doing something to the Google Home that messes up the subsequent radio cast or if I’m doing something else wrong in Node Red. I’ve been trying to get this working for a few weeks! Any help welcome. Thanks in advance.

Useful links: https://community.home-assistant.io/t/google-cast-play-media-from-app-bbc-radio/305644
https://www.home-assistant.io/integrations/cast/

This is what I am trying to achieve:

  1. Button is pressed

  2. Get speaker state

  3. If its off, turn it on and wait until its not off

  4. Set vol to a desired level (currently 5% so I don’t annoy my family trying to get it to work!)

  5. Play chime

  6. Wait 25s for it to finish

  7. Reset the volume (currently working if fed from a function as I couldn’t set vol to a previously captured variable in the service node (string vs float or general syntax errors)

  8. If previously captured content id was bbc_radio_two then cast this again (the broken bit)

  9. Wait a few secs and repeat step 8 if its not playing (happy to lose this step if anyone knows why I’m having to repeat it!)

[{"id":"7d27e0b530feec59","type":"inject","z":"e5a279481ebabf5f","name":"Test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":90,"y":340,"wires":[["cb49f1bb55e105b3"]]},{"id":"cb49f1bb55e105b3","type":"api-current-state","z":"e5a279481ebabf5f","name":"Speaker","server":"a8e8332.92727d","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"media_player.kitchen_speaker","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"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":120,"y":260,"wires":[["b749f7c59eb60e01"]]},{"id":"b749f7c59eb60e01","type":"switch","z":"e5a279481ebabf5f","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"off","vt":"str"},{"t":"neq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":260,"wires":[["0688c2a95877999f"],["3e7b36c7713bb008"]]},{"id":"0688c2a95877999f","type":"api-call-service","z":"e5a279481ebabf5f","name":"Speaker On","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"turn_on","entityId":"media_player.kitchen_speaker","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":450,"y":220,"wires":[["5053a5bfada1a4b7"]]},{"id":"5053a5bfada1a4b7","type":"ha-wait-until","z":"e5a279481ebabf5f","name":"","server":"a8e8332.92727d","version":0,"outputs":2,"entityId":"media_player.kitchen_speaker","entityIdFilterType":"exact","property":"state","comparator":"is_not","value":"off","valueType":"str","timeout":"5","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"data","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":600,"y":220,"wires":[["6a940cab9da17b71"],[]]},{"id":"6a940cab9da17b71","type":"api-current-state","z":"e5a279481ebabf5f","name":"Speaker","server":"a8e8332.92727d","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"media_player.kitchen_speaker","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"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":760,"y":220,"wires":[["3e7b36c7713bb008"]]},{"id":"3e7b36c7713bb008","type":"api-call-service","z":"e5a279481ebabf5f","name":"Set Vol","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.kitchen_speaker","data":"{\"volume_level\": 0.05}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":440,"y":300,"wires":[["792f7a13d192b31d"]]},{"id":"792f7a13d192b31d","type":"api-call-service","z":"e5a279481ebabf5f","name":"Chime","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"play_media","entityId":"media_player.kitchen_speaker","data":"{\"media_content_type\": \"audio/mp3\", \"media_content_id\":\"http://192.168.1.65:8123/local/Doorbell_Let_Em_In.mp3\"}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":590,"y":300,"wires":[["d6dfe34522338967"]]},{"id":"d6dfe34522338967","type":"delay","z":"e5a279481ebabf5f","name":"","pauseType":"delay","timeout":"25","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":440,"y":360,"wires":[["4445ee5bf0444b5f","3f6f1aa2776397a1"]]},{"id":"4445ee5bf0444b5f","type":"function","z":"e5a279481ebabf5f","name":"","func":"msg.payload =\n{\n    \"data\":{\n      \"entity_id\":\"media_player.kitchen_speaker\",\n      \"volume_level\": msg.speaker_data.attributes.volume_level \n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":360,"wires":[["225b505e028da429"]]},{"id":"225b505e028da429","type":"api-call-service","z":"e5a279481ebabf5f","name":"Reset Vol","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"","data":"{} ","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":760,"y":360,"wires":[[]]},{"id":"3f6f1aa2776397a1","type":"switch","z":"e5a279481ebabf5f","name":"","property":"speaker_data.attributes.media_content_id","propertyType":"msg","rules":[{"t":"eq","v":"bbc_radio_two","vt":"str"},{"t":"eq","v":"bbc_radio_five_live","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":590,"y":420,"wires":[["cc393b61b13bf77d"],["1a3107010771ee49"]]},{"id":"1a3107010771ee49","type":"api-call-service","z":"e5a279481ebabf5f","name":"R5","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"play_media","entityId":"media_player.kitchen_speaker","data":"{     \"media_content_type\": \"cast\",     \"media_content_id\": \" { \\\"app_name\\\": \\\"bbcsounds\\\", \\\"media_id\\\": \\\"bbc_radio_five_live\\\", \\\"is_live\\\": true }\",     \"extra\": {       \"metadata\": {         \"metadataType\": 0,         \"title\": \"Radio 5 live\",         \"images\": [           {             \"url\": \"https://sounds.files.bbci.co.uk/2.3.0/networks/bbc_radio_five_live/background_1280x720.png\"           }         ]       }     }   }","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":750,"y":480,"wires":[[]]},{"id":"ce80de8d0a2432a1","type":"api-current-state","z":"e5a279481ebabf5f","name":"Speaker","server":"a8e8332.92727d","version":3,"outputs":2,"halt_if":"playing","halt_if_type":"str","halt_if_compare":"is_not","entity_id":"media_player.kitchen_speaker","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"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":1000,"y":420,"wires":[["b96d05fa49709507"],[]]},{"id":"947b8325c4211ac7","type":"delay","z":"e5a279481ebabf5f","name":"12s","pauseType":"delay","timeout":"12","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":870,"y":420,"wires":[["ce80de8d0a2432a1"]]},{"id":"b96d05fa49709507","type":"api-call-service","z":"e5a279481ebabf5f","name":"R2","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"play_media","entityId":"media_player.kitchen_speaker","data":"{\t    \"media_content_type\": \"cast\",\t    \"media_content_id\": \" { \\\"app_name\\\": \\\"bbcsounds\\\", \\\"media_id\\\": \\\"bbc_radio_two\\\", \\\"is_live\\\": true }\",\t    \"extra\": {\t      \"metadata\": {\t        \"metadataType\": 0,\t        \"title\": \"Radio 2\",\t        \"images\": [\t          {\t            \"url\": \"https://sounds.files.bbci.co.uk/2.3.0/networks/bbc_radio_two/background_1280x720.png\"\t          }\t        ]\t      }\t    }\t  }","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1130,"y":420,"wires":[[]]},{"id":"cc393b61b13bf77d","type":"api-call-service","z":"e5a279481ebabf5f","name":"R2","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"play_media","entityId":"media_player.kitchen_speaker","data":"{\t    \"media_content_type\": \"cast\",\t    \"media_content_id\": \" { \\\"app_name\\\": \\\"bbcsounds\\\", \\\"media_id\\\": \\\"bbc_radio_two\\\", \\\"is_live\\\": true }\",\t    \"extra\": {\t      \"metadata\": {\t        \"metadataType\": 0,\t        \"title\": \"Radio 2\",\t        \"images\": [\t          {\t            \"url\": \"https://sounds.files.bbci.co.uk/2.3.0/networks/bbc_radio_two/background_1280x720.png\"\t          }\t        ]\t      }\t    }\t  }","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":730,"y":420,"wires":[["947b8325c4211ac7"]]},{"id":"853f8c097d73fcfe","type":"inject","z":"e5a279481ebabf5f","name":"Play","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":530,"y":500,"wires":[["cc393b61b13bf77d"]]},{"id":"a8e8332.92727d","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}]

What happens if you have a event state node as the start (or in a different sequence now for testing).
It should trigger when the state changes and give you previous state and new state as two objects with data.
Perhaps there is what you need in the previous state?

Thanks for the reply. Do you mean add one between the switch and first R2 node? I will give it a try.

No…
Add a new events state as a new start.
Then connect it to a debug set to complete message.

Start a radio
Clear the debug window
Press the door bell.

Paste what the debug window gave you here.

The plan at this stage is just to see what your media player holds. If there is a link that we can use to send back to the media player after the door bell.

At a later stage we connect this to the main sequence to make it work as intended.

Ok, thanks for clarifying. There are 4 messages pasted below.

{"entity_id":"media_player.kitchen_speaker","old_state":{"entity_id":"media_player.kitchen_speaker","state":"playing","attributes":{"volume_level":0.05000000074505806,"is_volume_muted":false,"media_content_id":"bbc_radio_two","media_content_type":"music","media_duration":-1,"media_position":21596.556427001953,"media_position_updated_at":"2022-01-14T17:38:32.610925+00:00","media_title":"BBC Radio 2","app_id":"D350F6A9","app_name":"BBC Sounds","entity_picture_local":"/api/media_player_proxy/media_player.kitchen_speaker?token=a9c41b62c87763cd0b815131e7b327a173a12fe397d3f5f84067eee1ab6e8db9&cache=4a1364155d3fc164","entity_picture":"https://sounds.files.bbci.co.uk/2.3.0/networks/bbc_radio_two/colour_450.png","friendly_name":"Kitchen Speaker","supported_features":152461},"last_changed":"2022-01-14T17:38:24.066553+00:00","last_updated":"2022-01-14T17:38:37.511527+00:00","context":{"id":"fefda2c0ce18fc80a8c38c6d1cdefa1d","parent_id":null,"user_id":"e5afebc91f304bf1bae4e9d46431bce8"},"original_state":"playing"},"new_state":{"entity_id":"media_player.kitchen_speaker","state":"off","attributes":{"friendly_name":"Kitchen Speaker","supported_features":152461},"last_changed":"2022-01-14T17:38:37.840284+00:00","last_updated":"2022-01-14T17:38:37.840284+00:00","context":{"id":"7ceec6bd6bcdbb354b14d5afce160eab","parent_id":null,"user_id":"e5afebc91f304bf1bae4e9d46431bce8"},"original_state":"off","timeSinceChangedMs":22}}

{"entity_id":"media_player.kitchen_speaker","old_state":{"entity_id":"media_player.kitchen_speaker","state":"off","attributes":{"friendly_name":"Kitchen Speaker","supported_features":152461},"last_changed":"2022-01-14T17:38:37.840284+00:00","last_updated":"2022-01-14T17:38:37.840284+00:00","context":{"id":"7ceec6bd6bcdbb354b14d5afce160eab","parent_id":null,"user_id":"e5afebc91f304bf1bae4e9d46431bce8"},"original_state":"off"},"new_state":{"entity_id":"media_player.kitchen_speaker","state":"idle","attributes":{"volume_level":0.05000000074505806,"is_volume_muted":false,"media_position_updated_at":"2022-01-14T17:38:37.838509+00:00","app_id":"CC1AD845","app_name":"Default Media Receiver","entity_picture_local":null,"friendly_name":"Kitchen Speaker","supported_features":152461},"last_changed":"2022-01-14T17:38:39.771058+00:00","last_updated":"2022-01-14T17:38:39.771058+00:00","context":{"id":"7ceec6bd6bcdbb354b14d5afce160eab","parent_id":null,"user_id":"e5afebc91f304bf1bae4e9d46431bce8"},"original_state":"idle","timeSinceChangedMs":18}}

{"entity_id":"media_player.kitchen_speaker","old_state":{"entity_id":"media_player.kitchen_speaker","state":"idle","attributes":{"volume_level":0.05000000074505806,"is_volume_muted":false,"media_content_id":"http://192.168.1.65:8123/local/Doorbell_Let_Em_In.mp3","media_position":0,"media_position_updated_at":"2022-01-14T17:38:40.007372+00:00","app_id":"CC1AD845","app_name":"Default Media Receiver","entity_picture_local":null,"friendly_name":"Kitchen Speaker","supported_features":152463},"last_changed":"2022-01-14T17:38:39.771058+00:00","last_updated":"2022-01-14T17:38:40.009309+00:00","context":{"id":"7ceec6bd6bcdbb354b14d5afce160eab","parent_id":null,"user_id":"e5afebc91f304bf1bae4e9d46431bce8"},"original_state":"idle"},"new_state":{"entity_id":"media_player.kitchen_speaker","state":"playing","attributes":{"volume_level":0.05000000074505806,"is_volume_muted":false,"media_content_id":"http://192.168.1.65:8123/local/Doorbell_Let_Em_In.mp3","media_duration":23.666939,"media_position":0,"media_position_updated_at":"2022-01-14T17:38:40.850802+00:00","app_id":"CC1AD845","app_name":"Default Media Receiver","entity_picture_local":null,"friendly_name":"Kitchen Speaker","supported_features":152463},"last_changed":"2022-01-14T17:38:40.852796+00:00","last_updated":"2022-01-14T17:38:40.852796+00:00","context":{"id":"7ceec6bd6bcdbb354b14d5afce160eab","parent_id":null,"user_id":"e5afebc91f304bf1bae4e9d46431bce8"},"original_state":"playing","timeSinceChangedMs":21}}

{"entity_id":"media_player.kitchen_speaker","old_state":{"entity_id":"media_player.kitchen_speaker","state":"playing","attributes":{"volume_level":0.05000000074505806,"is_volume_muted":false,"media_content_id":"http://192.168.1.65:8123/local/Doorbell_Let_Em_In.mp3","media_duration":23.666939,"media_position":1.235882,"media_position_updated_at":"2022-01-14T17:38:41.898623+00:00","app_id":"CC1AD845","app_name":"Default Media Receiver","entity_picture_local":null,"friendly_name":"Kitchen Speaker","supported_features":152463},"last_changed":"2022-01-14T17:38:40.852796+00:00","last_updated":"2022-01-14T17:38:41.900301+00:00","context":{"id":"7ceec6bd6bcdbb354b14d5afce160eab","parent_id":null,"user_id":"e5afebc91f304bf1bae4e9d46431bce8"},"original_state":"playing"},"new_state":{"entity_id":"media_player.kitchen_speaker","state":"idle","attributes":{"volume_level":0.05000000074505806,"is_volume_muted":false,"media_content_id":"http://192.168.1.65:8123/local/Doorbell_Let_Em_In.mp3","media_duration":23.666939,"media_position":0,"media_position_updated_at":"2022-01-14T17:39:04.638656+00:00","app_id":"CC1AD845","app_name":"Default Media Receiver","entity_picture_local":null,"friendly_name":"Kitchen Speaker","supported_features":152463},"last_changed":"2022-01-14T17:39:04.640464+00:00","last_updated":"2022-01-14T17:39:04.640464+00:00","context":{"id":"4ea91a796e2b12000031a76863346346","parent_id":null,"user_id":null},"original_state":"idle","timeSinceChangedMs":23}}

Nice!
If I have time tonight then I will have a look and import the flow you have and see what I can do.
But there doesn’t seem to be any URL in the message. It only says bbc_radio_two.

I have some kids to take care of then after that I will possibly have a look.

1 Like

Thanks a lot. This page mentions casting with other apps which is maybe why there is no URL. One thing I notice is that the final debug message still references the doorbell mp3. Its like its using that data rather than the data in the R2 node.

The reason you get doorbell as the last one is (I’m assuming now) is because the state didn’t change only the attributes.
But I’m not completely sure about this, I will have to look at it better when I sit by a computer. I have only been looking at this from the phone so far.

But did R2 start after the doorbell?

What is interesting to me is that the state changed from playing to off then idle then playing doorbell.
I wouldn’t have guessed that.

That means we need to create a sequence that can detect these changes and only of this specific pattern occurs should it cast what was playing three state changes earlier.
This is going to be fun.

Untested but I hope this will work. I don’t have any doorbell to test on. I could probably have done something, but I think this could work.
I have removed some of the data in the R2 node with the hope that it can now cast any BBC radio channel by removing the title and the channel specific data.
perhaps this can be modified even more to take any radio channel, but lets first try with BBC.

[{"id":"53351312b3c502e3","type":"api-current-state","z":"ebaa69a9.649708","name":"Speaker","server":"a8e8332.92727d","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"media_player.kitchen_speaker","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"speaker_data","propertyType":"flow","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":980,"y":1500,"wires":[["61f59476f8a61989"]]},{"id":"f9dd0461127ea15d","type":"function","z":"ebaa69a9.649708","name":"","func":"if(msg.data.new_state.state == \"playing\"  && msg.data.new_state.attributes.media_content_id == \"http://192.168.1.65:8123/local/Doorbell_Let_Em_In.mp3\"){             \n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":1500,"wires":[["53351312b3c502e3"]]},{"id":"61f59476f8a61989","type":"api-call-service","z":"ebaa69a9.649708","name":"Set Vol","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.kitchen_speaker","data":"{\"volume_level\": 0.05}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1140,"y":1500,"wires":[[]]},{"id":"1b1f32fa963099e9","type":"switch","z":"ebaa69a9.649708","name":"","property":"data.old_state.state","propertyType":"msg","rules":[{"t":"eq","v":"playing","vt":"str"},{"t":"eq","v":"off","vt":"str"},{"t":"eq","v":"idle","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":630,"y":1480,"wires":[["9aec36f10bb29915"],[],["9445fd5741424897","f9dd0461127ea15d"]]},{"id":"f38fa4702a356b10","type":"server-state-changed","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"media_player.kitchen_speaker","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":350,"y":1480,"wires":[["1b1f32fa963099e9"]]},{"id":"9aec36f10bb29915","type":"function","z":"ebaa69a9.649708","name":"","func":"if(msg.data.old_state.attributes.media_content_type == \"music\"  && msg.data.new_state.state == \"off\"){             \n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":1420,"wires":[["bedb53dcfd642e37"]]},{"id":"9445fd5741424897","type":"function","z":"ebaa69a9.649708","name":"","func":"if(msg.data.old_state.state == \"playing\"  && msg.data.new_state.attributes.media_content_id == \"http://192.168.1.65:8123/local/Doorbell_Let_Em_In.mp3\"){             \n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":1580,"wires":[["d5921e72fa3f9619","c04e962e188f0e93"]]},{"id":"bedb53dcfd642e37","type":"change","z":"ebaa69a9.649708","name":"","rules":[{"t":"set","p":"was_playing","pt":"flow","to":"data.old_state.attributes.media_content_id","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1020,"y":1420,"wires":[["7b037d018abadd1d"]]},{"id":"d5921e72fa3f9619","type":"change","z":"ebaa69a9.649708","name":"","rules":[{"t":"set","p":"doorbell","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1000,"y":1580,"wires":[["a8c93918e9f7c6b3"]]},{"id":"c04e962e188f0e93","type":"delay","z":"ebaa69a9.649708","name":"","pauseType":"delay","timeout":"4","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":980,"y":1640,"wires":[["ac5b668426537bf3"]]},{"id":"7b037d018abadd1d","type":"delay","z":"ebaa69a9.649708","name":"","pauseType":"delay","timeout":"4","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1220,"y":1420,"wires":[["3c2fe7bea79d28bd"]]},{"id":"a8c93918e9f7c6b3","type":"delay","z":"ebaa69a9.649708","name":"","pauseType":"delay","timeout":"7","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1180,"y":1580,"wires":[["0834da8e6a534f37"]]},{"id":"ac5b668426537bf3","type":"ha-wait-until","z":"ebaa69a9.649708","name":"","server":"a8e8332.92727d","version":0,"outputs":2,"entityId":"media_player.kitchen_speaker","entityIdFilterType":"exact","property":"state","comparator":"is","value":"idle","valueType":"str","timeout":"26","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"data","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":1140,"y":1640,"wires":[["9e8b77dcb479e7a6","7ae438aa3da71464"],["9e8b77dcb479e7a6","7ae438aa3da71464"]]},{"id":"3c2fe7bea79d28bd","type":"switch","z":"ebaa69a9.649708","name":"","property":"doorbell","propertyType":"flow","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":1370,"y":1420,"wires":[["e8239def2cbe07f0"]]},{"id":"0834da8e6a534f37","type":"change","z":"ebaa69a9.649708","name":"","rules":[{"t":"set","p":"doorbell","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1360,"y":1580,"wires":[[]]},{"id":"9e8b77dcb479e7a6","type":"api-call-service","z":"ebaa69a9.649708","name":"R","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"play_media","entityId":"media_player.kitchen_speaker","data":"{\t    \"media_content_type\": \"cast\",\t    \"media_content_id\": \" { \\\"app_name\\\": \\\"bbcsounds\\\", \\\"media_id\\\": \\\"\" & flow.was_playing & \"\\\", \\\"is_live\\\": true }\",\t    \"extra\": {\t      \"metadata\": {\t        \"metadataType\": 0\t      }\t    }\t  }","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1330,"y":1640,"wires":[["9418d80ec2e8f7f2"]]},{"id":"7ae438aa3da71464","type":"function","z":"ebaa69a9.649708","name":"","func":"msg.payload =\n{\n    \"data\":{\n      \"entity_id\":\"media_player.kitchen_speaker\",\n      \"volume_level\": flow.speaker_data.attributes.volume_level \n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1340,"y":1700,"wires":[["3390c629207f6621"]]},{"id":"e8239def2cbe07f0","type":"change","z":"ebaa69a9.649708","name":"","rules":[{"t":"set","p":"was_playing","pt":"flow","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1560,"y":1420,"wires":[[]]},{"id":"9418d80ec2e8f7f2","type":"delay","z":"ebaa69a9.649708","name":"12s","pauseType":"delay","timeout":"12","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1450,"y":1640,"wires":[["cfc8cc5f55d4b17a"]]},{"id":"3390c629207f6621","type":"api-call-service","z":"ebaa69a9.649708","name":"Reset Vol","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"","data":"{} ","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1500,"y":1700,"wires":[[]]},{"id":"71eb0b1cddb65782","type":"link in","z":"ebaa69a9.649708","name":"","links":["a0066175c03b3819"],"x":1395,"y":1500,"wires":[["e8239def2cbe07f0"]]},{"id":"cfc8cc5f55d4b17a","type":"api-current-state","z":"ebaa69a9.649708","name":"Speaker","server":"a8e8332.92727d","version":3,"outputs":2,"halt_if":"playing","halt_if_type":"str","halt_if_compare":"is_not","entity_id":"media_player.kitchen_speaker","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1580,"y":1640,"wires":[["ad5754f4d2457e3e"],["a0066175c03b3819"]]},{"id":"ad5754f4d2457e3e","type":"api-call-service","z":"ebaa69a9.649708","name":"R","server":"a8e8332.92727d","version":3,"debugenabled":false,"service_domain":"media_player","service":"play_media","entityId":"media_player.kitchen_speaker","data":"{\t    \"media_content_type\": \"cast\",\t    \"media_content_id\": \" { \\\"app_name\\\": \\\"bbcsounds\\\", \\\"media_id\\\": \\\"\" & flow.was_playing & \"\\\", \\\"is_live\\\": true }\",\t    \"extra\": {\t      \"metadata\": {\t        \"metadataType\": 0\t      }\t    }\t  }","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1750,"y":1640,"wires":[["a0066175c03b3819"]]},{"id":"a0066175c03b3819","type":"link out","z":"ebaa69a9.649708","name":"","mode":"link","links":["71eb0b1cddb65782"],"x":1815,"y":1700,"wires":[]},{"id":"a8e8332.92727d","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

This sequence does not “ding the bell”. I’m not sure how that is done.
But that does not have to be included here, that can be in a different sequence.
This sequence will only trigger on new media played in the kitchen.

Thanks for this. Not sure if I explained earlier but the doorbell is just a push button switch. The Test button in my original flow currently mimics this. I’m not sure how I incorporate a switch into your flow. Do I use your flow alongside or instead of my flow? I probably need to study it a bit longer as I’m a bit of a novice on all this.

I believe you can and should have a separate sequence for the doorbell.
Something like this is probably what you need:

So the doorbell is outside. And that is not an issue since the other sequence is triggered by the media-player state change.

The actual idea is to look at the state changes and if it was playing something and goes to off then save the thing that was played (upper sequence). But if the doorbell has not dinged within 4 seconds then remove the variable was_played again since that probably means you just turned off the radio.

If the media_player is now playing the doorbell sound then set the doorbell variable to true (the same variable that was used in the above sequence) for seven seconds. This is enough time for the sequence above to not delete the was_played variable.

The lower sequence is a branch of the if doorbell is playing. Wait until the doorbell sound is done or 30 seconds then cast the radio and reset volume.

Thanks again. I have tested it but it doesn’t seem to trigger the bottom branches of the flow. Not sure why as yet. The vol stays at 5% and the radio doesn’t resume.

The issue seems to be the casting of the radio.
I have tried with many attempts to get a flow variable in the cast node but I can’t do it.
I don’t understand why it doesn’t work.

Looking at how your node is set up with escaped " seems strange, but it works.
If it’s converted to a object then it says it needs string, but string doesn’t either work.

I will have to try some more later.
But I believe that is the missing piece. Making it cast the variable flow.was_played.