How to return only positive numbers - Template coding

Newbie here with regards to automation in Home Assistant.

I have the below code, that I use to adjust the amperage my car charger draws, based on the current estimated consumption in the current hour for the rest of the house. The goal is to ensure I never exceed 10kWh of usage in a single hour, as that incurs a higher electricity tariff.

The code works ok, but I am struggling with negative values. If the house consumption is already high, and close to the 10 kWh limit, sometimes the code I wrote will try and give my chargers a negative amperage value.

alias: Effektstyring
description: ""
trigger:
  - platform: time_pattern
    minutes: /1
condition: []
action:
  - service: easee.set_circuit_dynamic_limit
    data:
      circuit_id: XXXXXX
      currentP1: >-
        {{ (43 -
        (states('sensor.estimated_consumption_current_hour') |
        float/230*1000)) | int }}
      currentP2: >-
        {{ (43 -
        (states('sensor.estimated_consumption_current_hour') |
        float/230*1000)) | int }}
      currentP3: >-
        {{ (43 -
        (states('sensor.estimated_consumption_current_hour') |
        float/230*1000)) | int }}
mode: single

So if the calculation of amperage (the CurrentP1, P2 and P3 in the code) left based on the sensor.estimated_consumption_current_hour is more then 43, then the calculation returns a negative value. My car charger does not accept a negative value, and hence it discards it.

Is there a way to code this so that if the result is a negative value, it will return 0, instead of a negative value?

Try this:

action:
  - service: easee.set_circuit_dynamic_limit
    data:
      circuit_id: XXXXXX
      currentP1: >
        {{ [ 0, (43 - (states('sensor.estimated_consumption_current_hour') | float(0)/230*1000)) | int(0) ] | max }}
      currentP2: >
        {{ [ 0, (43 - (states('sensor.estimated_consumption_current_hour') | float(0)/230*1000)) | int(0) ] | max }}
      currentP3: >
        {{ [ 0, (43 - (states('sensor.estimated_consumption_current_hour') | float(0)/230*1000)) | int(0) ] | max }}

Use the function “max” like this:

{{ max(0, ((43 - (states('sensor.estimated_consumption_current_hour') | float(0)/230*1000))  |  int(0) )) }}

Why not trigger on state change of: sensor.estimated_consumption_current_hour?
If that is the only value in the calculation that actually matters then there is no reason to recalculate the exact same value every minute if there is no change?
Adding the condition car is charging means we can remove another few thousand calculations that is not needed.

1 Like

Thanks! I’ll try the max function! :+1:

Reason for triggering once a minute is that the sensor that estimates consumptions updates (and changes) every 3s, so it was actually an attempt to not have it calculate “constantly”. Adding a “car is charging” is a good idea though!

I just realised all your templates are the same. If you want to save a few cpu cycles you could use a variable to calculate the current only once instead of three times.

action:
  - variables: 
      current: "{{ [ 0, (43 - (states('sensor.estimated_consumption_current_hour') | float(0)/230*1000)) | int(0) ] | max }}"
  - service: easee.set_circuit_dynamic_limit
    data:
      circuit_id: XXXXXX
      currentP1: "{{ current }}"
      currentP2: "{{ current }}"
      currentP3: "{{ current }}"