Ideas on how to make a media player on/off switch?

Anyone have any good suggestions on how to make a switch to turn_on and turn_off a media_player? I looked at a RESTful Switch, but not sure if it will do what I want. I would like a way to view if it is on then toggle the state to do the opposite.

I can think of a way to use scripts, input_boolean, and templates all in conjunction but I feel like there must be a cleaner way! I have several media_player resources and don’t want a huge collection of code to make it all work. There is a simple on/off button in HASS and there is a service call that can turn_on/turn_off, so I was hoping that this would be easy! Despite me thinking it should be easy I’m not seeing how!

My end goal is to have a single button on my Hadashboard (https://github.com/home-assistant/hadashboard) that will show the state and toggle it. I might end up going the route of making the widget over there if I can’t get it on the HASS side.

What is your media player?

A command line switch will likely do it.

Asking the right questions… LOL

1 Like

Thanks @silvrr

I looked at the command line switch and I don’t see a clean way of determining current status. I want to be able to quickly identify if it is on then be able to switch it off if needed… vice versa.

I don’t think that type will matter since I’m intending on going through HASS to make this work. I do have several different media players, but they all use the same on/off/status commands in HASS.

I am using a sony bravia tv and created this switch which works like a charm

- platform: template
switches:
  teevee:
    value_template: "{{ is_state('media_player.sony_bravia_tv', 'on') }}"
    turn_on:
      service: media_player.turn_on
      data:
        entity_id: media_player.sony_bravia_tv
    turn_off:
      service: media_player.turn_off
      data:
        entity_id: media_player.sony_bravia_tv
    icon_template: >-
      {% if is_state('media_player.sony_bravia_tv', 'on') %}
        mdi:television-classic
      {% else %}
        mdi:television-classic-off
      {% endif %}

2 Likes

This should create an entity that I add myself into the gui, right? Because I tried this (Modified for my case) and it doesn’t, even when there are no error messages.