How can i extract a number from a sensor that gives me numbers and word

Hi there,

i have a sensor which brings me this value:

[{"organizationUnitId":30246,"personCount":101,"maxPersonCount":1400}]	

i only want to extract the number 101 - this number can be between 0 and 1400 so i cannot crop the text left and right.

Has someone an idea?

{{ (states('sensor.mysensor')|from_json)['personCount'] }}

Is the jinja code you would need, but you would need to provide more information about WHERE you want to use the number? A notification? A new sensor?

Hi sorry for weak informations.
I want to add a scraper and there i need a template for.

Assuming your value is a string exactly as you wrote, since it’s a dictionary inside a list, you would need this:

{{ (value | from_json)[0]["personCount"] }}

I’ve never used the Scrape integration, but according to this, value is the name of the variable inside value_template.

2 Likes

Yes, although if it is already JSON that is being delivered you can skip that too and just go straight to:

{{ value_json[0]['personCount'] }}
2 Likes

thanks a lot this works great!

1 Like