Split Entity State

I would like to pull the 1st 20 characters from the results

this is the entity name : sensor.mybuttoncardsensor
this is the return state : return entity.state.split(“|”)[0]
this is the output : Mixed dry recycling (blue lidded bin) and glass (black box or basket)|2|19.04.2024|Fri

Im happy to learn if someone could point me in the right direction.


The following template returns the first 20 characters of the sensor’s state value.

{{ states('sensor.mybuttoncardsensor')[:20] }}

So if the value is this:

Mixed dry recycling (blue lidded bin) and glass (black box or basket)|2|19.04.2024|Fri

the template will report this:

Mixed dry recycling

You are a star, for the quick reply, I’m struggling where to put it, thanks again.

this is my template

- platform: waste_collection_schedule
  name: MyButtonCardSensor
  value_template: '{{value.types|join(", ")}}|{{value.daysTo}}|{{value.date.strftime("%d.%m.%Y")}}|{{value.date.strftime("%a")}}'

and this is my card code

type: custom:button-card
entity: sensor.mybuttoncardsensor
layout: icon_label
show_label: true
label: |
    var days_to = entity.state.split("|")[1]
    if (days_to == 0)
    { return "Today" }
    else if (days_to == 1)
    { return "Tomorrow" }
    { return "in " + days_to + " days" }
show_name: true
name: |
    return entity.state.split("|")[0]
  - color: red
    operator: template
    value: '[[[ return entity.state.split("|")[1] == 0 ]]]'
  - color: orange
    operator: template
    value: '[[[ return entity.state.split("|")[1] == 1 ]]]'
  - value: default
    - justify-self: start;
    - overflow: unset
    - background: transparent
    - color: black
    - border-style: none;
    - width: 25%
    - color: yellow;
    - font-size: 18px;
    - align-self: middle
    - color: white;
    - font-size: 14px;

button-card accepts JS, not jinja.
And it is YOU who decides in which element of button-card (state? label? name? custom field?) you wish to show this “truncated” state.

Thanks I didn’t even realise I could use custom fields, I’m newish to all this.

could you help with the syntax

  shortdesc: |
    {{ states('sensor.mybuttoncardsensor')[:20] }}

Ive also tried

  shortdesc: "{{ states('sensor.mybuttoncardsensor')[:20] }}"

Its not displaying correctly

Suggest to ask button-card-related questions in the dedicated thread:

1 Like

There are 2 things here:

  1. To access a state of some entity in JS (which is used inside button-card), you need to use
  1. Now you need to use smth similar to “[:20]” from jinja - but written in JS. I am not an expert in JS, try to google it.

May be better to ask in the thread I mentioned above.