Notifications for AndroidTV 2

A few questions -

Is it possible to insert line breaks in “message:” ? maybe template?

Is it possible to use variable in duration? maybe from track length?

alias: Notifications for AndroidTV - Media Player
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.androidtv_2
    from: null
    to: playing
condition: []
action:
  - service: notify.android_tv
    data:
      title: ""
      message: >-
        SqueezeBox - {{ states('media_player.androidtv_2') }} {{
        state_attr('media_player.androidtv_2', 'media_content_type') }} {{
        state_attr('media_player.androidtv_2', 'media_title')
        }}                  {{ state_attr('media_player.androidtv_2',
        'media_content_id') }}
      data:
        duration: 30
        position: top-right
        fontsize: large
        transparency: 25%
        color: gray
        interrupt: 0
        icon:
          url: >-
            http://localhost:8123{{ state_attr('media_player.androidtv_2',
            'entity_picture') }}
mode: single

the way i’d do the line breaks is to change the >- to | and format the text with the linebreaks where you want them:

alias: Notifications for AndroidTV - Media Player
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.androidtv_2
    from: null
    to: playing
condition: []
action:
  - service: notify.android_tv
    data:
      title: ""
      message: |
        SqueezeBox - {{ states('media_player.androidtv_2') }} 
        {{  state_attr('media_player.androidtv_2', 'media_content_type') }} 
        {{  state_attr('media_player.androidtv_2', 'media_title') }} 
        {{ state_attr('media_player.androidtv_2',   'media_content_id') }}
      data:
        duration: 30
        position: top-right
        fontsize: large
        transparency: 25%
        color: gray
        interrupt: 0
        icon:
          url: >-
            http://localhost:8123{{ state_attr('media_player.androidtv_2',
            'entity_picture') }}
mode: single

yes, i believe duration can take a template pointing to whatever value you want. you said variable, but you don’t have one here. perhaps you meant attribute? do it like you have in the message.

thanks, but the literal line break doesn’t work for me. neither does double quote method (“1\n2\n3”); it actually converted it to |-. the values in message from state_attr stay on same line. and, yes duration works with variable value from state_attr. am i missing something? new to yaml…

alias: Notifications for AndroidTV - Media Player
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.androidtv
    from: null
    to: playing
condition: []
action:
  - service: notify.androidtv
    data:
      title: "{{ state_attr('media_player.androidtv', 'friendly_name') }}"
      message: |
        {{ state_attr('media_player.androidtv', 'media_title') }}
        {{ state_attr('media_player.androidtv', 'media_artist') }}
        {{ state_attr('media_player.androidtv', 'media_album_name') }}
      data:
        duration: "{{ state_attr('media_player.androidtv', 'media_duration') }}"
        position: top-right
        fontsize: large
        transparency: 25%
        color: black
        interrupt: 0
        icon:
          url: >-
            http://{{ states('sensor.local_ip') }}:8123{{
            state_attr('media_player.androidtv', 'entity_picture') }}
mode: single

Using | sends the line breaks. however I suspect your TV is then discarding it for space. I have seen that before. if that’s the case, then I don’t know that there is much you can do

Ok. ill look into it more. i also just noticed that it does not fire if attr doesnt exist; internet radio may not provide artist or album. need to add condition, but how do i say if attribute exist (without specifying value) ?

Ok, i figured it out. this will fire different notifications depending on if they are streams with no artist, album, and duration info. just got to figure out the line break issue.

alias: Notifications for AndroidTV - Media Player
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.androidtv
    from: null
    to: playing
action:
  - if:
      - condition: template
        value_template: >-
          {{ state_attr('media_player.androidtv', 'media_artist') == None }}
      - condition: template
        value_template: >-
          {{ state_attr('media_player.androidtv', 'media_album_name')  == None }}
      - condition: template
        value_template: >-
          {{ state_attr('media_player.androidtv', 'media_duration')  == None }}
    then:
      - service: notify.androidtv
        data:
          title: "{{ state_attr('media_player.androidtv', 'friendly_name') }}"
          message: |
            {{ state_attr('media_player.androidtv', 'media_title') }}
            {{ state_attr('media_player.androidtv', 'media_content_id') }}
          data:
            duration: 90
            position: top-right
            fontsize: large
            transparency: 25%
            color: black
            interrupt: 0
            icon:
              url: >-
                http://{{ states('sensor.local_ip') }}:8123{{
                state_attr('media_player.androidtv', 'entity_picture') }}
     
    else:
      - service: notify.androidtv
        data:
          title: "{{ state_attr('media_player.androidtv', 'friendly_name') }}"
          message: |
            {{ state_attr('media_player.androidtv', 'media_title') }}
            {{ state_attr('media_player.androidtv', 'media_artist') }}
            {{ state_attr('media_player.androidtv', 'media_album_name') }}
          data:
            duration: "{{ state_attr('media_player.androidtv', 'media_duration') }}"
            position: top-right
            fontsize: large
            transparency: 25%
            color: black
            interrupt: 0
            icon:
              url: >-
                http://{{ states('sensor.local_ip') }}:8123{{
                state_attr('media_player.androidtv', 'entity_picture') }}
mode: single



You might try the literal CR entity or something like that it is Mini HTML viewer  

your code:

{{ state_attr('media_player.androidtv', 'media_artist') == none }}

is a template that evaluates to true if the media_artist attribute doesn’t exist. (i prefer lower case, but it looks like both works).

