Replacing text in a template sensor with a string made of substrings

I am picking up track position, track length and track remaining from two different places in a REST sensor, and I display it like this:

image

This is in Norwegian, so it means “2:25 of 2:46, 0:21 remaining”. It’s exactly what I want, but one thing: When I stop the track it shows “Live” for both, and I have removed that. But then I get this:

image

What I would like, is for the " av , igjen" to be removed. But I can’t seem to find out how to do a replace of that in the full sensor code.

This is how the code looks:

      - name: "JRMC-posisjon"
        unique_id: jrmc_posisjon
        value_template: >
          {{ ((value_json['Response']['Item']|selectattr('@Name','==','PositionDisplay')|first)['#text'].split('/')[0]) }} av {{ ((value_json['Response']['Item']|selectattr('@Name','==','PositionDisplay')|first)['#text'].split('/')[1])|replace(' Live', '') }}, {{ (value_json['Response']['Item']|selectattr('@Name','==','RemainingTimeDisplay')|first)['#text']|replace('Live', '')|replace('-', '') }} igjen

I would assume that there has to be some combination of brackets or paranthesis to replace the text in the final outcome, but I can’t find out what. Can somebody please help me out?

You seem to only need to replace at the end, not bits…
so have the whole string and then: replace(‘av Live, Live igjen’,‘’)

Thanks for answering! I need to replace some of the stuff I get from REST first, because that has characters that I am not using. And I only need to remove av Live, Live igjen when it has that exact combination of characters, because that means that there is no info for total track length and track remaining coming through REST.

So I tried to put one or two paranthesis in front of the line and adding )replace(‘av Live, Live igjen’,‘’) to the end, like this:

({{ ((value_json['Response']['Item']|selectattr('@Name','==','PositionDisplay')|first)['#text'].split('/')[0]) }} av {{ ((value_json['Response']['Item']|selectattr('@Name','==','PositionDisplay')|first)['#text'].split('/')[1])|replace(' Live', '') }}, {{ (value_json['Response']['Item']|selectattr('@Name','==','RemainingTimeDisplay')|first)['#text']|replace('Live', '')|replace('-', '') }} igjen)replace(‘av Live, Live igjen’,‘’)

But I must have misunderstood that answer, because that did not do a replace, it gave me this:

image

You do have to read a it more into templating on how to concatenate / split things
Then, note that between av and Live there are 2 (!) spaces
This should work

{{ (((value_json['Response']['Item']|selectattr('@Name','==','PositionDisplay')|first)['#text'].split('/')[0]) + " av " +  ((value_json['Response']['Item']|selectattr('@Name','==','PositionDisplay')|first)['#text'].split('/')[1]) + ", " +  (value_json['Response']['Item']|selectattr('@Name','==','RemainingTimeDisplay')|first)['#text'] + " igjen").split("av  Live,")[0] }}

