Problem with Riemann sum integration

Hello,

I have a template sensor obtained fron a shelly EM with this yaml code:

template:
  - trigger:
      - platform: state
        entity_id:
          - sensor.shellyem_c45bbe6c51fe_channel_2_power
    sensor:
      - name: "Apartamento resto potencia barato"
        device_class: power
        unit_of_measurement: W
        state: >
          {%  if states('input_boolean.apartamento_horario_barato') == 'on' %}
          {{ states('sensor.shellyem_c45bbe6c51fe_channel_2_power') }}
          {% else %}
          {{0 | float }}
          {% endif %}

From that sensor the integration platform to obtain the Riemann sum integration with this yaml code:

sensor:
  - platform: integration
    source: sensor.apartamento_resto_potencia_barato
    name: Apartamento Resto Energía Barato
    unit_prefix: k
    round: 2

The problem is that the integral has a jump which has no sense from the input.

Here it is the resulting capture:

What am I doing bad? I don’t know if I should change the integral method from trapezoidal to left.

I think that the problem is that sensor.apartamento_resto_potencia_barato does not update its value while input_boolean.apartamento_horario_barato is off, so that the previous value is a 0, but from some time ago, which contributes as the area of a trapezoid veeery big, resulting in the jump of the sum.

I have 3 questions then:

  • Am I right?
  • Why does the sensor.apartamento_resto_potencia_barato not update with 0 each time the sensor triggers by a change in sensor.shellyem_c45bbe6c51fe_channel_2_power?
  • How can I get the desired behaviour?

Maybe the answer to the third question is using left method in integration.

Thank you!

sensor:
  - platform: integration
    source: sensor.apartamento_resto_potencia_barato
    name: Apartamento Resto Energía Barato
    unit_prefix: k
    round: 2
    method: left

However the Shelly EM has an energy sensor. You don’t need to do this.

1 Like

Yes, I know, but I’m doing this because I want to discriminate and obtain energy and cost of cheap and expernsive periods of my electricity tariff.

input_boolean.apartamento_horario_barato is on when electricity is cheap, and off when it`s expensive.

That’s exactly what the Utility meter tariff option is for.

Create a utility meter from your EM energy sensor with two tariffs, e.g, expensive and cheap.

Use an automation to switch the tariff, it can trigger from a change of state of your input boolean.

You end up with two energy sensors, one totalling cheap energy, one totalling expensive.

1 Like

Ohh, then I won’t reinvent the wheel and I’ll use it.

Thank you!