How to save and resume state in automation (scene / variable?)


Sorry for the long post, I wanted to include the full context and what I already tried. But let me start with a quick summary of what I want to achieve:

TL;DR: I need help on how to save the information if my Android TV projector was playing, so that I can resume playing after doing some other actions, all in one script. I tried scenes and variables, but can’t get it to work as I want.

I have setup TTS through my Yamaha receiver. It all works very well in general. I’m saving the state of the receiver by creating a scene at the start of the script, then I make some changes if necessary (to turn on the receiver, switch the source, adjust volume), play the message, then turn on the scene to reload the previous state of the device. This works fine in general.

But the same approach does not work for my projector with Android TV on it, connected to HA with Android TV and Google Cast integrations (both are needed for different kind of control/information about the projector). Each of those integrations create a separate media player entity. I tried saving either of those entities in the scene, but it does not work as I expected. It fails at resuming the previous state. Either does nothing (Android TV one) or starts casting HA to it (the Google Cast), even though it was playing Netflix before.

I read the scenes documentation, and couldn’t really find much in terms of how this actually works technically, how does HA restore the state from the scene, and what devices are supported exactly (is it supposed to work for all integrations and devices?).

Secondly, I looked into creating a variable. This would work perfectly for my specific case, as I just need the info if the projector was playing, so I can pause it and then resume playing. I looked into variables, but it seems this approach won’t work, because variables scope is very limited, documentation says:

Variables have local scope. This means that if a variable is changed in a nested sequence block, that change will not be visible in an outer sequence block.

I don’t know if I understand this correctly, but it seems the variable I would save in the if>then block at the start of the script, would be only available in that block, and not when I try to act on it in a later sequence. Am I right?

An unrelated part of documentation, about parallel actions, seems to suggest those variables may be available to other sequence actions though, so I’m confused now:

  • Variables created/modified in one parallelized action are not available in another parallelized action. Each step in a parallelized has its own scope.

Thanks in advance for any help.

Have you tried saving both in the same scene?

No, not yet. I thought it would somehow conflict, as they both refer to the same physical device. I can try it though.

I also just stumbled across this, not sure if related. Have you updated recently?

I don’t think it’s related. I did update, but the Google cast device was available as usual.

I finally got to testing it, but nope, didn’t change the result.

Have you tried the methods (services) which are exposed through the media player integration (e.g., pause media, do whatever, resume)?

I’m kind of shooting in the dark, as I use Kodi for my media playing needs, instead of devices like these. I much prefer something FLOSS like that over these proprietary alternatives, mostly because I got tired of fighting with the limits that $BIGCORP seem to always want to impose. Maybe there is some way and I just don’t know it (very possible).

That part is not the problem. I can pause and resume the player. The problem is I don’t know how to save the information on whether it was playing at the start of my script or not. I don’t want to send a command to resume, if the player was paused even before the script started running. That’s why I need to save the info if it was running or not.

Yeah, I don’t see a method to get the current state (at least not through the media player integration).

As far as your variable scope question, anything you set at the beginning/top of the script should be available throughout. It’s only when you get deeper in that becomes a problem (generally speaking).

I will try one more thing. I just reread the documentation for the Android TV Remote integration (sorry, that’s the full name, earlier I forgot the remote part) and they suggest to use Universal Media Player integration to merge the Android TV Remote and Google Cast into one media player. So I’ll try that next to see if that one can properly be saved and restored in a scene.