Need help with Template Sensor extracting 3rd comma seperated value of an attribute

Hey folks,

I’ve a connected a Xiaomi Vibration Sensor to hass. It can measure it’s angle and I want do place it on my mailbox so whenever the lid opens I can get a notification that mail has been delivered. The Device outputs an attribute called orientation. Orientation is in format of 3 values seperated by 2 commas. See:
vib

I need to access the last value behind the second comma. This value can also be negative. Can anyone help me? I’ve tried with a template sensor using regex but no success.

Thanks

sensor:
  - platform: template
    sensors:
      orientation:
        friendly_name: "Orientation"
        unit_of_measurement: '°'
        value_template: "{{ state_attr('binary_sensor.vib_briefkasten', 'orientation').split(',')[2] }}"

state_attr('binary_sensor.vib_briefkasten', 'orientation') returns the full string: 75,3,14.

It is split into a list at the commas using .split(',')

The third list item is returned by [2] as list numbering starts at 0.

1 Like

Thanks for your help but it’s not working.
vib

This one did it!

{{ states.binary_sensor.vib_briefkasten.attributes.orientation[2] }}

This format will give no errors if the sensor is unavailable:

{{ state_attr('binary_sensor.vib_briefkasten', 'orientation')[2] }}

Awesome. Thank you!

Still not sure why my original template is giving that error. Playing with the template editor to work it out…

Ah! The attribute is already a list. Same error as you got when I use a list:

It would appear that the attribute’s value is understood to be a list and not a string. That’s why the error message reports a list object doesn’t support split.

In the following example, I set value to 75, 3, 14 which the Jinja2 interpreter understood as (75, 3, 14) thereby allowing me to address items within the list.


EDIT
It would appear you already arrived at that conclusion while I typing away …

1 Like

Yep, but thanks for taking the time to confirm.