Automation: trigger on change of state based on time it was off

Hello everyone,

im trying to get this automation to work but it doesn’t trigger in anyway, can someone check?

I need to turn on a light when the p30_ground sensor changes from ‘off’ to ‘on’ but ONLY if the state of the sun is “below_horizon” and the p30_ground sensor was offline for at least ‘X’ minutes, i put 1 minute for tests.

In the condition test, both conditions pass but when I check the trace it shows me a “false” on the template condition, my guess its that when the template condition is tested the sensor its already on can someone help me?

alias: test BLE
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.p30_ground
    to: "on"
    from: "off"
condition:
  - condition: template
    value_template: |2-
        {% if is_state('binary_sensor.p30_ground', 'off') %}
              {% if as_timestamp(now()) - as_timestamp(states.binary_sensor.p30_ground.last_changed) > 60 %}
                {{ true }}
              {% else %}
                {{ false }}
              {% endif %}
            {% else %}
              {{ false }}
            {% endif %}
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - service: notify.mobile_app_mar_lx1b
    data:
      title: Test
      message: Test
      data:
        actions:
          - action: URI
            title: HomeAssistant
            uri: app://io.homeassistant.companion.android
mode: single
alias: test BLE
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.p30_ground
    to: "on"
    from: "off"
condition:
  - condition: template
    value_template: "{{ now() - states.binary_sensor.p30_ground.last_changed > timedelta(minutes=1) }}"
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - service: notify.mobile_app_mar_lx1b
    data:
      title: Test
      message: Test
      data:
        actions:
          - action: URI
            title: HomeAssistant
            uri: app://io.homeassistant.companion.android
mode: single

That’s the simplest way to compute the time difference.

However, the result will never be true. The time difference can’t be greater than 60 seconds because the last time the binary_sensor changed state was microseconds ago when it changed from on to off and triggered the automation.

hmmm yeah i tested it right now, it never passes on the condition check,

That Template Condition will never evaluate to true for the reason I provided above.

yup I was basically confirming what you said, any other suggestion for this case?

If I understood your requirement correctly, the desired condition is that the binary_sensor must have been off for at least 1 minute prior to the trigger.

The State Trigger is listening for the binary_sensor switching from on to off. When the trigger occurs, the binary_sensor’s previous state was on and now it’s off. How can it have ever been off for a minute if it’s previous state was on and it just turned off right now?

Yep you are right, meanwhile I found another solution that solved my problem. Since I have 1 esp32 running ble_presence in each floor, I could use it to detect if i come from the outside or already inside since when I am inside the beacon is always detected on more that 1 device.

Thanks for the help anyway!