Template split value_template

My plan is to use a Aqara Vibration Sensor to act as a garage door sensor for status Open/Closed.

The idea is to use the orientation. If someone has a better way please share both idea and code please. :slight_smile:

So far I have managed to get a template sensor for the orientation that returns the value: [0, 1, -89]

  - platform: template
    sensors:
      vibration_6_orientation:
        value_template: '{{ states.binary_sensor.vibration_6.attributes.orientation }}'
        friendly_name: 'Vibration Sensor Orientation'

Now I am trying to split the value template and just get -89 back but I can’t seem to get it correct with the syntax.

Any help would be appreciated? :smiley:

1 Like

what does this give you in the template editor:

value_template: "{{ state_attr('binary_sensor.vibration_6', 'orientation')[2] }}"

I suspect it won’t be as simple as extracting the 3rd element of the list as it is a string that looks like a list.

In which case you could use:

value_template: >
  {{ state_attr('binary_sensor.vibration_6', 'orientation')|regex_findall_index(find="-?\d+", index=2 }}

Thanks for the quick feedback and help.

The template editor do returns back:
value_template: “-89”
so it seems to work.

The second suggestion works also after a little correction. :slight_smile:

Thanks a lot!!

I’m curious to know what this was. I don’t see it.

Just a missing ) at the end of the regex part

value_template: >
  {{ state_attr('binary_sensor.vibration_6', 'orientation')|regex_findall_index(find="-?\d+", index=2 )}}
1 Like

Doh!