How to use a numeric state with a "for:" condition (duration) in a template

Hi there,

Please allow me to pick your brains. It should be easy, but I currently cannot get my head around it… I am controlling the charger of my electric car such way that I only use excess solar power; it works great and I love it!

But I am not satisfied yet with my logic to switch between one and three phased charging. It works quite well already but coming closer to autumn I would like to avoid too many switches between one- and three-phased charging in order to reduce wear on the electronics.

Basically, I have an input_select helper with three states (“too low”, “1-phase”, “3-phase”) and I would like to change between these states if (and only if) a statistical sensor constantly stays above (or below) a thresholds for a minimum duration of 10 minutes.

This is part where I cannot get my head around: Currently I have an automation which runs on a time pattern and decides on the preferred state. But I would like to add the condition about the duration. The only solution I see is to split it up in 4 separate automations and to use for: minutes: 10 as a trigger or conditions to fire it off, but shouldn’t it be possible to write this simple logic a bit smarter? For example as a template in the config file?

Currently it looks like this:

choose:
  - conditions:
      - condition: numeric_state
        entity_id: sensor.solar_to_ingolf_available_power_lowpass_filtered
        above: 1380
        for:          {# this doesn't work in the 'action' part of my automation, maybe in a template? #}
          hours: 0    {# this doesn't work in the 'action' part of my automation, maybe in a template? #}
          minutes: 10 {# this doesn't work in the 'action' part of my automation, maybe in a template? #}
          seconds: 0  {# this doesn't work in the 'action' part of my automation, maybe in a template? #}
      - condition: state
        entity_id: input_select.solar_to_ingolf_charging_mode
        state: too low
        for:
          hours: 0
          minutes: 10
          seconds: 0
    sequence:
      - service: input_select.select_option
        data:
          option: one phase
        target:
          entity_id: input_select.solar_to_ingolf_charging_mode

# etc. etc. ...

If of interest, here’s how I would set it up with 4 separate automations:

  • (a) change the state form “too low” to “1-phase” if the power surplus sensor constantly is above 1380 for 10 minutes AND the current state is “too low”.

  • (b) change the state from “1-phase” to “3-phase” if the power surplus sensor constantly is above 3680 W for 10 minutes AND the current state is “1-phase”

  • (c) change the state from “3-phase” to “1-phase” if the power surplus sensor constantly stays below 3680 W for 10 minutes AND the current state is “3-phase”

  • (d) change the state from “1-phase” to “too low” if the power surplus sensor constantly stays below 1380 W for 10 minutes AND the current state is “1-phase”

Most likely the solution is terribly easy… And sorry for the noise, by the way :wink:

Using “for” is not supported in numeric conditions. If it’s not in the docs it isn’t a thing.

You can have one automation with 2 triggers that use the for: paramerter and use a template to select the option for your input select in the action (if I understand your desired logic correctly).

You weren’t consistent with your option naming (one phase, 1-phase) in your question so you might have to change those.

    variables:
      current: "{{ states('sensor.solar_to_ingolf_available_power_lowpass_filtered')|int(-1) }}"                
    trigger:
      - platform: numeric_state
        entity_id: sensor.solar_to_ingolf_available_power_lowpass_filtered
        below: 1380
        above: 1380
        for: 
          minutes: 10

      - platform: numeric_state
        entity_id: sensor.solar_to_ingolf_available_power_lowpass_filtered
        below: 3680
        above: 3680
        for: 
          minutes: 10
    condition: "{{ current > 0 }}"
    action:
      - service: input_select.select_option
        target:
          entity_id: input_select.solar_to_ingolf_charging_mode      
        data:
          option: >
            {% if current < 1380 %} too low
            {% elif 1380 <= current < 3680 %} one phase
            {% else %} three phase
            {% endif %}

Hi Jason,

Thanks a lot for pushing me in the right direction! I missed the fact that I actually can define a number of triggers and that the automation will start if any of them fires.

Just as a side note: Isn’t this a thing that typically should go into a template (in the config file, I mean)? An entity which is purely calculated on the basis of another entity. But well, I will give it a shot with your suggestion, thanks!