Automation help : condition to check if state hasn't changed

Greetings,

I have this automation code:

alias: Relay1S ON Fail Alert
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_boolean.head_office_presence_indicator
    to: "on"
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.relay1s
            state: "OFF"
            for:
              hours: 0
              minutes: 1
              seconds: 0
        sequence:
          - action: notify.gmail
            metadata: {}
            data:
              message: Relay 1S ON Fail
              title: Relay1S ON Fail
mode: single

I want an email alert if sensor.relay1s is OFF a minute after input_boolean.head_office_presence_indicator turns on. I’m not sure if the above code would accomplish this - I’m guessing the action will trigger only when sensor.relay1s CHANGES TO OFF and STAYS OFF for a min. This is not what I’m looking for - I want to be notified if it’s OFF after 1 min, regardless of it changing to OFF. Here’s an another attempt to accomplish the same:

actions:
  - sequence:
      - delay:
          hours: 0
          minutes: 1
          seconds: 0
          milliseconds: 0
      - choose:
          - conditions:
              - condition: state
                entity_id: sensor.relay1s
                state: "OFF"
            sequence:
              - action: notify.gmail
                metadata: {}
                data:
                  message: Relay1S ON Fail
                  title: Relay1S ON Fail
mode: single

The 2nd code waits for a min. and then checks to see if sensor.relay1S is OFF - should the sensor change to OFF again in this case? Will the action fire if it was OFF prior and hasn’t changed since?

Hope this makes sense. I’m not sure which version of the code to use for this and if any changes need to be made.

Thanks.

In the first example, the actions will only be executed if the sensor’s state has been “OFF” for at least the past 1 minute. No state change is required. Conditions do not wait, they are a snapshot of the current state.

Since the condition doesn’t have a for duration defined, all that matters is that it is “OFF” at the exact moment the delay finishes… the condition doesn’t care if it has been that way for 1 millisecond or two days.

Between the title of the thread and your description, it’s not completely clear what you are looking for.

Wait 60 sec. and check that the sensor is off right now
alias: Relay1S ON Fail Alert
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_boolean.head_office_presence_indicator
    to: "on"
conditions: []
actions:
  - delay: 60
  - condition: state
    entity_id: sensor.relay1s
    state: "OFF"
  - action: notify.gmail
    metadata: {}
    data:
      message: Relay1S ON Fail
      title: Relay1S ON Fail
mode: single
Wait 60 sec. and check that the sensor has been off for the entire time
alias: Relay1S ON Fail Alert
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_boolean.head_office_presence_indicator
    to: "on"
conditions: []
actions:
  - delay: 60
  - condition: state
    entity_id: sensor.relay1s
    state: "OFF"
    for: 
      minutes: 1
  - action: notify.gmail
    metadata: {}
    data:
      message: Relay1S ON Fail
      title: Relay1S ON Fail
mode: single
1 Like

Thank you - that answers it. Sometimes it’s so hard describing these things, especially for someone like me who doesn’t have a programming background. Apologies for any confusion with the title - it’s the first thing; wait 60 sec and check that the sensor is off right now. The second version of the code seems to do just that