Please help me convert a value template for a sensor from number to human readable text

I have several REST sensors that checks the web interface of the JRMC media player. The interface gives this result:

<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>

The problematic rest sensor is this:

      - name: "JRMC Playback State"
        unique_id: jrmc_playback_state
        value_template: >
          {{ (value_json['Response']['Item']|selectattr('@Name','==','State')|first)['#text'] }}

This gives one of three possible numerical values, 0 for stopped, 1 for paused and 2 for playing. Can somebody please tell me how I can get those as human readable values instead of the numbers?

Not sure if this will work but…

{% set ix = (value_json['Response']['Item']|selectattr('@Name','==','State')|first)['#text'] %}
{{ ['Stopped','Paused','Playing'][ix] }}

EDIT: possibly this can be a one-liner all in all

Thanks for answering! I have probably made a mistake, but neither version did work. The sensor value was empty. I tried both this:

      - name: "JRMC-avspillingsstatus"
        unique_id: jrmc_avspillingsstatus
        value_template: >
          {% set ix = (value_json['Response']['Item']|selectattr('@Name','==','State')|first)['#text'] %} {{ ['Stopped','Paused','Playing'][ix] }}

And this:

      - name: "JRMC-avspillingsstatus"
        unique_id: jrmc_avspillingsstatus
        value_template: >
          {% set ix = (value_json['Response']['Item']|selectattr('@Name','==','State')|first)['#text'] %}
          {{ ['Stopped','Paused','Playing'][ix] }}

Edit: The versions above are the correct version of the sensor name in Norwegian, I translated “avspillingsstatus” to “playback state” to make it clearer what it was. But I don’t think that should matter, right?

ix is just used to collect the value from the array with strings, it must be a number though so 0 and not ‘0’ and I cannot replicate your rest sensor so not sure

what does the state show when you use this:

          {% set ix = (value_json['Response']['Item']|selectattr('@Name','==','State')|first)['#text'] %}
          {{ ix }}

Right, I would assume that the problem is that the zero comes as string instead of an integer because of the track name and artist name being the same format. I know how to change string to integer in Python, but is it possible in this YAML string?

Edit: Sorry, I did’t see your message! Just a sec, I’l test it.

| int

The code two posts ago gave me 0 as a result for the sensor.

Maybe pipe to int

EDIT: it does not help when I cannot replicate :frowning:

So if I want to assume that the response as string and convert that to an integer, how would that be? Not like this:

        value_template: >
['Item']|selectattr('@Name','==',int|'State')|first)['#text'] %}
          {{ ['Stopped','Paused','Playing'][ix] }}

That gives me an error.

Edit: Checked now in Node-RED. It really is a string. So I need to convert it to integer.

as mentioned…try to | int

Yes, I saw that. I just don’t know where to put it. As I wrote in the previous post I tried to put it before ‘State’, but that gave an error. Sorry if I’m dense.

It took me a while to find out how rest works with xml but, this works with me

        {% set ix = (value_json['Response']['Item']|selectattr('@Name','==','State')|first)['#text'] | int %}
          {{ ['Stopped','Paused','Playing'][ix] }}

and so does this

 {{ ['Stopped','Paused','Playing'][(value_json['Response']['Item']|selectattr('@Name','==','State')|first)['#text'] | int] }}

Thank you very much! That one line version worked!