I’m trying to save the notification volume level of my phone in order to change it and setting it back to what is was later.
I’m running this code which successfully retrieve the value and stores it correctly in the input_number entity, which is great, but it happens that no matter what I do, I have to run the script exactly twice before it works.
The first part of the script is there to make sure I have the right value, in case it was changed in the last 15 minutes.
Can you tell me what I have to do in order to make it work without having to run it twice?
I’m guessing the script goes to set_value before the notify.mobile_app_myphone actually populates the data. Its an asynchronous call I believe. You can add a delay but even then it may not actually update. What I’d probably do is put that command_update_sensors in an actual script and call that script with the -script.turn_on so it returns immediately and doesn’t wait… Then id probably use a wait in the script for when the value updates to set the volume.
That was my first hypothesis, but apparently no, according to my tests. To be sure, I just tried your suggestion, so I put it in 2 scripts as below. Notice that I did not use the script.turn_on service as it doesn’t wait for the called script to finish before continuing, so I used the script.NAME service instead.
Also even when I trigger the command_update_sensors separately and manually, then wait, then do the same thing for the input_number.set_value part, I still get the same result.
So I excluded the asynchronous call/delay to update as a cause of the problem. No matter what I do I have to repeat the whole thing twice.
And this pattern actually repeats. So it’s precisely exactly every second time that the input_number is updated, I tried it by triggering the script multiple times, and this in both short delays (a few seconds between each) and with a 1 minute at least delay.
Now I would love to replace the “delay”’ by a proper wait command. Despite my research in the docs I don’t know what syntax is appropriate to write that condition. This condition would be true if sensor.myphone_volume_level_notification has an updated value, regardless whether it’s the same value or a new one. Could you please give me a pointer on this one?
So these volume sensors are poll based sensors so they update once every 15 minutes, if that is not frequent enough then they also update any time any other sensor updates so you can try enabling more sensors. This page does a good job explaining how sensor updates work, hard to tell how long of a delay to add because for the most part sensor updates are very fast (within a second).
I would say if you enable the interactive sensor that will do a good job of capturing the volume state next time the screen turns off since you need the screen on in order to adjust the volume. Unless of course you used a notification command to change the volume level.