Error getting attribute value with "(" in

I am trying to get the Tesla PW2 attributes turned into sensor values (since these contain the energy numbers needed for the energy integration(), but the attributes have “(” and “)” in their names, which seems to throw YAML. Or maybe I am just doing it wrong.

These attributes can be accessed from the stored DB under those name (via the API), but I only get the following errors when trying to use them in a sensor template:

  • TemplateError(‘UndefinedError: ‘homeassistant.helpers.template.TemplateState object’ has no attribute ‘energy_imported_’’) while processing template ‘Template("{{states.sensor.powerwall_site_now.energy_imported_(in_kW)}}")’ for attribute ‘_attr_state’ in entity ‘sensor.site_import’
  • TemplateError(‘UndefinedError: ‘mappingproxy object’ has no attribute ‘energy_imported_’’) while processing template ‘Template("{{states.sensor.powerwall_site_now.attributes.energy_imported_(in_kW)}}")’ for attribute ‘_attr_state’ in entity ‘sensor.site_import’
  • TemplateError(‘UndefinedError: ‘mappingproxy object’ has no attribute ‘energy_exported_’’) while processing template ‘Template("{{states.sensor.powerwall_site_now.attributes.energy_exported_(in_kW)}}")’ for attribute ‘_attr_state’ in entity ‘sensor.site_export’
  • TemplateError(‘UndefinedError: ‘mappingproxy object’ has no attribute ‘energy_imported_’’) while processing template ‘Template("{{states.sensor.powerwall_battery_now.attributes.energy_imported_(in_kW)}}")’ for attribute ‘_attr_state’ in entity ‘sensor.battery_use’
  • TemplateError(‘UndefinedError: ‘mappingproxy object’ has no attribute ‘energy_exported_’’) while processing template ‘Template("{{states.sensor.powerwall_battery_now.attributes.energy_exported_(in_kW)}}")’ for attribute ‘_attr_state’ in entity ‘sensor.battery_charge’

As you can see it looks like the dictionary lookup key stopped at the first “(” and then complained that the shortened version wasn’t there. I not a YAML expert, so suggestions welcome. YIA

Just to clarify-
Your entity_id is sensor.powerwall_site_now and the attribute name is energy_imported_(in_kW). Is that correct?

Can you try using the following?
{{ state_attr('sensor.powerwall_site_now', 'energy_imported_(in_kW)') }}

Also, please provide the YAML codes that you use for creating template sensor. Maybe the problem lies because of an indentation or missing "".

I tried creating a dummy sensor and it works-
{{ state_attr('sensor.special_character', 'special_character_(hello)') }}

Thanks for the reply Ardy. Yes, you are correct about the sensor. My YAML (extract) is (the forum messes up the indent but I think it is correct)

  • platform: template
    sensors:
    battery_use:
    device_class: energy
    friendly_name: “Battery Use”
    unique_id: baimp
    value_template: >-
    {{states.sensor.powerwall_battery_now.attributes.energy_imported_(in_kW)}}
    unit_of_measurement: “kWh”

I tried the version you suggested - still got an error. (This is HA running under docker on a Pi3, if it makes any difference - latest version)

"Logger: homeassistant.helpers.template
Source: helpers/template.py:1417
First occurred: 11:00:05 AM (1074 occurrences)
Last logged: 1:13:03 PM

Template variable error: ‘mappingproxy object’ has no attribute ‘energy_imported_’ when rendering ‘{{states.sensor.powerwall_site_now.attributes.energy_imported_(in_kW)}}’

Maybe those attributes just are not there under the names I am using?

YAML now looks like:

  • platform: template
    sensors:
    site_import:
    device_class: energy
    friendly_name: “Site Import”
    unique_id: siimp
    value_template: >-
    {{state_attr(‘sensor.powerwall_site_now’, ‘energy_imported_(in_kW)’)}}
    unit_of_measurement: “kWh”

Oh, looking at the entity(ies, I have 4) it appears to be showing the right result now DESPITE the continuing error messages?!

You are using legacy format to create the sensor - that’s not going to work if you want to integrate it to the new energy integration. The sensor must have state_class of measurement which only supported by the modern template sensor configuration format. Can you try this?

template:
  - sensor:
      - name: "Tesla Energy Imported"
        unit_of_measurement: "kWh"
        state: >-
          {{ state_attr('sensor.powerwall_site_now', 'energy_imported_(in_kW)') }}
        state_class: measurement
        device_class: "energy"

You might also need to add the following line in your customize.yaml-

sensor.tesla_energy_imported:
  last_reset: '1970-01-01T00:00:00+00:00'

Sorry, need to clarify about energy_imported_(in_kW). What does this attribute value reports in? If it’s kW then you need to convert it to kWh using Riemann Integration.

Ah the dangers of relying on out of date documentation (which 90% of the HA documentation seems to be). The attribute is actually kWh, the naming is wrong. I will update all my YAML as you suggest, however (error logs aside) it does appear to work if you use state_attr() rather than the form I was originally trying.

Thanks again for the assistance. Must go do some real work now. I just wish the new energy feature had some place to put battery data!

Can you explain why you think that won’t work?

The issue was that there were special characters in the template that confused the renderer. Not the use of “legacy” vs “modern” template syntax.

Because state_class is not supported using the legacy format. He wants to integrate it to the energy integration.

but the error wasn’t complaining about “not a valid option for sensor”.

It was complaining specifically about the invalid template.

that might have ultimately been a secondary issue but the original issue/error posted about was the invalid template syntax. not about “state_class not supported”.

So saying that the solution was to use the 'modern" template integration instead of using 'legacy" to fix the invalid syntax error isn’t correct.

Sorry, my mistake for being unclear. The “that’s not going to work” sentence is directed to using legacy format for the new energy integration.

As for the confusion by the renderer, I already suggested the recommended approach according to the templating documentation on the #2 post. It appears that it already solves the problem according to this statement below-

I will revise my previous post so that it won’t be misleading.

Thank you :slight_smile:

1 Like