Automation with timestamp condition

I am trying to build an automation where within a certain timeframe, I want the bathroom fan turned on with the bathroom light turning on and depending on how long the bathroom light stays on, I want two paths:

  • Path1: more than 10 seconds, turn the fan off 15 minutes after the light goes off.
  • Path2: (default): less than 10 seconds, turn the fan off when the light goes off.

I’ve tried building this automation using the GUI editor but I’m not getting desired behavior as automation always goes the default route, turning fan off immediately after light is off, regardless of the time the light was on. As shown in the screenshot below, time on was >10s but still went default route.

The value_template: "{{as_timestamp(now()) | int - as_timestamp(states.light.baie_light.last_changed) | int}} reports true when tested under developer tools so it doesn’t make sense to me. Is this a limitation of the visual editor?

My automation has an additional option which works fine but concerning failing option starts at line 32:

alias: Fan Automation 
description: ''
trigger:
  - platform: device
    type: turned_on
    device_id: 781ee485eb41ce9a494fc8a3ceb8a17e
    entity_id: light.baie_light
    domain: light
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            after: '22:00:00'
            before: '07:00:00'
        sequence:
          - type: turn_on
            device_id: 781ee485eb41ce9a494fc8a3ceb8a17e
            entity_id: light.baie_fan
            domain: light
          - wait_for_trigger:
              - platform: device
                type: turned_off
                device_id: 781ee485eb41ce9a494fc8a3ceb8a17e
                entity_id: light.baie_light
                domain: light
            continue_on_timeout: false
          - type: turn_off
            device_id: 781ee485eb41ce9a494fc8a3ceb8a17e
            entity_id: light.baie_fan
            domain: light
      - conditions:
          - condition: time
            after: '07:00:00'
            before: '22:00:00'
        sequence:
          - type: turn_on
            device_id: 781ee485eb41ce9a494fc8a3ceb8a17e
            entity_id: light.baie_fan
            domain: light
          - wait_for_trigger:
              - platform: device
                type: turned_off
                device_id: 781ee485eb41ce9a494fc8a3ceb8a17e
                entity_id: light.baie_light
                domain: light
            continue_on_timeout: false
          - choose:
              - conditions:
                  - condition: template
                    value_template: >-
                      {{as_timestamp(now()) | int -
                      as_timestamp(states.light.baie_light.last_changed) | int >
                      10}}
                sequence:
                  - delay:
                      hours: 0
                      minutes: 15
                      seconds: 0
                      milliseconds: 0
                  - type: turn_off
                    device_id: 781ee485eb41ce9a494fc8a3ceb8a17e
                    entity_id: light.baie_fan
                    domain: light
            default:
              - type: turn_off
                device_id: 781ee485eb41ce9a494fc8a3ceb8a17e
                entity_id: light.baie_fan
                domain: light
    default: []
mode: restart

The issue is that by the time you are checking the conditions the state has already changed to “off”. And that means that the “last_changed” is always going to be to the “off” state and will always be less than 10 seconds so those actions will never happen.

the only way I can think to do it would be to capture the date/time that the switch went to “on” in an input_datetime and then use the current time difference to that input_datetime to determine your choose actions.

Based on your initial requirements could you not do something like the following:

Turn fan on
Wait for trigger of light turning off with a wait of 10 secs but continue on time out
Then have a choose with option 1 being state of light off = turn off fan And option 2 being state of light on with a wait for trigger of light turning off and then delay 15 mins then turn off fan.

Sorry for garbled message, I would type out an example but on iPhone.

1 Like

That does the job nicely! Thank you!