Suggsy
(Suggsy Blackburn)
April 17, 2024, 3:03pm
1
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.
thanks
123
(Taras)
April 17, 2024, 3:08pm
2
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
Suggsy
(Suggsy Blackburn)
April 17, 2024, 3:45pm
3
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" }
else
{ return "in " + days_to + " days" }
]]]
show_name: true
name: |
[[[
return entity.state.split("|")[0]
]]]
state:
- color: red
operator: template
value: '[[[ return entity.state.split("|")[1] == 0 ]]]'
- color: orange
operator: template
value: '[[[ return entity.state.split("|")[1] == 1 ]]]'
- value: default
styles:
card:
- justify-self: start;
- overflow: unset
- background: transparent
- color: black
- border-style: none;
icon:
- width: 25%
label:
- color: yellow;
- font-size: 18px;
- align-self: middle
name:
- 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.
Suggsy
(Suggsy Blackburn)
April 18, 2024, 7:23am
5
Thanks I didn’t even realise I could use custom fields, I’m newish to all this.
could you help with the syntax
custom_fields:
shortdesc: |
[[[
{{ states('sensor.mybuttoncardsensor')[:20] }}
]]]
Ive also tried
custom_fields:
shortdesc: "{{ states('sensor.mybuttoncardsensor')[:20] }}"
Its not displaying correctly
Suggest to ask button-card-related questions in the dedicated thread:
Button card
Lovelace Button card for your entities. -> github
[all]
Features
works with any toggleable entity
3 actions on tap toggle, more_info and service
state display (optional)
custom color for on and off state (optional)
custom state definition with customizable color (optional)
custom size (optional)
custom icon (optional)
custom css style (optional)
automatic color for light (optional)
custom default color for lights (when color cannot be determined) (optional)
2 color types
icon :…
1 Like
There are 2 things here:
To access a state of some entity in JS (which is used inside button-card), you need to use
states['sensor.mybuttoncardsensor'].state
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.