Separate increasing and decreasing sensor

I have a victron shunt that measures consumed ah as positive and negative values depending on direction to the batteries. But the sensor does never reset and just continues to show the “ah” flow which isnt realy any useful data. I created a victron_smartshunt_energy_consumption_daily with the “utility_meter” but this sensor only records rising changes and not the lowering changes and doesnt seem to care about positive or negative values.

I’d like a sensor that monitors the decrementing values and sums them up and another one for the decrementing values and sums them up. Does anyone know some logic approach for this? Only thing I’ve seen seems to be automations for input numbers that copies values when they change but it seems like an waful lot of config, has to be an easier way?

ChatGPT gave me this, unfortunately it broke my HA bootup completely

template:
  - trigger:
      - platform: state
        entity_id: sensor.your_sensor  # Replace with your actual sensor
    sensor:
      - name: "Rising Value"
        unique_id: rising_value
        unit_of_measurement: "W"
        state_class: total_increasing
        device_class: energy
        state: >
          {% set prev = trigger.from_state.state | float(0) %}
          {% set curr = trigger.to_state.state | float(0) %}
          {% if curr > prev %}
            {{ ((states('sensor.rising_value') | float(0)) + (curr - prev)) | round(2) }}
          else %}
            {{ states('sensor.rising_value') | float(0) | round(2) }}
          {% endif %}

      - name: "Lowering Value"
        unique_id: lowering_value
        unit_of_measurement: "W"
        state_class: total_increasing
        device_class: energy
        state: >
          {% set prev = trigger.from_state.state | float(0) %}
          {% set curr = trigger.to_state.state | float(0) %}
          {% if curr < prev %}
            {{ ((states('sensor.lowering_value') | float(0)) + (prev - curr)) | round(2) }}
          else %}
            {{ states('sensor.lowering_value') | float(0) | round(2) }}
          {% endif %}

Stop using the LLM. It will only waste your time.

Try reading the utility meter documentation. There is an option in there that allows both increasing and decreasing the total.

You mean net_consumption? That would give me the net ofc which would be ok I guess. But I’d ideally want two sensors, one for power in and one for power out

dont really know how to do that with utility_meter, cant find anything about decreasing values

However I could invert the original sensor with a sensor template and then use that one for daily sensor, since it seems to record the incrementing values. kind of a bad way to do it I guess

What is the entity id of this sensor?

See: Help with energy sensor templates - Negative to positive - #4 by tom_l

its this one: sensor.victron_smartshunt_energy_consumption

I’m unsure if I understood your post in the link correctly, but it seems like it would only update the template sensor if the value is over or under 0. But what if the value is 40 for example, and then goes down to 35? Would it record that decrease? I would like to record all decreases but your example wouldnt I guess?

The template sensors update whenever the source sensor changes. That’s how template sensors work.

Both sensors would update (“trigger”) when this change occurs. Only the positive sensor would record the change though. The negative one would remain at zero.

It is not what you want however.

Your sensor is unsuitable for that example.

If I understand you correctly you want two sensors like this:

Also that is not your Victron sensor. It is not measured in Ah. Please share the actual sensor that your battery reports, not the thing you constructed. Also any other sensors the device reports.

yes I suspected that example wouldnt apply here

the sensor my esp32 gives me is this one: victron_smartshunt_consumed_ah

exact same but not multiplied with the voltage and divided by 1000 to get Wh in kWh

but yes, I want exactly what you drew. My current test is to have two utility meters, one for the original sensor and another for an inverted version of the original sensor with this:

{{ - (states('sensor.victron_smartshunt_energy_consumption') | float | round(0)) }}

Yeah that’s one way to do it.

The other way would be a utility meter with two tariffs, one for energy in, one for energy out. Then create a trend binary sensor that tracks if the energy is increasing or decreasing:

You switch which utiltiy meter tariff sensor accumulates based on that sensor state. See here for an example of using tariffs: How to use Utility Meter Tariffs to conditionally measure things

Your way has:

two template sensors
two utility meters

My way:

one template sensor
one utility meter (that generates two sensors)
one automation.

No real advantage to doing it either way. They both produce the two sensors you want.

Thanks for the help, I’ll see how my sensors behave tomorrow. Or else I’ll try your way