Home Assistant Remote Control For My Samsung TV and Sonos

Last modified date

Comments: 29

Home Assistant Remote Control for Samsung TV

I have a Samsung Smart TV (UA49NU7100) and a Sonos Soundbar/Subwoofer in my lounge room, all of which work really well. For many years we have also used a Logitech Harmony Hub remote control – which was great, as it could give quite specific control, and worked more reliably than the standard Samsung remote. However, after many years of service the Harmony remote finally died. The Samsung remote also seems to be partially blocked by the soundbar, and we have to hold the remote up high to get it to work. Which is not ideal.

So – I built a remote control from within Home Assistant to control the TV and soundbar.

After quite a bit of frustration, it all seems to work well.

Reliably powers on and off, toggles mute, icon base shortcuts and links to Google to enable voice control.

Samsung Smart TV

There is a standard Samsung TV integration available within Home Assistant. It is self discoverable and simple to configure, however, I could not get it to reliably do all the things I required for my remote. So, I disabled it, and installed the custom component Home Assistant SamsungTV Smart Component. This is a custom component to allow control of SamsungTV devices in Home Assistant. Is a modified version of the built-in samsungtv with some extra features.

The installation is straight forward – just follow the instructions in the link above.

The installation creates a media_player entity. In my setup it created – media_player.lounge_tv_2. It added a 2 as I had already created a device using the original Samsung component. I could not be bothered to change the naming.

The remote has a number of rows/groups that enables control of the TV.

Main Control

The first panel provides the ability to toggle the power on the TV, go to the home menu and exit. The power toggle finished up being quite simple. This is one area that did not work with the original component. There is quite a lot of discussion about this on the internet, as it is not always easy to turn on a TV from a network device when the TV is off. You need to enable the TV to be controlled from a network device from within the TV network setup.

Home Assistant Remote Control - 01
type: custom:button-card
entity_id: media_player.lounge_tv_2
tap_action:
  action: call-service
  service: media_player.toggle
  service_data:
    entity_id: media_player.lounge_tv_2
show_name: false
icon: mdi:power
size: 25%

The power toggle (on/off) is quite simple, but if it does not work there a number of options to try. Refer to the discussion on the instruction site.

type: custom:button-card
tap_action:
  action: call-service
  service: media_player.play_media
  service_data:
    entity_id: media_player.lounge_tv_2
    media_content_type: send_key
    media_content_id: KEY_HOME
show_name: false
icon: mdi:home
size: 25%

The home and exit buttons use the ’send_key’ content type, using the KEY_HOME and KEY_EXIT content id’s.

Currently Playing

We use a number of streaming services apps on the TV, and it is not always clear what app is running when content is streaming to the TV. I added a banner that presents what app is running at any time. The banner also uses a font color and background color that aligns with the colors of the streaming service.

Home Assistant Remote Control - 02
type: custom:button-card
name: |
  [[[
    if(states['media_player.lounge_tv_2'].state == 'on') {
      var src = states['media_player.lounge_tv_2'].attributes.source;
      if(src == 'TV/HDMI') {
        src = 'Broadcast TV';
      }
      return 'Currently playing - '+ src;
    } else {
      return 'TV is currently turned off';
    }
  ]]]
styles:
  card:
    - background-color: |
        [[[
          var src = states['media_player.lounge_tv_2'].attributes.source;
          if(src == 'YouTube') { return '#CD201F'; }
          if(src == 'Netflix') { return '#000000'; }
          if(src == 'BINGE') { return '#70139F'; }
          if(src == 'Prime Video') { return '#223040'; }
          if(src == 'Kayo Sports') { return '#06E277'; }
          if(src == 'Plex') { return '#202020'; }
          if(src == 'ABC iview') { return '#40B8B6'; }
          if(src == 'SBS On Demand') { return '#FDB717'; }
          if(src == '7plus') { return '#EB1B23'; }
          if(src == '9Now') { return '#FFFFFF'; }
          if(src == '10 play') { return '#0049F4'; }
          return 'var(--table-row-background-color)';
        ]]]
    - color: |
        [[[
          var src = states['media_player.lounge_tv_2'].attributes.source;
          if(src == 'YouTube') { return '#FFFFFF'; }
          if(src == 'Netflix') { return '#E50915'; }
          if(src == 'BINGE') { return '#FFFFFF'; }
          if(src == 'Prime Video') { return '#02A5DE'; }
          if(src == 'Kayo Sports') { return '#002314'; }
          if(src == 'Plex') { return '#FFFFFF'; }
          if(src == 'ABC iview') { return '#FFFFFF'; }
          if(src == 'SBS On Demand') { return '#2E2D2C'; }
          if(src == '7plus') { return '#FFFFFF'; }
          if(src == '9Now') { return '#0153A2'; }
          if(src == '10 play') { return '#FFFFFF'; }
          return 'var(--primary-text-color)';
        ]]]

