Can't get script reliably working as part of automation

I’ve got a script that I’m using as a wakeup routine; parts of it work fine and parts work intermittently and I can’t figure out for the life of me what’s going on. I can manually do the service calls in order through the developer UI and it works. But if I manually “Trigger Script” through the UI nothing happens. It’s supposed to start playing sonos, wait a bit, turn on the light, wait a bit, turn up the sonos volume. The light always works but sonos is hit or miss. What’s happening?

Script:

'1574606550086':
  alias: Wakeup playlist
  sequence:
  - data:
      entity_id: media_player.bedroom
    service: sonos.unjoin
  - data:
      entity_id: media_player.bedroom
      volume_level: '.05'
    service: media_player.volume_set
  - data:
      entity_id: media_player.bedroom
      shuffle: 'true'
    service: media_player.shuffle_set
  - data:
      entity_id: media_player.bedroom
      source: Tastebreakers
    service: media_player.select_source
  - delay: 00:05:00
  - data:
      entity_id: switch.wemo_bedroom
    service: switch.turn_on
  - delay: 00:30:00
  - data:
      entity_id: media_player.family_room
      master: media_player.bedroom
    service: sonos.join
  - data:
      entity_id:
      - media_player.bedroom
      - media_player.family_room
      volume_level: '.2'
    service: media_player.volume_set

automation:

- id: '1574654401075'
  alias: Wake Up
  description: ''
  trigger:
  - at: 07:00
    platform: time
  condition:
  - condition: state
    entity_id: binary_sensor.workday
    state: 'on'
  action:
  - service: script.1574606550086

Are you triggering the script or the automation?

Because in the states page the button to start the script running is “execute” not “trigger”. The button to start and automation is called “trigger”.

If you are “triggering” the automation and the script isn’t running the are you sure you are using the correct script entity_id?

Ah sorry, I was referring to the play button in the Scripts section of the UI which on mouse over is labeled “Trigger Script”

The light turning on is the only part of the whole thing that works reliably. Came on at 7:05 am but still no sonos activity. The current state of the script (script.1574606550086) is as follows:

last_triggered: '2019-12-13T13:35:05.003386+00:00'
can_cancel: true
friendly_name: Wakeup playlist

That’s different than “nothing happens” but at least you know that part of it works.

That tells me that other parts also work but just not all the time?

Are you getting any errors in the home-assistant.log file in your config directory?

Ya the light always works, sorry if that was confusing. There was one day last week, I think I might have been tinkering with the automation, when the music worked. I haven’t made any changes since and I’ve definitely restarted HA multiple times after. So confusing.

What else can I try to troubleshoot? Nothing in the logs that gives any information.

2019-12-13 06:39:33 WARNING (MainThread) [homeassistant.components.ecobee] Ecobee update failed; attempting to refresh expired tokens
2019-12-13 06:48:44 ERROR (MainThread) [homeassistant.core] Error doing job: Future exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/homekit/type_thermostats.py", line 299, in update_state
    HC_HASS_TO_HOMEKIT_ACTION[hvac_action]
KeyError: 'fan'
2019-12-13 07:40:28 WARNING (MainThread) [homeassistant.components.ecobee] Ecobee update failed; attempting to refresh expired tokens

I don’t have Sonos stuff but is it possible that the script is running thru the unjoin/volume/shuffle/play steps too fast for every thing to correctly register with the Sonos?

Maybe add a few seconds delay in between those steps and see if it works then.

1 Like

firstly change you ‘.05’ and ‘.2’ values etc. to ‘0.05’ and ‘0.2’ etc

I can see that you have use the AE and SE to put these together so the following may make no difference at all, we are just eleininating possiblities here : -

put quotes around your delays eg 00:30:00 to ‘00:30:00’
And just to be sure each command gets through properly put a delay: ‘00:00:05’ between each step currently without a delay.
The code looks okay to me but I admit the AE does make it confusing (at least to me)
See if you get anything
Can you set completely different values, run the script and see which values have changed and therfore where its falling over ?

Oh I think I know what you mean and that’s a great idea. Set the sonos to volume level .65 and then run the script to see if it go far enough to change the volume?

Made script progress adding in 5 seconds of delay between each sonos service call. Also put quotes around delay times and volume values. Was too impatient to try it one at a time so I can’t say for sure what it was that helped. Testing the automation next.

Sorry, please be clear. Is the script now working ?
If so that’s good, concentrate on the automation, when that works you can pair down some of the delays till it breaks then just go back a notch.
Good Luck

It is indeed working as intended. Delays between sonos service calls are 5 seconds which I don’t even notice so I’m going to leave as is.