Error in making sensor, please help

I got some advise in making a sensor that removes negative and large kWh values.
For me this is mainly copy / paste while learning more and more.

Below the sensor i added in the configuration.yaml and the error I get.
Does someone sees what is wrong?

Templates

template:

  • sensor:
  • name: “Schrek Verlichting Energie verbruik”
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    state: >-
    {% set value = states(‘sensor.schrek_verlichting_electric_consumption_kwh’) | float(0) %}
    {% set const = 21474836.47 %}
    {% if value < 0 %}{{ (value + const) | round(2) }}
    {% elif < 5 %}{{ value }}
    {% endif %}

Logger: homeassistant.config
Source: config.py:864
First occurred: 19:56:10 (1 occurrences)
Last logged: 19:56:10
Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected ‘<’) for dictionary value @ data[‘sensor’][0][‘state’]. Got “{% set value = states(‘sensor.schrek_verlichting_electric_consumption_kwh’) | float(0) %} {% set const = 21474836.47 %} {% if value < 0 %}{{ (value + const) | round(2) }} {% elif < 5 %}{{ value }} {% endif %}”. (See /config/configuration.yaml, line 43).

You missed specifying what is less than 5.

{% elif value < 5 %}{{ value }}

Also you need to specify an {% else %} case for when the value is greater then or equal to 5, if you don’t the sensor will be undefined.

Also please format your posted config correctly in future.

Thanks Tom. I understand what you mean but do not know how to correct it.
If the value of : ‘sensor.schrek_verlichting_electric_consumption_kwh’ is higher than 5, than no value needs to be mentioned. At first I thought 0 needed to be mentioned but I learned this “resets” the counter.

I will pay attention to the format in future posts.

If you want an unavailable state when 5 or greater try this:

  - sensor:
      - name: “Schrek Verlichting Energie verbruik”
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        state: >
          {% set value = states('sensor.schrek_verlichting_electric_consumption_kwh') | float(0) %}
          {% set const = 21474836.47 %}
          {% if value < 0 %}
            {{ (value + const) | round(2) }}
          {% elif value < 5 %}
            {{ value }}
          {% else %}
            {{ this.state }}
          {% endif %}
        availability: >
          {{ states('sensor.schrek_verlichting_electric_consumption_kwh') | float(6) < 5 }}

Thanks. I will try this when I am back home.

I have added this but i get an error in the energy dashboard settings.
Any idea?

Schermafbeelding 2023-07-18 213322

Did you restart home assistant after creating it?
Where did you create it?
Errors in the log?

Yes i have restarted home assistant, after the sensor was created.

I have created in the configuration.yalm

The logs shows nothing when i search for this new sensor…

It does mention the original senor, not sure if it is related to the new sensor we are making.

2023-07-19 02:05:10.997 WARNING (Recorder) [homeassistant.components.sensor.recorder] Entity sensor.schrek_verlichting_electric_consumption_kwh from integration zwave_js has state class total_increasing, but its state is not strictly increasing. Triggered by state 79.3 (81.86) with last_updated set to 2023-07-19T00:02:58.748332+00:00. Please create a bug report at Issues · home-assistant/core · GitHub

2023-07-19 04:15:10.896 WARNING (Recorder) [homeassistant.components.sensor.recorder] Entity sensor.schrek_uv_electric_consumption_kwh from integration zwave_js has state class total_increasing, but its state is not strictly increasing. Triggered by state 66.59 (66.67) with last_updated set to 2023-07-19T02:10:26.093822+00:00. Please create a bug report at Issues · home-assistant/core · GitHub

That’s your issue. They both have the wrong state class.

At the mentioned time in the log a negative consumption was reported, followed with the same consumption positive. Further the correct consumption and high Peaks i want to have ignored. Should the negative stop the new sensor from working?

What problem are you trying to solve with this template?

What is the reason for it?

There may be a better way to approach this.

The goal was to solve 2 issues.
1 : issue with the Neo coolcam reporting negative values which is a known Neo coolcam issue.
This is the first part of the sensor.
2 : the energy dashbord is messed up with high incorrect values. Each day I need to adjust about 10 values for some power plugs at “developer tools” → statitics (see image).
The second part of the sensor should ignore the value if the usage is higher than x (currently 5)

I hope this makes sense…

Can you give a little more detail on the negative value issue?

Is it always the same value?

HA mentions “This device is buggy and sends large negative meter reports from time to time”. The include image shows a huge consumption. But I also have seen smal ones like just 2 kWh.

Ok, and why do you want to limit it to less than 5?

5 is just a number I thought was good. But reading by own previous post having small ones like 2, 5 is already to high. Most adjustments I need to make range from 10 to thousands.(247.37 in the previous post).

Good for what?

Why do you want to limit it?

Because the high mentioned consumpties are not correct and messes up the enery report. Therefor I want to have them removed.

So your sensor reports erroneous negative and positive values?

It is going to be very difficult to filter this. Especially as an energy sensor keeps increasing.

Can you post a history graph showing these wrong readings?