The code cleans up the Broadcast TV banner, and notifies us if the TV is off.

Icon Shortcuts

This panel provides quick access to streaming apps and some main broadcast channels. It also provides the channel up and down buttons when you are in broadcast TV. These are the services and channels that we use, and you would need to change to meet your requirements.

Home Assistant Remote Control - 04
type: custom:button-card
color: rgb(108, 108, 108)
entity: media_player.lounge_tv_2
tap_action:
  action: call-service
  haptic: medium
  service: media_player.select_source
  service_data:
    entity_id: media_player.lounge_tv_2
    source: YouTube
entity_picture: /local/icons/channels/youtube.png
show_name: false
show_icon: false
show_entity_picture: true
style:
  - padding: 0px
styles:
  entity_picture:
    - width: 100%
    - padding-bottom: 0%

I have created a custom button for each service. The button uses an entity_picture to present the service logo. I placed all the logos in the www/icons/channels directory. Also you need to get the spelling and formatting of the source correct. You can check the available sources from the source list in the media_player state.

type: custom:button-card
entity: media_player.lounge_tv_2
tap_action:
  action: call-service
  haptic: medium
  service: media_player.play_media
  service_data:
    entity_id: media_player.lounge_tv_2
    media_content_type: send_key
    media_content_id: KEY_EXIT+3000+KEY_TV+KEY_2+KEY_0
entity_picture: /local/icons/channels/tv_abc.png
show_name: false
show_icon: false
show_entity_picture: true
style:
  - padding: 0px
styles:
  entity_picture:
    - width: 100%
    - padding-bottom: 0%

The third row contains shortcuts to broadcast channels. In this you need to convert to broadcast TV and then enter the channel numbers using the send_key content type. I debated if I should create a broadcast TV button and then set channel number on another button. I decided to set in one button – the only downside is that the TV does not always change to broadcast TV fast enough to accept the channel numbers. I have added a 3 sec delay, but it still does not always work and it misses the first channel number. If this happens I just push the button again and it always works, as it is now in broadcast TV mode. I could have added a longer delay, but then the whole process becomes too slow.

type: custom:button-card
entity: media_player.lounge_tv_2
color: var(--primary-text-color)
tap_action:
  action: call-service
  haptic: medium
  service: media_player.play_media
  service_data:
    entity_id: media_player.lounge_tv_2
    media_content_type: send_key
    media_content_id: KEY_CHUP
show_name: false
icon: mdi:arrow-up-circle-outline
size: 82%

I have added the channel up and down buttons. Quite simple – just uses a send_key message for KEY_CHUP and KEY_CHDOWN

Navigation And Select

The navigation and select buttons are straight forward. They are a send_key type of function and uses KEY_ENTER, KEY_LEFT, KEY_UP, KEY_RIGHT and KEY_DOWN.

Home Assistant Remote Control - 05
type: custom:button-card
entity: media_player.lounge_tv_2
color: var(--primary-text-color)
tap_action:
  action: call-service
  haptic: medium
  service: media_player.play_media
  service_data:
    entity_id: media_player.lounge_tv_2
    media_content_type: send_key
    media_content_id: KEY_ENTER
show_name: false
icon: mdi:check-circle-outline
size: 25%

Control

Control buttons have volume control, video control plus a couple of extras. The sound from my TV is played through the Sonos Soundbar and Subwoofer. I installed the Sonos integration, which creates a number of devices (depends on how many Sonos devices you have) and a whole pile of entities. The rest is straight forward. I have created a mute toggle, which takes a little more effort (see code below).

Home Assistant Remote Control - 06
type: custom:button-card
entity: media_player.lounge
color: var(--primary-text-color)
tap_action:
  action: call-service
  haptic: medium
  service: script.sonos_toggle
show_name: false
icon: mdi:volume-off
size: 25%

To get the mute button to toggle on and off required the creation of a script to process the toggle, as you need to test the toggle state before you toggle, otherwise it will not toggle on again. The above button calls the script.sonos_toggle script.

service: media_player.volume_mute
target:
  entity_id: media_player.lounge
data:
  is_volume_muted: '{{ not state_attr(''media_player.lounge'', ''is_volume_muted'') }}'

The script needs to process the is_volume_muted: attribute to operate the mute properly. This now works perfectly.

type: custom:button-card
entity: media_player.lounge
color: var(--primary-text-color)
tap_action:
  action: call-service
  haptic: medium
  service: media_player.volume_down
  service_data:
    entity_id: media_player.lounge
show_name: false
icon: mdi:volume-minus
size: 25%

The volume control is straight forward, but you must make sure the Sonos media play and not the TV media player have been used.

Channel Numbers

The channel number buttons use the send_key content type and sends KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9 and KEY_0. These buttons only have an effect when you in Broadcast TV mode. Pushing them any other time does nothing.

