Time Active as condition in Automation

I need an automation that runs based on my solar system having batteries state above 90% and PV production above 2kW and then switching on the geyser, this I can achieve. I need these conditions to be active for more than 5 minutes before switching the geyser on.
I will have a second automation that will switch of the geyser when batteries fall below 90% and PV production is below 2kW, these conditions need to be active for more than 2 minutes before execution.

Please assist on how to check for the 5 and 2 minutes.
If this is done in YAML, in which step will it be, since I will have 2 triggers or should I add a third one?

Here are some examples: Automation Trigger - Home Assistant

automation:
  triggers:
    - trigger: numeric_state
      entity_id: sensor.temperature
      # At least one of the following required
      above: 17
      below: 25

      # If given, will trigger when condition has been for X time.
      for: "01:10:05"

Add in the trigger for: 00:05:00 or the minutes.

for:
  minutes: 5
1 Like

Also keep in mind that if you want the automation to run only when both those two things become true, you will need to trigger your automation when either one becomes true and then check whether the other is already true. This is typically called “matching triggers and conditions” because you’ll end up with triggers and conditions that look very similar. So, in pseudo code:

- triggers
  - trigger: pv production > 2kW
  - trigger: batteries > 90%
- conditions:
  - condition: pv production > 2kW
  - condition: batteries > 90%

However, your requirement to have both be active for 5 minutes throws a bit of complexity here. You can easily add for: "00:05:00" to each trigger, but you can’t add the same to each condition because you are using numeric state conditions which don’t allow that.

If you are ok with only the requirement that the trigger has to be true for 5 minutes but the other condition could have only been true for a moment, then you can leave it like this.

If you need both to be true for 5 minutes then you’ll need to create a threshold helper entity for each condition. Go to integrations → helpers → threshold and create one for each condition. Then in your automation conditions, instead of a numeric state condition, you can use a state condition and refer to the new helper, and that DOES allow you to specify for:

2 Likes

As explained by mekaneck, the challenge here is that two things (battery level and PV production) must exceed thresholds (90 and 2) for a minimum duration (5 minutes) in order to turn on the geyser. Similarly, the same two things must be below the same thresholds for a minimum duration (2 minutes) in order to turn off the geyser.

Home Assistant’s Numeric State Trigger allows for duration (using the for option) but Numeric State Condition does not. To meet your exact requirements, it requires additional Helpers.

Here is how I would handle your requirements. My suggestion employs two Trigger-based Binary Sensors and one automation. The single automation can turn the geyser on and off (so a second automation is not needed).

The examples below assume your entity for solar batteries is sensor.batteries and the entity for PV production is sensor.pv_production and its unit of measurement is kW. Naturally you will need to change the example’s entity_ids to match what you actually have in your system.

Here is the YAML configuration for two Trigger-based Template Binary Sensors. It should be placed in your configuration.yaml file under the template: key. If your file does not contain a template: key, remove the leading # symbol from the first line of the following YAML.

Click to reveal Trigger-based Binary Sensor YAML
#template:
  - triggers:
      - id: 'on'
        trigger: numeric_state
        entity_id: sensor.batteries
        above: 90
        for:
          minutes: 5
      - id: 'off'
        trigger: numeric_state
        entity_id: sensor.batteries
        below: 90
        for:
          minutes: 2
    binary_sensor:
      - name: Solar Batteries
        state: "{{ trigger.id }}"

  - triggers:
      - id: 'on'
        trigger: numeric_state
        entity_id: sensor.pv_production
        above: 2
        for:
          minutes: 5
      - id: 'off'
        trigger: numeric_state
        entity_id: sensor.pv_production
        below: 2
        for:
          minutes: 2
    binary_sensor:
      - name: Solar Production
        state: "{{ trigger.id }}"

After copy-pasting the YAML code into your configuration.yaml file, save the file and restart Home Assistant. Check for any associated errors in the Log.

The automation is short and simple:

Click to reveal automation YAML
alias: Geyser Control
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.solar_batteries
      - binary_sensor.solar_production
    to:
      - 'on'
      - 'off'
    from:
      - 'off'
      - 'on'
conditions:
  - condition: template
    value_template: |
      {{ states('binary_sensor.solar_batteries') ==
        states('binary_sensor.solar_production') }}
actions:
  - action: "switch.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: switch.geyser

NOTE

Due to the way Trigger-based Template Binary Sensors work, the initial state of these two entities will be unknown.

  • binary_sensor.solar_batteries
  • binary_sensor.solar_production

In order for them to report on or off, you must wait until they are triggered at least once. That means waiting until:

  • Battery level either increases above 90 for 5 minutes or decreases below it for 2 minutes.
  • PV production either increases above 2kW for 5 minutes or decreases below it for 2 minutes.

So a little patience is needed because until that happens for both binary_sensors, they will report unknown and the automation will not be triggered.

You can check for one as condition and other for action. You create two triggers and add id to both. Create actions that run based on the ids.


Triggers
  - trigger: pv production > 2kW
    Id: PV
    Condition: time

  - trigger: batteries > 90%
  - Id: battery
  - condition: time
  


Action
 - if Trigger by id: PV
   and: battery > 90 some time
   And: some check for if already running
   Do: x

 - if Trigger by id: battery
   and: PV > 2kw some time
   And: some check for if already running
   Do: x

A Numeric State Condition (or a Template Condition) can handle the “Battery > 90” portion. However, Numeric State Condition does not track the value’s duration so it cannot do the “some time” part of the pseudo-code.

What did you have in mind to implement “some time”?

Actual minutes and seconds

Sorry. I was only giving concept not literal. Idea being that checks can occur at point of action giving more options and flexibility if ID is added to initial trigger

I see.

I thought you might know how to easily convert “battery > 90 some time” into working code, namely something the OP might be able to use.

I suggest you try it for yourself. You’ll see the “some time” part is challenging to implement as a test within an automation’s actions.