Keep Chromecast alive while paused

I just wanted to share how to keep a stream alive while paused on a Chromecast.
In order to not loose the cast you have to play - pause at least every five minutes.
Here is how you make Home Assistant do that for you.

A short explanation of the sequence:
It triggers on paused state waits four minutes, first time it rewinds 20 seconds, then does a play, and pause.
Then it waits four minutes and rewinds 1 second, play - pause, and keeps this loop forever and maintaining the position {{ time_when_you_paused - 20 seconds }}.

That way when you come back you can press play and get a short refresher before continuing the view.

[{"id":"94bc1df0.962d9","type":"tab","label":"Chromecast","disabled":false,"info":""},{"id":"21318654.e2391a","type":"trigger-state","z":"94bc1df0.962d9","name":"","server":"4bbca37b.1700ec","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"media_player.chromecast_vardagsrum","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":200,"y":140,"wires":[["f477737f.5411"],["f477737f.5411"]]},{"id":"f477737f.5411","type":"api-current-state","z":"94bc1df0.962d9","name":"","server":"4bbca37b.1700ec","version":1,"outputs":2,"halt_if":"paused","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"media_player.chromecast_vardagsrum","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":660,"y":140,"wires":[["449e78d4.0a53d8"],[]]},{"id":"70c4c614.633208","type":"function","z":"94bc1df0.962d9","name":"Payload = rewind 20 seconds","func":"msg.pos = Math.max(parseFloat(msg.data.attributes.media_position-20), 0);\n\nmsg.payload = {\n    data:{\n        \"entity_id\": \"media_player.chromecast_vardagsrum\",\n        \"seek_position\": msg.pos,\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1220,"y":140,"wires":[["b07c26bf.0bd798"]]},{"id":"b07c26bf.0bd798","type":"ha-wait-until","z":"94bc1df0.962d9","name":"","server":"4bbca37b.1700ec","outputs":2,"entityId":"media_player.chromecast_vardagsrum","entityIdFilterType":"exact","property":"state","comparator":"is","value":"playing","valueType":"str","timeout":"4","timeoutType":"num","timeoutUnits":"minutes","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":1480,"y":140,"wires":[["d34eb474.5f9d88"],["a824d3f4.b8e8b"]]},{"id":"a824d3f4.b8e8b","type":"api-call-service","z":"94bc1df0.962d9","name":"rew","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"media_seek","entityId":"media_player.chromecast_vardagsrum","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1650,"y":140,"wires":[["8407d36e.95e2"]]},{"id":"73d459f6.5e5a38","type":"api-call-service","z":"94bc1df0.962d9","name":"play","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"media_play","entityId":"","data":"{\"entity_id\":\"media_player.chromecast_vardagsrum\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1390,"y":260,"wires":[["feba9f5a.f1009"]]},{"id":"9dee33b1.46c11","type":"api-call-service","z":"94bc1df0.962d9","name":"pause","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"media_pause","entityId":"media_player.chromecast_vardagsrum","data":"{\"entity_id\": \"media_player.chromecast_vardagsrum\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1690,"y":260,"wires":[["a5c1a018.5beed"]]},{"id":"449e78d4.0a53d8","type":"trigger","z":"94bc1df0.962d9","name":"","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"0","extend":false,"units":"ms","reset":"reset","bytopic":"all","topic":"topic","outputs":1,"x":1000,"y":140,"wires":[["70c4c614.633208"]]},{"id":"c13ebb34.3eba98","type":"function","z":"94bc1df0.962d9","name":"Payload = reset","func":"msg.payload = \"reset\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":900,"y":260,"wires":[["449e78d4.0a53d8"]]},{"id":"fda46a2d.fa0c38","type":"function","z":"94bc1df0.962d9","name":"Payload = rewind 1 second","func":"msg.pos = parseFloat(msg.data.attributes.media_position-1);\nmsg.payload = {\n    data:{\n        \"entity_id\": \"media_player.chromecast_vardagsrum\",\n        \"seek_position\": msg.pos,\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":2040,"y":340,"wires":[["b07c26bf.0bd798"]]},{"id":"659bbfe5.21d11","type":"api-current-state","z":"94bc1df0.962d9","name":"","server":"4bbca37b.1700ec","version":1,"outputs":2,"halt_if":"paused","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"media_player.chromecast_vardagsrum","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1680,"y":340,"wires":[["fda46a2d.fa0c38"],["f5e89eb8.9b068"]]},{"id":"53a2715e.bdefb","type":"inject","z":"94bc1df0.962d9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":300,"wires":[["c13ebb34.3eba98"]]},{"id":"8407d36e.95e2","type":"delay","z":"94bc1df0.962d9","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1780,"y":140,"wires":[["b064a90f.beac48"]]},{"id":"feba9f5a.f1009","type":"delay","z":"94bc1df0.962d9","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1540,"y":260,"wires":[["9dee33b1.46c11"]]},{"id":"a5c1a018.5beed","type":"delay","z":"94bc1df0.962d9","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1380,"y":340,"wires":[["659bbfe5.21d11"]]},{"id":"b064a90f.beac48","type":"function","z":"94bc1df0.962d9","name":"msg.payload = {};","func":"msg.payload = {};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1950,"y":140,"wires":[["73d459f6.5e5a38"]]},{"id":"f5e89eb8.9b068","type":"link out","z":"94bc1df0.962d9","name":"Release trigger node","links":["2b2f450d.8a73ba"],"x":1915,"y":400,"wires":[]},{"id":"2b2f450d.8a73ba","type":"link in","z":"94bc1df0.962d9","name":"","links":["f5e89eb8.9b068","d34eb474.5f9d88"],"x":755,"y":380,"wires":[["c13ebb34.3eba98"]]},{"id":"d34eb474.5f9d88","type":"link out","z":"94bc1df0.962d9","name":"Release trigger node","links":["2b2f450d.8a73ba"],"x":1595,"y":80,"wires":[]},{"id":"d9f621ca.74c4c","type":"inject","z":"94bc1df0.962d9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":350,"y":80,"wires":[["f477737f.5411"]]},{"id":"4bbca37b.1700ec","type":"server","z":"","name":"Home Assistant","addon":true}]

2 Likes

This looks useful, and with a few modifications - this flow will work for multiple chromecasts.

It probably will.
I have added a volume set to 0.3 when the first rewind is done, and a 1 when the trigger node is reset.
That way if you paused when a bomb went off you don’t have to hear the bomb over and over again at full volume.

I once had issues with the chromecast not responding to inputs and it just kept playing. But a restart fixed the issue.

Also make the wait until node wait “is not paused” instead because otherwise, if you are paused and press stop the wait node does not reset.

1 Like