Help with value template from json data

I’m using the Rest platform to access my Cloudflare API to get some information on a firewall rule. Here is the value_template that I’m using:

value_template: '{{ value_json.result.rules[0].expression }}'

It returns the following:

(cf.tls_client_auth.cert_verified) or (ip.src eq fe82::e0xb:3xx:fe16:daaa) or (ip.src eq 192.168.177.42)

How do I change my value_template to only return the following part?

fe82::e0xb:3xx:fe16:daaa

Thank you

You could just use a couple of splits for the characters before and after:

value_template: "{{ value_json.result.rules[0].expression.split('(ip.src eq')[1].split(')')[0] }}"
1 Like

If you want this to be robust you’d need to provide context around what you expect the response to be. Will it always have 3 groups? Will the value you want always be in the second position? Etc.

Paste this is the template editor in developer tools and play around with it

{% set text = '(cf.tls_client_auth.cert_verified) or (ip.src eq fe82::e0xb:3xx:fe16:daaa) or (ip.src eq 192.168.177.42)' %}

option using character indexing
{{ text[48:74] }}

option using split with keywords and replace
{{ text.split(' or ').1.split(' eq ').1.replace(')','')}}

option using regex
{{ text | regex_findall_index(find='[0-9a-fx]{4}::[0-9a-fx:]{10,20}', index=0) }}

1 Like

This works great, thank you!
Both excellent solutions