Automation condition based on last trigger time other automation(s)

Hello,

I have a number of (similar) automations that will trigger if a certain energy level is detected, the difference between these automations is mainly the detected energy level and the smartplugs that will be activated.

I would like to add a condition that avoids an automation from running if another automation (from lets say a list of 9 automations) has been triggered within the last 30 seconds. I tried to create a helper with a 30 second timer that activates once the automations runs and added the condition to the automation but I didn’t manage to get this to work.

Can anyone point me in the right direction?

Below the YAML for one of these automations.

alias: Boven 900W
description: ""
triggers:
  - type: power
    device_id: e0526a8c835a1b71ebb9b667a001264b
    entity_id: e3ccc1913bc4163ae168b61d1c57347a
    domain: sensor
    trigger: device
    above: 900
    below: 1200
conditions:
  - type: is_power
    condition: device
    device_id: a091fb88a4e2f5e188dde60fbbb67936
    entity_id: 26fda7c22bea942358af1e19942fbd5a
    domain: sensor
    above: 0
actions:
  - type: turn_on
    device_id: aaa58f1913083bf4511ff7d6c9e3be45
    entity_id: c3d733b33d12171a17dba9c3032b0199
    domain: switch
  - type: turn_on
    device_id: d8a2b0881d9180cafa00aac0fefd8ea5
    entity_id: 3691be51a5c0117835d9253d2996e828
    domain: switch
  - type: turn_off
    device_id: 8cc9d8252cf4706b0c4f1a1849008cd7
    entity_id: 9e08bf64aae495ae5c5150ada1b7fc6f
    domain: switch
  - type: turn_off
    device_id: 1099fc1ff86152c551027ab99aefdcfc
    entity_id: 7140b1014bc935f8db260bbeeede2a40
    domain: switch
  - type: turn_off
    device_id: c41f692a25e6f5553d52d437c3d8f7e9
    entity_id: fb4d53e69a34ff386c3bb892e4e21c50
    domain: switch
mode: single

This sounds like a situation where the 9 similar automations can be consolidated into a single automation. That would eliminate the need to create a condition that checks if some other automation was triggered.

To determine if it’s feasible, post all or some of the other automations.


NOTE

It’s preferable to use State Triggers, State Conditions, and standard actions instead of the Device Triggers/Conditions/Actions in your example. The resulting YAML is not only more concise, it provides more flexibility (especially if the goal is to consolidate multiple automations).

alias: Boven 900W
description: ""
triggers:
  - trigger: numeric_state
    entity_id: sensor.your_sensor_1
    above: 900
    below: 1200
conditions:
  - condition: numeric_state
    entity_id: sensor.some_other_sensor
    above: 0
actions:
  - action: switch.turn_on
    target:
      entity_id:
        - switch.your_switch_1
        - switch.your_switch_2
  - action: switch.turn_off
    target:
      entity_id:
        - switch.your_switch_3
        - switch.your_switch_4
        - switch.your_switch_5

Thanks for your help. I am eager to learn how to do this the right way. Below some additional explanation to the automations.

The triggers are all very similar (min and max sensor power). The power is related to power availability at a given moment.

The conditions:

  • first condition (is_power) will check if measured remaining power availability is above 0
  • second condition (numeric_state) is checking if boiler charge status is above or below 95% => I have some extra duplicate automations base on both above and below 95… so this could be simplified also I hope?

The actions are enabling a combination of smartplugs, which are always the same list but enable/disabled in different combinations.

Below I have added three other automations:

1200W-1650W measured:

alias: Boven 1200W
description: ""
triggers:
  - type: power
    device_id: e0526a8c835a1b71ebb9b667a001264b
    entity_id: e3ccc1913bc4163ae168b61d1c57347a
    domain: sensor
    trigger: device
    above: 1200
    enabled: true
    below: 1650
conditions:
  - type: is_power
    condition: device
    device_id: a091fb88a4e2f5e188dde60fbbb67936
    entity_id: 26fda7c22bea942358af1e19942fbd5a
    domain: sensor
    above: 0
    enabled: true
  - condition: numeric_state
    entity_id: input_number.boiler
    below: 95
actions:
  - type: turn_on
    device_id: 8cc9d8252cf4706b0c4f1a1849008cd7
    entity_id: 9e08bf64aae495ae5c5150ada1b7fc6f
    domain: switch
  - type: turn_off
    device_id: d8a2b0881d9180cafa00aac0fefd8ea5
    entity_id: 3691be51a5c0117835d9253d2996e828
    domain: switch
  - type: turn_off
    device_id: aaa58f1913083bf4511ff7d6c9e3be45
    entity_id: c3d733b33d12171a17dba9c3032b0199
    domain: switch
  - type: turn_off
    device_id: 1099fc1ff86152c551027ab99aefdcfc
    entity_id: 7140b1014bc935f8db260bbeeede2a40
    domain: switch
  - type: turn_off
    device_id: c41f692a25e6f5553d52d437c3d8f7e9
    entity_id: fb4d53e69a34ff386c3bb892e4e21c50
    domain: switch
