Why I have to run the script twice in order to store the state of my phone's notification volume in input number?

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?

Here is my script:

alias: save_mobile_vol_notif
sequence:
  - service: notify.mobile_app_myphone
    data:
      message: command_update_sensors

  - service: input_number.set_value
    data:
      value: '{{ states("sensor.myphone_volume_level_notification") | float }}'
    target:
      entity_id: input_number.previous_vol_notif_myphone
mode: single

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.

alias: save_myphone_vol_notif
sequence:
  - service: script.update_mobile_sensors
  - service: input_number.set_value
    data:
      value: '{{ states("sensor.myphone_volume_level_notification") | float }}'
    target:
      entity_id: input_number.previous_vol_notif_myphone
mode: single
alias: update_mobile_sensors
sequence:
  - service: notify.mobile_app_myphone
    data:
      message: command_update_sensors
mode: single

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.

I mean it should be easy to debug?

Run this:
- service: script.update_mobile_sensors

Check the actual value of sensor.myphone_volume_level_notification and see if it changed. Has it?

Then if it has, Run this and see if it updated.

service: input_number.set_value

1 Like

That’s actually why I said to use turn_on so you can plop a wait in the script and it’ll trigger when the value updates…

Interesting.

With this code, in a single script, it works.

alias: save_myphone_vol_notif
sequence:
  - service: notify.mobile_app_myphone
    data:
      message: command_update_sensors

  - delay: "00:00:01"
  
  - service: input_number.set_value
    data:
      value: '{{ states("sensor.myphone_volume_level_notification") | float }}'
    target:
      entity_id: input_number.previous_vol_notif_myphone
mode: single

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?

And thank you for the help!

The companion app will not send a state update if the state has not changed since the last update. Sensors are only updated if the state has changed.

1 Like

Thank you for the information. Do you have any advice about how to put the most appropriate wait, in order to minimize the slowing of the process?

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.

1 Like