Total energy counter

Can someone help me get the following into yaml correctly ?
In `the development template it looks good, but I can’t get it into yaml without errors.

  - sensor:
    - name: "Total power use"
    {% if states('sensor.omvormer_active_power') in ("unavailable", "unknown") %}
    state: "{{states('sensor.power_consumption')|float}}"
    {% else %}
    state: "{{states('sensor.omvormer_active_power')|float/1000 + states('sensor.power_consumption')|float - states('sensor.power_production')|float}}"
    {% endif %}

Try this:

  - sensor:
    - name: "Total power use"
      state: >
            {% if states('sensor.omvormer_active_power') in ("unavailable", "unknown") %}
                "{{states('sensor.power_consumption')|float}}"
            {% else %}
                "{{states('sensor.omvormer_active_power')|float/1000 + states('sensor.power_consumption')|float - states('sensor.power_production')|float}}"
            {% endif %}

thank you,

So I had to add state: > ?

But now get the message:

invalid config for [template]: [name] is an invalid option for [template]. Check: template->name. (See /config/configuration.yaml, line 58).

You can try it like this:

  - platform: template
    sensors:
      total_power_use:
        friendly_name: Total power use
        value_template: >-
          {% if states('sensor.omvormer_active_power') in ("unavailable", "unknown") %}
              "{{states('sensor.power_consumption')|float}}"
          {% else %}
              "{{states('sensor.omvormer_active_power')|float/1000 + states('sensor.power_consumption')|float - states('sensor.power_production')|float}}"
          {% endif %}

This code should be in the template sensors… Something like this:

template:
  - sensor:
    - name: "Total power use"
      state: >
            {% if states('sensor.omvormer_active_power') in ("unavailable", "unknown") %}
                "{{states('sensor.power_consumption')|float}}"
            {% else %}
                "{{states('sensor.omvormer_active_power')|float/1000 + states('sensor.power_consumption')|float - states('sensor.power_production')|float}}"
            {% endif %}

See documentation:

The code proposed by @pepe59 is the legacy method (and should work) but is not the recommended method which is to move sensors under template… See the following link:

The last solution worked!
I’m going to read it all again tomorrow to understand.

Going to bed now I get up early tomorrow.

Thank you both very much,

Did some fine tuning and everything seems to be working fine.
thanks again.

  - sensor:
    - name: "Total power use"
      state: >-
            {% if states('sensor.omvormer_active_power') in ('unavailable', 'unknown') %}
                {{states('sensor.power_consumption')}}
            {% else %}
                {{states('sensor.omvormer_active_power')| float /1000 + states('sensor.power_consumption') | float - states('sensor.power_production') | float | round(3)}}
            {% endif %}
      unit_of_measurement: "KW

But you should add float defaults, according to this

1 Like

If I may ask the question here:

default float

If I understand correctly, the default value is the value that the status should assume if no numerical value is received from a sensensor ?

Yes and no. But not the status overall, but only the part, where you use the float filter. And you have some of them. If only one state is not there, the calculation will not work without defaults for float-filters.

Thank you,
If I understand correctly, the default values ​​will prevent you from getting errors in your log?

In addition, I should write a separate routine if this error occurs.

For example in the above : a default value of -13.

If a default value of -13 is assigned then a sensor is not working properly then do this…