would you explain more or point me to info on literal CR entity?

See ASCII Character Chart with Decimal, Binary and Hexadecimal Conversions

You could try:




It all depends on how the characters are interpreted (and not stripped).
I use these in many HTML displays like in flex-table and such to get another line when I want it.

Those above are CR, you might also try LF (linefeed) and a combination like CR LF.

As in:

message: |
        {{ state_attr('media_player.androidtv', 'media_title') }}
        {{ state_attr('media_player.androidtv', 'media_artist') }}

that would be quite interesting if those chars work. please let us know!

this isnt working for me. it just prints out



it prints out /n when i add

 {{'
'}} 

but does not break line

bummer… honestly this is what I expected, but was hoping I was wrong…

OK, I have seen other posts ad I do not have your system to test.
Have you tried sending …

<br>

or

\\n

The first would be an HTML break, the second would escape the break (which may work for Android but not iPhone/iPad).

Just throwing out things here, the best is to examine in debug what exactly is being sent and how it is being interpreted.

If I send a notification to my Android tablet, this works with multiple lines:

service: notify.mobile_app_tab1
data:
  message: >
    {{"This is a message <br> with other <br> stuff.<br>Oh look, multiple lines!"}}

It is possible your television does not support the same, I do not know as I do not have your TV to test.

//n or {{‘//n’}} doesn’t work
but <br> does indeed work (i thought i had already tried it)
wasn’t sure if i could use html tags

thanks

 message: >-
            {{ state_attr('media_player.androidtv', 'media_title')
            }}<br>{{state_attr('media_player.androidtv', 'media_artist')
            }}<br>{{state_attr('media_player.androidtv', 'media_album_name') }}
1 Like

Awesome the solution works. More interesting if you could do this:

message: >-
            <h3>{{ state_attr('media_player.androidtv', 'media_title')
            }}</h3><br>
          <h4>{{state_attr('media_player.androidtv', 'media_artist')
            }}</h4><br>
           <h4>{{state_attr('media_player.androidtv', 'media_album_name') }}</h4>

Meaning what HTML tags do work? Can we format each line differently?

I guess so … because this just worked on my Android tablet:

service: notify.mobile_app_tab1
data:
  message: >-
    {{"This is a message <br> with other <br> stuff.<br>Oh look, multiple
    lines! and now a heading? <h4>WOW a Heading</h4>"}}

it looks like html elements may work for text strings, it does not format text inserted from script {{ }} (or whatever you want to call it), ie

<h3>{{ state_attr('media_player.androidtv', 'media_title') }}</h3>

I also test other elements like <b>, <u>, <font>, even <img> - no luck

strange since <br> works for me

Try something like this:

service: notify.mobile_app_tab1
data:
  message: >-
    {{"<h4>"}}
    {{states('sensor.sun_next_dawn')}}
    {{"</h4>"}}

Works for me.

Note a paragraph works also which you can use to break lines. SO this uses both br and p as well as font formatting:

service: notify.mobile_app_tab1
data:
  message: >-
    {{"<p>Regular:<br>"}}
    {{states('sensor.sun_next_dawn')}}
    {{"</p>"}}
    {{"<p><b>Bold:<br>"}}
    {{states('sensor.sun_next_dawn')}}
    {{"</b></p>"}}
    {{"<p><i>Italic:<br>"}}
    {{states('sensor.sun_next_dawn')}}
    {{"</i></p>"}}

This works also (setting colors):

service: notify.mobile_app_tab1
data:
  message: >-
    {{"<p><font color='red'>Red:<br>"}}
    {{states('sensor.sun_next_dawn')}}
    {{"</font></p>"}}
    {{"<p><b>Bold:<br>"}}
    {{states('sensor.sun_next_dawn')}}
    {{"</b></p>"}}
    {{"<p><i>Italic:<br>"}}
    {{states('sensor.sun_next_dawn')}}
    {{"</i></p>"}}

Enough testing. Thanks for bringing this up, I may use this for many of my notifications!

thanks, but not working for me, maybe androidtv thing.

message: >-
           {{ "<p><font color='red'>Red:<br>" }}
           {{ state_attr('media_player.androidtv', 'media_title') }}
           {{ "</font></p>" }}

this just prints out

agree, enough time on this, im just happy i can break lines, and of course that some one created this integration

What? Are you missing the image?
Because it could very well be the difference of an expanded notification and one that is not.

For me to see what I show in those screenshots, I have to drag the notification screen down to see (and if there are more then one, drag it twice).

Of course this would not be possible on TV, BUT I think your can send it the data portion the fact you wish it to auto expand.

i mean, when i use this

message: >-
           {{ "<p><font color='red'><br>" }}
           {{ state_attr('media_player.androidtv', 'media_title') }}
           {{ "</font></p>" }}

with notifications for android tv i get this

with standard notifications on my galaxy s22 (android 14) i get this

with standard notifactions on old tablet (android 8.1) i get this

it is what it is, not a big deal. i have more important issues than this. like trying to figure out how (or if possible) to interrupt current notification, which has duration set by media_duration attribute, when skipping tracks. the automation works fine if you dont skip track; triggered by state changing to playing and when media_content_id changes. but this may be a notifications for android tv app issue, dont see any settings to allow new notifications to interrupt / replace current.

duh, it was in front of my face the whole time - interrupt: 1

spoke to soon not working with notifications for android tv

and yes im missing the icon or image on standard notifications