Home Assistant Remote Control - 07
type: custom:button-card
entity: media_player.lounge_tv_2
tap_action:
  action: call-service
  haptic: medium
  service: media_player.play_media
  service_data:
    entity_id: media_player.lounge_tv_2
    media_content_type: send_key
    media_content_id: KEY_1
name: 1
show_icon: false
styles:
  name:
    - padding-top": 10px

Voice Control

I have added some voice controls using Google Assistant. This enables me to say “Hey Google. Turn on Youtube” or ”Hey Google. Watch Youtube”. I have added commands for most of my streaming services, plus a few control type commands.

To link Google to Home Assistant you need to subscribe to Home Assistant Cloud, provided by their partner Nabu Casa, Inc.. This provides secure and external access to your Home Assistant environment. There is quite a number of sites on the internet on how to do this. If you need any help, please let me know.

In Home Assistant I created a series of scripts to essentially perform the same tasks that are performed by the icon buttons on the remote.

This image shows the bottom half of the script UI. In this case you choose the service required, the entity and the media source.

In Settings>Home Assistant Cloud you manage the entities and scripts required to be synced with Google. Select the required scripts and sync with Google.

It took me while to find where these scripts turn up in Google. Device based entities are visible in Google Home and can be easily accessed, but not the scripts. It turns out the scripts appear as ‘scenes’ in Google Assistant (not sure why).

I created a series of routines in Google Assistant. For each routine I created two starters – ‘Watch Youtube’ and ’Turn on Youtube’ (for example). Follow this link to access the scenes:

Add action > Adjust Home Devices > Adjust Scenes > Edit Action

Select scene / script and enable action – check box.

I hope you find this post useful. If you have any questions or comments, please leave details below.

ken

Share

29 Responses

  1. Thanks for all this clear documentation!

    I am still having issues opening applications like Netflix using the examples you provided. Most other buttons are working fine except for the streaming services on my samsung tv.

    • Sorry for delay – been unavailable. Did you get this working? My Pi sd card died and I had to rebuild – and I finished with the same problem you had (not sure why it worked the first time). Discovered I had to rebuild the app list to get it working. I have made a couple changes in my rebuild and will blog post in the next couple of days.

      Regards
      Ken

  2. sorry i am a newbee
    this seems to be a great work
    what must i do to import it to mey dashboards
    thanks in advance
    udo

    • Sorry for delay – unfortunately this is not a simple install. It is a ‘built’ page made of custom-buttons and TV integration. One has to follow all the steps in the blog. I have found a HACS card called ‘TV Remote Card (with touchpad and haptic feedback)’ – https://github.com/usernein/tv-card/blob/master/README.md which is very good. It does not quite have the level of formatting I wanted, but it is simple installed card.

      Regards
      Ken

  3. Nice work man, i copied you yaml code. But the pictures doesn’t show at the custom button for youtub etc and a few buttons does not work

    • How are you going. The icons for the buttons must be placed in the www/icons/channels directory. If you wish to place in a differnt location you must change – entity_picture: /local/icons/channels/youtube.png

      I created the icons for the channels I use. You can download those icons that correspond with your channels – you will have to create the icons specific to you channel list. I use the opensource app Gimp to create the icons.

      Regards
      Ken

  4. Hey Ken,
    Great work on this remote. I’m trying to implement it now.
    One thing is the Icons you have created, they look great…. would you be able to share the png files with a fellow Melbournian!?

    thanks
    Marco

  5. Hey Ken,

    This is amazing and it turns out we watch all the same channels! I started downloading icons myself, but it is relatively time consuming and realising the pain in getting them sized the same.

    Any chance you can upload your icon pack to git alongside the yaml source?

  6. Hi,
    Thanks for all the effort and documentation, this is exactly what i needed and you saved me a huge task in design.

    For my Samsung TV (QE49Q70RATXXU) i found the power option for toggle turns off but not on, so i had to switch that out to two conditional cards, but everything else worked great!

    Ash

    • I am happy this was a help. Yes – I had trouble with the ‘On’ switch. I tried all types of approaches. The approach I used worked for me. I have no idea why this worked and many of the other options did not.

  7. Hey Ken, I love this remote and really want to integrate it into my HA. I have everything working and showing up, but I am at a loss for how to setup the app list. In my media player states, I only see sources for the HDMIs even though I have tons of apps installed. Any help you could give would be much appreciated

    • Hi, Shawn. In my second post on the remote, I mentioned that the app list was generated automatically when I first built the remote (although I’m unsure how or why). However, when my Raspberry Pi died, and I had to rebuild my HA, the list was empty. To solve this issue, I found a webpage (that I linked to in my post) with all the app IDs I needed. I listed the IDs I used in my post, but when I checked the linked page again just now, it had been rewritten, and the list was no longer available (I should have saved a copy). I did a quick google search for Samsung App IDs but had limited success. I will continue to search and let you know if I find anything useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment