(Solved) Help extract substring from string states

Help me: I have sensor.myday whose states are: “27/03/2023”
I want to extract only the 27, how do I do that?

value_template: '{{ states("sensor.myday ")|int == 27}}'
Error

value_template: "{{ states('sensor.myday')[:2] }}"

if it’s always the first two characters (dd/mm/yyyy). If it’s not,

value_template: "{{ states('sensor.myday').split('/')[0] }}"

You can try these out in the Template Editor. I’ve used x because my system doesn’t have your sensor:

Your attempt says “convert the sensor state to an int (which will fail because the string can’t be converted, and no default was supplied); and return True if it’s equal to 27, or False if it isn’t”.

If you are trying to compare it to 27, both of my templates still return a string, so you could do this to return True for 27:

value_template: "{{ states('sensor.myday').split('/')[0] == '27' }}"
1 Like

Thanks bro very much <3