Only run second action if the first action was triggered in automation

I have an automation that has 2 triggers… that fire when the entity is “on” and “off”.

How can I set it so that the off action only runs if the on action has been triggered… say within the last 7 hrs?

alias: Keep Charged via Intelligent Dispatch
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.octopus_intelligent_dispatching
    to: "on"
    id: "on"
  - trigger: state
    entity_id:
      - binary_sensor.octopus_intelligent_dispatching
    to: "off"
    id: "Off"
conditions: []
actions:
  - alias: ON outside of normal
    choose:
      - conditions:
          - condition: trigger
            id:
              - "on"
          - condition: time
            after: "05:30:00"
            before: "23:30:00"
        sequence:
          - delay:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - action: select.select_option
            metadata: {}
            data:
              option: Keep Charged
            target:
              entity_id: select.select_battery_mode
          - action: notify.mobile_app
            metadata: {}
            data:
              message: Keep Charged set to ON - via Intelligent Dispatch
  - alias: "OFF "
    choose:
      - conditions:
          - condition: trigger
            id:
              - "Off"
        sequence:
          - action: notify.mobile_app
            metadata: {}
            data:
              message: Keep Charged set to OFF - as outside off peak times
          - action: select.select_option
            metadata: {}
            data:
              option: Self - BatteryLife
            target:
              entity_id: select.select_battery_mode
mode: single

Add a Template Condition that checks if the time of the previous state-change was less than 7 hours ago.

- alias: "OFF "
  choose:
    - conditions:
        - condition: trigger
          id: - "Off"
        - condition: template 
          value_template: >
            {{ now() - trigger.from_state.last_changed < timedelta(hours=7) }}

I need it to only turn off it was specifically turned on by this automation… not if it was turned on by any other means?

Was that requirement mentioned in your first post?

What else wasn’t mentioned?

by on action I mean the on action from the automation…

actions:
  - alias: ON outside of normal

The example I posted simply shows where to put the Template Condition in your existing automation.

It gets processed only when the automation is triggered by its second State Trigger (i.e. the one detecting the binary_sensor’s state-change to off).

If the binary_sensor’s previous state-change (which would have been to on) occured less than 7 hours ago, the actions to send a notification and set the Select entity are executed.

If that’s not what you want then I have misunderstood your stated requirements.

Please correct me if I am wrong…

If I add this template condition to the ‘OFF’ action then it will turn off, as long as it was turned on within the last 7 hrs.

However, I need it to be more specific than that…

The intelligent dispatch entity triggers every day at 2330 on, and off at 0530. I have an entire set of schedules configured on an inverter to handle this daily schedule fine, which doesn’t involved HA. I’ve used HA for 3+ years now, and occasionally for what ever reason my battery would not charge as the mode change did not make it to the inverter using node red. This way is more reliable…

However, the intelligent dispatch object may turn on outside these hours, due to various edge cases, and octopus deciding to charge my car. As it stands unless I change the status of the battery to ‘Keep Charged’ my battery will then charge the car… defeating the whole point. Hence the condition on the ‘ON’ trigger to define what is outside the normal charging window…

          - condition: time
            after: "05:30:00"
            before: "23:30:00"

So what I would like is for the OFF action to fire, only if the conditions were met within the last 7 hrs for the ‘ON’ trigger… not the intelligent dispatch entity. This is needed as at the end of the intelligent window I need to change it back to 'Self - BatteryLife` otherwise the inverter schedule keeps it on the ‘keep charged’ mode as that is what is was when it began its schedule…

Hope that makes more sense?

If I understood your additional requirement correctly, add a third condition that checks if the state of the Select entity is Keep charged (because only the automation is permitted to set it within the allowable time period).

  1. First condition confirms trigger.id is Off.

  2. Second condition confirms the previous state-change occured with the last 7 hours.

  3. Third condition confirms battery mode is Keep charged.

If that doesn’t achieve your goal then, unfortunately, I didn’t understand your explanation.

That ok, I appreciate the help. However, I only want to run the second action if it was the automation that has triggered it in the first place as per the title of the post.

Sure, I can add lots of conditions to the OFF based on the states of other entities but now I’m having to explicitly define all these conditions and edge cases.

I’m going to go for a different path, which is to remove the off, and have it defined as a sequence from the on action… see how that works.

The danger of this approach is that it won’t survive reboots of HA

That’s exactly what is accomplished by what I had suggested in my previous post.

Describe a scenario where, using those three conditions, the second actions can be executed if it was NOT the automation that had originally executed the first actions.

Which is the main purpose of conditions. After the automation is triggered, conditions ensure actions are executed only for desired circumstances.

Correct. The time spent idling by a delay, wait_template, or wait_for_trigger represents a window of opportunity to be terminated by a restart.

The best practice is to minimize this “window” and that’s usually done by restructuring the automation to trigger on multiple events.

Just an idea: Can we do two automations, one for each trigger. And then at the start of the sequence of actions of the automation #1, you turn off automation #2, and at the end of the same sequence you turn automation #2 back on…?

That way, the automation #2 would never be able to run during the sequence of actions of automation #1, even if/when trigger #2 being triggered.

Less elegant and ugly 2 separate automations for the same thing… definitely rooms to optimize… but easier to understand and maintain for my simple brain. :slight_smile:

That’s not a recommended design pattern plus the problem is likely to be solved with what has already been suggested (however it’s being pre-emptively dismissed without any report that it actually failed during testing).