mode: single

Above 1650W:

alias: Boven 1650W
description: ""
triggers:
  - type: power
    device_id: e0526a8c835a1b71ebb9b667a001264b
    entity_id: e3ccc1913bc4163ae168b61d1c57347a
    domain: sensor
    trigger: device
    above: 1650
    below: 2100
conditions:
  - type: is_power
    condition: device
    device_id: a091fb88a4e2f5e188dde60fbbb67936
    entity_id: 26fda7c22bea942358af1e19942fbd5a
    domain: sensor
    above: 0
  - condition: numeric_state
    entity_id: input_number.boiler
    below: 95
actions:
  - type: turn_on
    device_id: d8a2b0881d9180cafa00aac0fefd8ea5
    entity_id: 3691be51a5c0117835d9253d2996e828
    domain: switch
  - type: turn_on
    device_id: 8cc9d8252cf4706b0c4f1a1849008cd7
    entity_id: 9e08bf64aae495ae5c5150ada1b7fc6f
    domain: switch
  - type: turn_off
    device_id: aaa58f1913083bf4511ff7d6c9e3be45
    entity_id: c3d733b33d12171a17dba9c3032b0199
    domain: switch
  - type: turn_off
    device_id: 1099fc1ff86152c551027ab99aefdcfc
    entity_id: 7140b1014bc935f8db260bbeeede2a40
    domain: switch
  - type: turn_off
    device_id: c41f692a25e6f5553d52d437c3d8f7e9
    entity_id: fb4d53e69a34ff386c3bb892e4e21c50
    domain: switch
mode: single

This is an example of a similar automation but taking into account more than 1750W of power produced with an already charged boiler (> 95%).

alias: Boven 1750W (no boiler)
description: ""
triggers:
  - type: power
    device_id: e0526a8c835a1b71ebb9b667a001264b
    entity_id: e3ccc1913bc4163ae168b61d1c57347a
    domain: sensor
    trigger: device
    above: 1750
    below: 2200
conditions:
  - type: is_power
    condition: device
    device_id: a091fb88a4e2f5e188dde60fbbb67936
    entity_id: 26fda7c22bea942358af1e19942fbd5a
    domain: sensor
    above: 0
  - condition: numeric_state
    entity_id: input_number.boiler
    above: 95
actions:
  - type: turn_off
    device_id: aaa58f1913083bf4511ff7d6c9e3be45
    entity_id: c3d733b33d12171a17dba9c3032b0199
    domain: switch
  - type: turn_off
    device_id: d8a2b0881d9180cafa00aac0fefd8ea5
    entity_id: 3691be51a5c0117835d9253d2996e828
    domain: switch
  - type: turn_off
    device_id: 8cc9d8252cf4706b0c4f1a1849008cd7
    entity_id: 9e08bf64aae495ae5c5150ada1b7fc6f
    domain: switch
  - type: turn_on
    device_id: 1099fc1ff86152c551027ab99aefdcfc
    entity_id: 7140b1014bc935f8db260bbeeede2a40
    domain: switch
  - type: turn_off
    device_id: c41f692a25e6f5553d52d437c3d8f7e9
    entity_id: fb4d53e69a34ff386c3bb892e4e21c50
    domain: switch
mode: single

Here is a single automation that combines your four automations.

  • It has four Numeric State Triggers, each one monitors the same power sensor (sensor.your_power_sensor_1) but with unique thresholds.

  • Each Numeric State Trigger has a unique identifier (id). It will be used by the choose in the actions section.

  • There are two Numeric State Conditions, one monitors a power sensor (sensor.your_power_sensor_2) and the other an Input Number (input_number.boiler).

  • The actions section contains a choose that uses the trigger’s id to determine which actions it should perform.

  • The actions consist of turning on/off five switches. Which switches are turned off, and which are turned on, is governed by which Numeric State Trigger fired.

alias: Boven
description: ""
triggers:
  - id: '900W'
    trigger: numeric_state
    entity_id: sensor.your_power_sensor_1
    above: 900
    below: 1200
  - id: '1200W'
    trigger: numeric_state
    entity_id: sensor.your_power_sensor_1
    above: 1200
    below: 1650
  - id: '1650W'
    trigger: numeric_state
    entity_id: sensor.your_power_sensor_1
    above: 1650
    below: 2100
  - id: '1750W'
    trigger: numeric_state
    entity_id: sensor.your_power_sensor_1
    above: 1750
    below: 2200
