Trigger automation change TV channel?

Hello, I have a Sony Bravia TV that is installed in HA with bravia PsK.
I would like to trigger an automation on a change to any channel, but not on a change to an input (eg HDMI 2).
Might that be possible?

can you turn on your tv, put some channel and print screen this?

1 Like

This is a link to Sony’s information about their Bravia interface
https://pro-bravia.sony.net/develop/integrate/ip-control/

I took a quick look, and I didn’t see a status you can read that would tell you if a channel changed, but I could have missed something. If there is a status available, you’d probably need to keep reading it, and look for changes. Its what I do in Node Red to see if the TV has been turned on or off.

Can you do something with this event log?


{
    "event_type": "state_changed",
    "data": {
        "entity_id": "media_player.fernseher",
        "old_state": {
            "entity_id": "media_player.fernseher",
            "state": "on",
            "attributes": {
                "source_list": [
                    "Das Erste HD",
                    "ZDF HD",
                    "WDR HD Köln",
                    "SAT.1 HD",
                    "RTL HD",
                    "RTLZWEI HD",
                    "ProSieben HD",
                    "Kabel Eins HD",
                    "VOX HD",
                    "sixx HD",
                    "SUPER RTL HD",
                    "n-tv HD",
                    "Welt HD",
                    "phoenix HD",
                    "ServusTV HD",
                    "ProSieben MAXX HD",
                    "SAT.1 Gold HD",
                    "arte HD",
                    "3sat HD",
                    "ZDFinfo HD",
                    "zdf_neo HD",
                    "KiKA HD",
                    "ONE HD",
                    "DMAX HD",
                    "ANIXE HD",
                    "MTV HD",
                    "SPORT 1 HD",
                    "Eurosport 1 HD",
                    "NITRO HD",
                    "National Geographic HD",
                    "ProSieben FUN HD",
                    "ARD-alpha HD",
                    "SONY AXN HD",
                    "NatGeo HD",
                    "Sky Cinema Special HD",
                    "History HD",
                    "Eurosport 2 HD",
                    "Nat Geo Wild HD",
                    "DAZN 2 Bar HD",
                    "Universal TV HD",
                    "Fox HD",
                    "SYFY HD",
                    "13th Street HD",
                    "Discovery HD",
                    "SPORT1+ HD",
                    "DAZN 1 Bar HD",
                    "Beate Uhse HD",
                    "RTL Living HD",
                    "MTV Live HD",
                    "TNT Comedy HD",
                    "Spiegel Geschichte HD",
                    "Kabel Eins Doku HD",
                    "Spiegel TV Wissen HD",
                    "TNT Film HD",
                    "Sky Sport Austria 1 HD",
                    "Sky One HD",
                    "TNT Serie HD",
                    "Sky Cinema Premieren HD",
                    "TELE 5 HD",
                    "NICK/MTV+ HD",
                    "Sky Sport 1 HD",
                    "TLC HD",
                    "Sky Sport Bundesliga 2 HD",
                    "Sky Sport 3 HD",
                    "Sky Sport Bundesliga 3 HD",
                    "Sky Sport 4 HD",
                    "Sky Sport Bundesliga 4 HD",
                    "Sky Sport 5 HD",
                    "Sky Sport Bundesliga 6 HD",
                    "Sky Sport 7 HD",
                    "Sky Sport Bundesliga 7 HD",
                    "Sky Sport 8 HD",
                    "Sky Sport Bundesliga 8 HD",
                    "Sky Sport 9 HD",
                    "Sky Sport Bundesliga 9 HD",
                    "Sky Sport 10 HD",
                    "Sky Serien & Shows HD",
                    "Deluxe Music HD",
                    "Comedy Central HD",
                    "Disney Channel HD",
                    "FUEL TV HD",
                    "Mezzo Live HD",
                    "Sky Cinema Premieren +24 HD",
                    "Sky Cinema Animation HD",
                    "CRIME + INVESTIGATION HD",
                    "SyFy HD",
                    "Sky Cinema Action HD",
                    "KBS World  HD",
                    "SONY CHANNEL HD",
                    "sonnenklar.TV HD",
                    "Juwelo HD",
                    "1-2-3.tv HD",
                    "CLASSICA HD",
                    "Sky Krimi HD",
                    "SWR RP HD",
                    "BR Fernsehen SĂŒd HD",
                    "NDR FS NDS HD",
                    "rbb Berlin HD",
                    "MDR Sachsen HD",
                    "hr-fernsehen HD",
                    "tagesschau24 HD",
                    "Sky Atlantic HD",
                    "Bibel TV HD",
                    "NRWision HD",
                    "Sky Sport 2 HD",
                    "FOX HD",
                    "QVC HD",
                    "Sky Sport Austria 2 HD",
                    "Sky Sport Austria 3 HD",
                    "Sky Sport 12 HD",
                    "Sky Sport 13 HD",
                    "CNBC HD",
                    "Sky Sport Bundesliga 5 HD",
                    "Sky Sport 6 HD",
                    "Sky Cinema Thriller HD",
                    "HDMI 1/MHL",
                    "HDMI 2",
                    "HDMI 3",
                    "HDMI 4/ARC"
                ],
                "volume_level": 0,
                "is_volume_muted": false,
                "media_content_id": "Das Erste HD",
                "media_content_type": "tvshow",
                "media_title": "1: Das Erste HD",
                "media_series_title": "Sportschau [19:05 - 20:00]",
                "source": "Das Erste HD",
                "friendly_name": "Fernseher",
                "supported_features": 24505,
                "device_class": "tv",
                "icon": "mdi:television"
            },
            "last_changed": "2021-01-15T17:36:25.249562+00:00",
            "last_updated": "2021-01-15T18:05:05.668171+00:00",
            "context": {
                "id": "1cfacdbaaeb576ffc4d1180a8b0ba1ac",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "media_player.fernseher",
            "state": "on",
            "attributes": {
                "source_list": [
                    "Das Erste HD",
                    "ZDF HD",
                    "WDR HD Köln",
                    "SAT.1 HD",
                    "RTL HD",
                    "RTLZWEI HD",
                    "ProSieben HD",
                    "Kabel Eins HD",
                    "VOX HD",
                    "sixx HD",
                    "SUPER RTL HD",
                    "n-tv HD",
                    "Welt HD",
                    "phoenix HD",
                    "ServusTV HD",
                    "ProSieben MAXX HD",
                    "SAT.1 Gold HD",
                    "arte HD",
                    "3sat HD",
                    "ZDFinfo HD",
                    "zdf_neo HD",
                    "KiKA HD",
                    "ONE HD",
                    "DMAX HD",
                    "ANIXE HD",
                    "MTV HD",
                    "SPORT 1 HD",
                    "Eurosport 1 HD",
                    "NITRO HD",
                    "National Geographic HD",
                    "ProSieben FUN HD",
                    "ARD-alpha HD",
                    "SONY AXN HD",
                    "NatGeo HD",
                    "Sky Cinema Special HD",
                    "History HD",
                    "Eurosport 2 HD",
                    "Nat Geo Wild HD",
                    "DAZN 2 Bar HD",
                    "Universal TV HD",
                    "Fox HD",
                    "SYFY HD",
                    "13th Street HD",
                    "Discovery HD",
                    "SPORT1+ HD",
                    "DAZN 1 Bar HD",
                    "Beate Uhse HD",
                    "RTL Living HD",
                    "MTV Live HD",
                    "TNT Comedy HD",
                    "Spiegel Geschichte HD",
                    "Kabel Eins Doku HD",
                    "Spiegel TV Wissen HD",
                    "TNT Film HD",
                    "Sky Sport Austria 1 HD",
                    "Sky One HD",
                    "TNT Serie HD",
                    "Sky Cinema Premieren HD",
                    "TELE 5 HD",
                    "NICK/MTV+ HD",
                    "Sky Sport 1 HD",
                    "TLC HD",
                    "Sky Sport Bundesliga 2 HD",
                    "Sky Sport 3 HD",
                    "Sky Sport Bundesliga 3 HD",
                    "Sky Sport 4 HD",
                    "Sky Sport Bundesliga 4 HD",
                    "Sky Sport 5 HD",
                    "Sky Sport Bundesliga 6 HD",
                    "Sky Sport 7 HD",
                    "Sky Sport Bundesliga 7 HD",
                    "Sky Sport 8 HD",
                    "Sky Sport Bundesliga 8 HD",
                    "Sky Sport 9 HD",
                    "Sky Sport Bundesliga 9 HD",
                    "Sky Sport 10 HD",
                    "Sky Serien & Shows HD",
                    "Deluxe Music HD",
                    "Comedy Central HD",
                    "Disney Channel HD",
                    "FUEL TV HD",
                    "Mezzo Live HD",
                    "Sky Cinema Premieren +24 HD",
                    "Sky Cinema Animation HD",
                    "CRIME + INVESTIGATION HD",
                    "SyFy HD",
                    "Sky Cinema Action HD",
                    "KBS World  HD",
                    "SONY CHANNEL HD",
                    "sonnenklar.TV HD",
                    "Juwelo HD",
                    "1-2-3.tv HD",
                    "CLASSICA HD",
                    "Sky Krimi HD",
                    "SWR RP HD",
                    "BR Fernsehen SĂŒd HD",
                    "NDR FS NDS HD",
                    "rbb Berlin HD",
                    "MDR Sachsen HD",
                    "hr-fernsehen HD",
                    "tagesschau24 HD",
                    "Sky Atlantic HD",
                    "Bibel TV HD",
                    "NRWision HD",
                    "Sky Sport 2 HD",
                    "FOX HD",
                    "QVC HD",
                    "Sky Sport Austria 2 HD",
                    "Sky Sport Austria 3 HD",
                    "Sky Sport 12 HD",
                    "Sky Sport 13 HD",
                    "CNBC HD",
                    "Sky Sport Bundesliga 5 HD",
                    "Sky Sport 6 HD",
                    "Sky Cinema Thriller HD",
                    "HDMI 1/MHL",
                    "HDMI 2",
                    "HDMI 3",
                    "HDMI 4/ARC"
                ],
                "volume_level": 0,
                "is_volume_muted": false,
                "media_content_id": "ZDF HD",
                "media_content_type": "tvshow",
                "media_title": "2: ZDF HD",
                "media_series_title": "Bettys Diagnose [19:25 - 20:15]",
                "source": "ZDF HD",
                "friendly_name": "Fernseher",
                "supported_features": 24505,
                "device_class": "tv",
                "icon": "mdi:television"
            },
            "last_changed": "2021-01-15T17:36:25.249562+00:00",
            "last_updated": "2021-01-15T18:38:05.989466+00:00",
            "context": {
                "id": "f18e9207ebaa68c72875d9d7ff3ed132",
                "parent_id": null,
                "user_id": null
            }
        }

Try to add this to your config.yaml and restart HA. It’ll create a sensor named ‘sensor.tv_channel’ with the current playing channel. You can use it as a trigger in your automations. You can replace ‘tv_channel’ & ‘TV channel’ with any name you like

sensor:
  - platform: template
    sensors:
      tv_channel:
        name: TV channel
        value_template: "{{state_attr('media_player.fernseher', 'media_title')}}"
1 Like

I just read that. You might like this one better. Goes ‘on’ if it’s a channel, ‘off’ if it’s a input

binary_sensor:
  - platform: template
    sensors:
      in_not_input:
        friendly_name: Is not input?
        value_template: "{{state_attr('media_player.fernseher', 'source') != 'HDMI 4/ARC' and state_attr('media_player.fernseher', 'source') != 'HDMI 3' and state_attr('media_player.fernseher', 'source') != 'HDMI 2' and state_attr('media_player.fernseher', 'source') != 'HDMI 1/MHL'}}"
2 Likes

Thank you very much @obaldius that seem linke a very nice solution! I think I will go for the second option and give it a try. I just need a bit more time.

1 Like

It works very fine! Could this solution be extended to include an additional state „off“?
That means three states and not on/off any more.
I am not experienced in templating, so any idea is helpful.

Or should I test for the off state in a condition in the automation?

Explain exactly what you’re trying to achieve. Maybe you meant this:

sensor:
  - platform: template
    sensors:
      tv_channel:
        name: TV channel
        value_template: >
          {% set t = state_attr('media_player.fernseher', 'source') %}
          {% if is_state('media_player.fernseher', 'off')%}
            off
          {% elif t == 'HDMI 4/ARC' or t == 'HDMI 3' or t == 'HDMI 2' or t == 'HDMI 1/MHL'%}
            Input
          {% else %}
            TV channel
          {% endif %}

If TV is off, sensor goes ‘off’, if tv is on it goes ‘TV channel’ or ‘Input’ depending on the content displayed.

1 Like

That is exactly what I meant. Many thanks! Impressive, what’s possible with the right knowledge.
I think I can now build additional sensors, based on this example.
I want to change some backlight according to the setting of the TV. (off, scene1, scene2)
But I could not yet get this running:

  • changed “name” to “friendly_name”
    but still I am missing something . . .
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got '=') for dictionary value @ data['sensors']['tv_setting']['value_template']. Got "{% set t = state_attr('media_player.fernseher', 'source') %} {% if is_state('media_player.fernseher', 'off') %}\n TVoff\n{% elif t = 'HDMI 4/ARC' or t = 'HDMI 3' or t = 'HDMI 2' or t = 'HDMI 1/MHL' %}\n TVinput\n{% else %}\n TVchannel\n{% endif %}". (See ?, line ?).

finally I found the solution: (the “==”)

sensor:
  - platform: template
    sensors:
      sony_tv_setting:
        friendly_name: "Sony TV Setting"
        value_template: >
          {% set t = state_attr('media_player.fernseher', 'source') %}
          {% if is_state('media_player.fernseher', 'off') %}
            aus
          {% elif t == 'HDMI 4/ARC' or t == 'HDMI 3' or t == 'HDMI 2' or t == 'HDMI 1/MHL' %}
            extern
          {% else %}
            TV Kanal
          {% endif %}
2 Likes

My mistake, sorry