How to get specific in an array of attributes

Hello,

How to get the specific attribute from this global entity with the bellow result.
This is the result of my entity:

faces:
  - name: James
    confidence: 71.771
    entity_id: image_processing.id_tester
total_faces: 1
friendly_name: id_tester
device_class: face

For example, I can get easely the total_faces attribute with this in my configuration.yaml:

sensor:
  - platform: template
    sensors:
    face_recognition_id_faces: 
       friendly_name: "Face Recognition faces"
       value_template: "{{ state_attr('image_processing.id_tester', 'total_faces') }}" `

But question is :
I want to get also name and confidence in seperate entity that are under face attribute which is an array to use automation.

Do you know how to proceed to get the state of this attribute in a particular entity ?

Please format your pasted code correctly. See point 11 here: How to help us help you - or How to ask a good question

Hi Tom_I, I have updated the code as request
Any update on this topic ? :slight_smile:

Can you take a screenshot of the attributes of this sensor image_processing.id_tester in the developer tools states menu?

Hello tom_I, here it is:
image

I meant the right hand column in the states list.

Hello tom_I, here it is:

Any idea to get only my name from this entity ? :slight_smile:

1 Like

Try this in the developer tools template editor:

{{ state_attr('image_processing.id_tester', 'faces')[name] }}

I doubt it will work but give it a try.

Thanks for the suggestion, but no luck with that :(ā€¦

Yeah I didnā€™t think it would. Sigh. Looks like regex to the rescue again.

Try:

  value_template: >
    {{ state_attr('image_processing.id_tester', 'faces')|regex_findall_index("- name:\s(.+$)") }}

Oh yeah, for the confidence level:

  value_template: >
    {{ state_attr('image_processing.id_tester', 'faces')|regex_findall_index("confidence:\s(.+$)") }}

If you want to know how it works you can learn regular expressions here: https://regexone.com/

Hello tom,

Thank a lot for this logical !
Regex is very powerfull. I had to modify a little bit your in order to get what I want because the output was like that:

- 'name': 'Jeremy', 
'confidence': 77.498,
'entity_id': 'image_processing.id_tester'

I had put this in order to get only name and confidence as state:

     face_recognition_id_name: 
       friendly_name: "FR - name"
       value_template: >
            {{ state_attr('image_processing.id_tester', 'faces')|regex_findall_index("....\s'(.+)',") }}
     face_recognition_id_confidence:  
       friendly_name: "FR - confidence"    
       value_template: >
            {{ state_attr('image_processing.id_tester', 'faces')|regex_findall_index(".confidence...(.+),") }}

So now it works, I can get the state:

Thanks a lot for your help man :slight_smile:

2 Likes

Hey, I had the same problem and I solved it by using different regex:

facebox_person_detection:
friendly_name: ā€˜Facebox person detectionā€™
value_template: >-
{% if (state_attr(ā€˜image_processing.facebox_sonoff_local_fileā€™, ā€˜total_facesā€™)|int) == 0 -%}
Nobody
{%- else -%}
{{ state_attr(ā€˜image_processing.facebox_sonoff_local_fileā€™, ā€˜facesā€™) | regex_findall_index(".nameā€™:\s+?ā€™([\w+#\s]+)ā€™?") }}
{%- endif %}

Let me know if it works for you as well.

vehicles:

  • plate: 5nn45
    confidence: 0.903
    region_code: eu
    vehicle_type: Sedan

Can you please help to make - plate attribute a sensor? I tried the above example but no go.

I know this is an older thread, but I had the same problem getting temperature values from a weather entity.

The entity is weather.muckleford_south with attributes:

temperature: 33.9
humidity: 19
pressure: 1016
wind_bearing: 300
wind_speed: 5
visibility: 60.2
forecast: 
- condition: partlycloudy
  precipitation: 0
  precipitation_probability: 10
  temperature: 33
  templow: 14
  datetime: '2022-01-09T13:00:00+00:00'
  wind_bearing: 181
  wind_speed: 16.9
- condition: rainy
  precipitation: 3.9
  precipitation_probability: 80
  temperature: 30
  templow: 18
  datetime: '2022-01-10T13:00:00+00:00'
  wind_bearing: 144
  wind_speed: 22.3
- condition: rainy
  precipitation: 1.3
  precipitation_probability: 50
  temperature: 30
  templow: 17
  datetime: '2022-01-11T13:00:00+00:00'
  wind_bearing: 119
  wind_speed: 17.3
- condition: lightning
  precipitation: 17.4
  precipitation_probability: 80
  temperature: 29
  templow: 19

I extract a condition with:

{{ state_attr('weather.muckleford_south', 'forecast')[0].condition }}

or

{{ state_attr('weather.muckleford_south', 'forecast')[1].condition }}

or

{{ states.weather.muckleford_south.attributes.forecast[0].condition }}

To the OPā€™s original question:

{{ state_attr('image_processing.id_tester', 'faces')[0].entity_id }}

or

{{ 'sensor.' + states.image_processing.id_tester.faces[0].entity_id + '_condition' }}

should work.

For example.
I hope this helps someone.

4 Likes

Iā€™m trying to extract values from a complex nested attribute structure and this thread is as close as I could get to what I need but not quite yet.

Iā€™m using Solcast integration which exposes among others

sensor.solcast_forecast_tomorrow

which has as state tomorrowā€™s estimate in kWh and as attribute a lot of related information namely the hourly forecasts (the paste below is truncated to the first 9 hours)

resource_id: my_secret_resource_id
hourly: 
- '00:00':
    pv_estimate: 0
    pv_estimate10: 0
    pv_estimate90: 0
- '01:00':
    pv_estimate: 0
    pv_estimate10: 0
    pv_estimate90: 0
- '02:00':
    pv_estimate: 0
    pv_estimate10: 0
    pv_estimate90: 0
- '03:00':
    pv_estimate: 0
    pv_estimate10: 0
    pv_estimate90: 0
- '04:00':
    pv_estimate: 0
    pv_estimate10: 0
    pv_estimate90: 0
- '05:00':
    pv_estimate: 0
    pv_estimate10: 0
    pv_estimate90: 0
- '06:00':
    pv_estimate: 0
    pv_estimate10: 0
    pv_estimate90: 0
- '07:00':
    pv_estimate: 125.4
    pv_estimate10: 14.3
    pv_estimate90: 302.3
- '08:00':
    pv_estimate: 1476.3
    pv_estimate10: 162.7
    pv_estimate90: 4159.4
- '09:00':
    pv_estimate: 4292
    pv_estimate10: 525.1
    pv_estimate90: 10227.4

I want to extract the pv_estimate value for a given hour using a template but after many attempts canā€™t figure out how to do it.

Iā€™ve tried

{{ state_attr('sensor.solcast_forecast_tomorrow','hourly')[9] }}

which gives me for the 9th hour

{'09:00': {'pv_estimate': 4292.0, 'pv_estimate10': 525.1, 'pv_estimate90': 10227.4}}

but after that Iā€™m lost and canā€™t get any further to just extract the pv_estimate value.
This is a dictionary within a dictionary and I would know how to do it if it was JSON but I canā€™t figure out how to do it when it is within an attribute.

Any help or pointers appreciated.

PS: I can use a REST sensor and query the solcast API directly and template the JSON output to get what I want. But that means I have both the Solcast integration which is convenient because, well, integrated, and my own REST sensor. Thatā€™s messy and doubles the number of Solcast API calls. So Iā€™m looking for a more elegant solution using the sensor attributes which contain the data I want

The other arrays are similar with the same attributes and different values. I am trying to fetch the name attribute from each of these objects.
.

{{ state_attr('sensor.solcast_forecast_tomorrow','hourly')[9].pv_estimate }}

Thanks but I tried that and it doesnā€™t work.
From another forum, the owner of the Solcast integration, I got the working expression

{{ state_attr('sensor.solcast_forecast_today', 'hourly')[9]['09:00']['pv_estimate'] }}

The trick is in the [ā€œ09:00ā€] which moves the template further onto the data. I missed that and was trying ā€œ09:00ā€ which also didnā€™t work.

Your solution would have worked if there wasnā€™t the double dictionary thing. Thanks anyhow.

Edit: Whereas in the developer tools/template the double quotes in ā€œ09:00ā€ do work it throws an error when in the yaml file. I had to put single quotes as in ā€˜09:00ā€™ to clear the error.

@Eddie1974 Iā€™m sure you got there but for anyone else this worked for me with Plate Recognizer:

{{ state_attr('image_processing.platerecognizer_frontyard_west_sub', 'vehicles')[0]['plate'] }}