conditions:
  - condition: numeric_state
    entity_id: sensor.your_power_sensor_2
    above: 0
  - condition: numeric_state
    entity_id: input_number.boiler
    below: 95
actions:
  - choose:
      - conditions: "{{ trigger.id == '900W' }}"
        sequence:
          - action: switch.turn_off
            target:
              entity_id:
                - switch.your_switch_3
                - switch.your_switch_4
                - switch.your_switch_5
          - action: switch.turn_on
            target:
              entity_id:
                - switch.your_switch_1
                - switch.your_switch_2
      - conditions: "{{ trigger.id == '1200W' }}"
        sequence:
          - action: switch.turn_off
            target:
              entity_id:
                - switch.your_switch_1
                - switch.your_switch_2
                - switch.your_switch_4
                - switch.your_switch_5
          - action: switch.turn_on
            target:
              entity_id:
                - switch.your_switch_3
      - conditions: "{{ trigger.id == '1650W' }}"
        sequence:
          - action: switch.turn_off
            target:
              entity_id:
                - switch.your_switch_1
                - switch.your_switch_4
                - switch.your_switch_5
          - action: switch.turn_on
            target:
              entity_id:
                - switch.your_switch_2
                - switch.your_switch_3
      - conditions: "{{ trigger.id == '1750W' }}"
        sequence:
          - action: switch.turn_off
            target:
              entity_id:
                - switch.your_switch_1
                - switch.your_switch_2
                - switch.your_switch_3
          - action: switch.turn_on
            target:
              entity_id:
                - switch.your_switch_4
                - switch.your_switch_5
    default: []
mode: single

NOTES

Your examples use Device Triggers/Conditions/Actions which identify entities using a long alphanumeric string (entity_id: 26fda7c22bea942358af1e19942fbd5a).

The example I posted uses traditional State Triggers/Conditions and actions which identify entities using a human-readable string (entity_id: switch.foo).

You will need to replace all of the fictitious entity_ids in my example (sensor.your_power_sensor_1) with the actual, human-readable entity_ids that are used in your system.


EDIT

Corrected syntax error.

Here is a more sophisticated, but far more concise, way to do the same thing. It does not require a choose statement.

Each trigger contains two trigger_variables, sw_off and sw_on, that list which switches should be turned off/on. The two actions use the trigger_variables to determine which switches to turn off/on.

alias: Boven
description: ""
triggers:
  - id: '900W'
    trigger: numeric_state
    entity_id: sensor.your_power_sensor_1
    above: 900
    below: 1200
    variables:
      sw_off:
        - switch.your_switch_3
        - switch.your_switch_4
        - switch.your_switch_5
      sw_on:
        - switch.your_switch_1
        - switch.your_switch_2
  - id: '1200W'
    trigger: numeric_state
    entity_id: sensor.your_power_sensor_1
    above: 1200
    below: 1650
    variables:
      sw_off:
        - switch.your_switch_1
        - switch.your_switch_2
        - switch.your_switch_4
        - switch.your_switch_5
      sw_on:
        - switch.your_switch_3
  - id: '1650W'
    trigger: numeric_state
    entity_id: sensor.your_power_sensor_1
    above: 1650
    below: 2100
    variables:
      sw_off:
        - switch.your_switch_1
        - switch.your_switch_4
        - switch.your_switch_5
      sw_on:
        - switch.your_switch_2
        - switch.your_switch_3
  - id: '1750W'
    trigger: numeric_state
    entity_id: sensor.your_power_sensor_1
    above: 1750
    below: 2200
    variables:
      sw_off:
        - switch.your_switch_1
        - switch.your_switch_2
        - switch.your_switch_3
      sw_on:
        - switch.your_switch_4
        - switch.your_switch_5
conditions:
  - condition: numeric_state
    entity_id: sensor.your_power_sensor_2
    above: 0
  - condition: numeric_state
    entity_id: input_number.boiler
    below: 95
actions:
  - action: switch.turn_off
    target:
      entity_id: "{{ sw_off }}"
  - action: switch.turn_on
    target:
      entity_id: "{{ sw_on }}"
mode: single

Thank you very much, I will in first instance rework my automations and come back with a new version. If you could then advice me on how to integrate the 30 second ‘inactivity’ before possibly retriggering the automation, I would be very gratefull.

I suggest you consider using the Numeric State Trigger’s for option.

In the following example, the trigger will fire when the sensor’s value increases above 900 and remains above 900 for at least 30 seconds. In other words, it doesn’t fire instantly when the value crosses the threshold of 900. It fires when the value has crossed the threshold and remained above it for at least 30 seconds. This prevents triggering if the value rises above the threshold but then quickly falls below it.

  - id: '900W'
    trigger: numeric_state
    entity_id: sensor.your_power_sensor_1
    above: 900
    below: 1200
    for:
      seconds: 30
1 Like

Great thank you

1 Like