Help with looping through devices and running an action

I’m trying to stop all of my media players. I can loop through them but I don’t know how to run an action inside the template. I’m guessing I probably can’t but I thought I would ask.

Here is what I have:

{% for i in states.media_player %}
  {{ i.name | lower }} is {{ i.state }}  
{% endfor %}

Now I want to run media_stop on all the media players but I don’t see how to do this.

You can call the media_player.media_stop without specifying an entity_id and it’ll stop all of them.

1 Like

Ok… Tried this…

  test_script:
    sequence:
      - service: media_player.media_pause

That didn’t work.

I did get some errors…

17-04-07 13:40:23 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 233, in _step
    result = coro.throw(exc)
  File "/opt/python-venvs/hass-prod/lib/python3.4/site-packages/homeassistant/core.py", line 1010, in _event_to_service_call
    yield from service_handler.func(service_call)
  File "/opt/python-venvs/hass-prod/lib/python3.4/site-packages/homeassistant/components/media_player/__init__.py", line 365, in async_service_handler
    yield from getattr(player, method['method'])(**params)
  File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/opt/python-venvs/hass-prod/lib/python3.4/site-packages/homeassistant/components/media_player/sonos.py", line 868, in media_pause
    self._player.pause()
  File "/opt/hass-prod-cfg/deps/soco/core.py", line 104, in inner_function
    return function(self, *args, **kwargs)
  File "/opt/hass-prod-cfg/deps/soco/core.py", line 501, in pause
    ('Speed', 1)
  File "/opt/hass-prod-cfg/deps/soco/services.py", line 181, in _dispatcher
    return self.send_command(action, *args, **kwargs)
  File "/opt/hass-prod-cfg/deps/soco/services.py", line 408, in send_command
    self.handle_upnp_error(response.text)
  File "/opt/hass-prod-cfg/deps/soco/services.py", line 469, in handle_upnp_error
    error_xml=xml_error
soco.exceptions.SoCoUPnPException: UPnP Error 701 received: Transition not available from 10.0.1.118

I also tried but it didn’t stop and appears to be the same errors.

  test_script:
    sequence:
      #- service: media_player.media_pause
      - service: media_player.media_play_pause