Yeah, I know that’s one of my many weak points… Not yours, obviously. But your code is missing one replace from the original code. I would like to get rid of the minus sign, dash, - in front of the remaining time. I tried to put the code for that |replace('-', '') at the end, both with and without a ) just in front and with and without an extra ( at the beginning, but that just messed it up, of course

No clue as no data, you have to try this out yourselves

If you post the JSON that is returned from the REST sensor, it would make it easier for someone to help you out

Damn, I thought I had. But it was in my other thread yesterday about another part of that setup. My bad, here it is:

<Response Status="OK">
<Item Name="ZoneID">0</Item>
<Item Name="ZoneName">Hovedsone</Item>
<Item Name="State">0</Item>
<Item Name="FileKey">78199</Item>
<Item Name="NextFileKey">78198</Item>
<Item Name="PositionMS">0</Item>
<Item Name="DurationMS">198000</Item>
<Item Name="ElapsedTimeDisplay">0:00</Item>
<Item Name="RemainingTimeDisplay">Live</Item>
<Item Name="TotalTimeDisplay">Live</Item>
<Item Name="PositionDisplay">0:00 / Live</Item>
<Item Name="PlayingNowPosition">0</Item>
<Item Name="PlayingNowTracks">30</Item>
<Item Name="PlayingNowPositionDisplay">1 of 30</Item>
<Item Name="PlayingNowChangeCounter">2</Item>
<Item Name="Bitrate">0</Item>
<Item Name="Bitdepth">0</Item>
<Item Name="SampleRate">0</Item>
<Item Name="Channels">0</Item>
<Item Name="Chapter">0</Item>
<Item Name="Volume">1</Item>
<Item Name="VolumeDisplay">100% (+0,0 dB)</Item>
<Item Name="ImageURL">MCWS/v1/File/GetImage?File=78199</Item>
<Item Name="Artist">Pink Floyd</Item>
<Item Name="Album">The Wall</Item>
<Item Name="Name">In the Flesh</Item>
</Response>

So I’m picking PositionDisplay from the first part of that info, TotalTimeDisplay from two steps down, replacing it with nothing if it’s “Live”, and finally RemainingTimeDisplay, where I want to remove the dash if it’s anything but “Live” and “Live” if it is “Live”. Is that remotely logical?

I won’t get to this until later today, but I at least got this into a format where it can easily be worked on in the template editor for anyone who wants to mess with it:

{% set value_json = 
  {"Response":{"@Status":"OK","Item":[{"#text":"0","@Name":"ZoneID"},{"#text":"Hovedsone","@Name":"ZoneName"},{"#text":"0","@Name":"State"},{"#text":"78199","@Name":"FileKey"},{"#text":"78198","@Name":"NextFileKey"},{"#text":"0","@Name":"PositionMS"},{"#text":"198000","@Name":"DurationMS"},{"#text":"0:00","@Name":"ElapsedTimeDisplay"},{"#text":"Live","@Name":"RemainingTimeDisplay"},{"#text":"Live","@Name":"TotalTimeDisplay"},{"#text":"0:00 / Live","@Name":"PositionDisplay"},{"#text":"0","@Name":"PlayingNowPosition"},{"#text":"30","@Name":"PlayingNowTracks"},{"#text":"1 of 30","@Name":"PlayingNowPositionDisplay"},{"#text":"2","@Name":"PlayingNowChangeCounter"},{"#text":"0","@Name":"Bitrate"},{"#text":"0","@Name":"Bitdepth"},{"#text":"0","@Name":"SampleRate"},{"#text":"0","@Name":"Channels"},{"#text":"0","@Name":"Chapter"},{"#text":"1","@Name":"Volume"},{"#text":"100% (+0,0 dB)","@Name":"VolumeDisplay"},{"#text":"MCWS/v1/File/GetImage?File=78199","@Name":"ImageURL"},{"#text":"Pink Floyd","@Name":"Artist"},{"#text":"The Wall","@Name":"Album"},{"#text":"In the Flesh","@Name":"Name"}]}}
  %}

{{ ((value_json['Response']['Item']|selectattr('@Name','==','PositionDisplay')|first)['#text'].split('/')[0]) }} av {{ ((value_json['Response']['Item']|selectattr('@Name','==','PositionDisplay')|first)['#text'].split('/')[1])|replace(' Live', '') }}, {{ (value_json['Response']['Item']|selectattr('@Name','==','RemainingTimeDisplay')|first)['#text']|replace('Live', '')|replace('-', '') }} igjen

This should work out of the box

{% set item = value_json['Response']['Item'] %}
{% set position, total = (item|selectattr('@Name','eq','PositionDisplay')| map(attribute='#text')|first| default('/')).split('/') %}
{% set remaining = item|selectattr('@Name','eq','RemainingTimeDisplay')| map(attribute='#text')|first| default('') %}
{% if position and total and remaining and total != 'Live' and remaining != 'Live' %}
  {{ position }} av {{ total }}, {{ remaining }} igjen
{% else %}
  00:00
{% endif %}

Thanks for chiming in, both of you! @petro Almost, but the dash is still there before the remaining time of the track:

image

just replace it then remaining | replace('-','')

Thank you again! That did it! It took me a couple of tries to find out exactly how, I thout I had to add another remaining, but then it dawned on me! :rofl:

image