Cannot parse @ in value template sensor

No, he changed it for testing. His error is related to startup issues with the update to 2021.5 where silent errors produced by things being ‘undefined’ in templates are no longer ignored. This results in the template sensor failing the onboarding process and a listener is never created. You basically have to safely access things from here on out, even at startup when things aren’t initialized.

See his error:

It’s referencing Device which is a key inside root. I.E. root is set to none. That only happens at startup (Typically).

1 Like

Hi Petro and Troon,

Thank you for your quick replies. As Petro suggested at the time I built this, there is already a check builtin if root was defined or not:

      solar_live_output_kw:
        value_template: >
            {% set root = state_attr('sensor.solar_inverter', 'root') %}
            {% if root.Device.Measurements.Measurement[2]["@Value"] is defined %}
                {{ (root.Device.Measurements.Measurement[2]["@Value"] | float / 1000) }}
            {% else %}
                0
            {% endif %}
        device_class: power
        unit_of_measurement: 'kW'
        friendly_name: Live opgewekt in kW

Now, without changing anything, BUT upgrading to core-2021.6.6 the issue disappeared and showed me data again:

Same here :wink: (Also working) - Had to be a buggy implementation in early 2021.6.x releases.

With the new Energy focus in HA 2021.8 I am wondering what the criterias is for our Steca inverters to be able to be selectable as a Solar input?
I know that it is Power (Watt) that is measured, and Energy (wh) that is wanted as input - but no matter how I tweak any sensor I can not figure out how to get anything working.
Do i need to go to a ModBus solution for this to work?

Any ideers?

All thank you for this threat it helped me a lot on using REST with my inverter.

@nila nila mayby you can use the http://ip_address/yields.xml page. That should sum up the amount of energy created.

Or try all.xml for all options of your inverter.

Every day my home assistant stops responding some where in the night. It started after using the REST integration as Rene (see threat)

The error i get is:

2021-09-22 03:16:42 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: dict object has no element 2') while processing template 'Template("{% set root = state_attr('sensor.solar_frontier', 'root') %} {% if root.Device.Measurements.Measurement[2]["@Value"] is defined %}
    {{ root.Device.Measurements.Measurement[2]["@Value"] | round(0)}}
{% else %}
    0
{% endif %}")' for attribute '_attr_native_value' in entity 'sensor.solar_live_output'

Below is the config i am using

# Configuration.yaml entry
sensor:
  - platform: rest #Solar Frontier Inverter
    resource: http://x.x.x.x/measurements.xml
    json_attributes:
      - root
      - Measurements
    name: Solar_Frontier
    scan_interval: 60
    timeout: 20
    value_template: 'OK'
  - platform: template
    sensors:
      solar_live_output:
        value_template: >
            {% set root = state_attr('sensor.solar_frontier', 'root') %}
            {% if root.Device.Measurements.Measurement[2]["@Value"] is defined %}
                {{ root.Device.Measurements.Measurement[2]["@Value"] | round(0) }}
            {% else %}
                0
            {% endif %}
        device_class: power
        unit_of_measurement: 'W'
        friendly_name: Solar Frontier Live power

The inverter has all value’s at daytime but at night some value disappear from measurements.xml (complete lines disappear at night)

<Measurement Value="239.392" Unit="V" Type="AC_Voltage"/>
<Measurement Value="12.826" Unit="A" Type="AC_Current"/>
<Measurement Value="3043.300" Unit="W" Type="AC_Power"/>
<Measurement Value="50.000" Unit="Hz" Type="AC_Frequency"/>
<Measurement Value="612.804" Unit="V" Type="DC_Voltage"/>
<Measurement Value="5.050" Unit="A" Type="DC_Current"/>
<Measurement Value="3099.480" Unit="W" Type="DC_Power"/>
<Measurement Value="37.000" Unit="°C" Type="Temp"/>

Any1 a suggestion on how to fix this?

{% set root = state_attr('sensor.solar_frontier', 'root') %}
{% if root.Device.Measurements.Measurement is defined and root.Device.Measurements.Measurement 
 | length > 0 %}
  {{ (root.Device.Measurements.Measurement | selectattr('@Type', 'eq', 'AC_Current') | list | first)['@Value'] | default }}
{% else %}
  0
{% endif %}

Petro thanks a lot it works!!

Most of the error messages have disappeared. There is only one error left every day. Is there a way to resolve this condition to?

2021-09-28 19:15:41 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: No first item, sequence was empty.') while processing template 'Template("{% set root = state_attr('sensor.solar_frontier', 'root') %} {% if root.Device.Measurements.Measurement is defined and root.Device.Measurements.Measurement | length > 0 %}
  {{ (root.Device.Measurements.Measurement | selectattr('@Type', 'eq', 'AC_Power') | list | first)['@Value'] | default }}
{% else %}
  0
{% endif %}")' for attribute '_attr_native_value' in entity 'sensor.solar_live_output'

And now the complete error hoping someone can help me solve it

Logger: homeassistant.helpers.event
Source: helpers/template.py:399
First occurred: 2:00:46 AM (2 occurrences)
Last logged: 2:00:46 AM

Error while processing template: Template("{% set root = state_attr('sensor.solar_frontier', 'root') %} {% if root.Device.Measurements.Measurement is defined and root.Device.Measurements.Measurement | length > 0 %} {{ (root.Device.Measurements.Measurement | selectattr('@Type', 'eq', 'AC_Power') | list | first)['@Value'] | default }} {% else %} 0 {% endif %}")
Error while processing template: Template("{% set root = state_attr('sensor.solar_frontier', 'root') %} {% if root.Device.Measurements.Measurement is defined and root.Device.Measurements.Measurement | length > 0 %} {{ (root.Device.Measurements.Measurement | selectattr('@Type', 'eq', 'Temp') | list | first)['@Value'] | default }} {% else %} 0 {% endif %}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 397, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1604, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 2, in top-level template code
  File "/usr/local/lib/python3.9/site-packages/jinja2/sandbox.py", line 303, in getitem
    return obj[argument]
jinja2.exceptions.UndefinedError: No first item, sequence was empty.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 513, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 399, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: No first item, sequence was empty.

root.Device.Measurements.Measurement has no @Type = AC_Power, apparently

I think the problem is that sometimes @Type = AC_Power is not available. But i dont know how to change the command to first check is it is there or not.

Hi Petro, sorry to bother you, but I lost my sensor data also last few days. Worked fine untill then.
The ‘REST’ results in data:

This is how the sensor is configured:

What goes wrong?

look in the logs for errors, you likely don’t have 7 measurements

I do have more… 22 in total.
Here are the first.


Log is showing:

Your template is using the wrong entity_id

On top of that, it’s also using an outdated configuration. entity_id for template sensors was deprecated almost 4 years ago.

I’m starting to think you just copied this today from Rene’s post above without altering it.

So I deleted the entity_id.
Unfortunately it results in the same errors:

Yes, because the entity_id in the template is incorrect. Do you have an entity named sensor.solar_inverter?

@gerlof — you can create this in the UI now.

Create a Template Sensor helper with this state template, which looks up the name of the measurement rather than relying on its position:

{{ (state_attr('sensor.stecagrid','root')['Device']['Measurements']['Measurement']
   |selectattr('@Type','eq','AC_Power')|first)['@Value'] }}

Like this, which shows Unavailable for me because I don’t have your entity:

My test: