Trigger to an expression instead of an entity

I have the following automation but wish to replace binary_sensor.fubar with an expression, such as

{{ states('sensor.foo')|float > states('input_number.bar')|float }}

I guess what I’m asking is, how can I specify an expression in an automation as a trigger (and adhering to the “for” clause)?

- alias: Fubar
  trigger:
    - platform: state
      entity_id: binary_sensor.fubar
      to: 'on'
      for:
        seconds: 3
  action:
    - service: switch.turn_on
      entity_id: switch.fubar

There’s no reason you can’t use a template trigger.

If your trigger is a relatively simple mathematical comparison you can use the Numeric state trigger.

- alias: Fubar
  trigger:
    - platform: numeric_state
      entity_id: sensor.foo
      above: input_number.bar
      for:
        seconds: 3
  action:
    - service: switch.turn_on
      entity_id: switch.fubar

For a more complex comparison where you need to use a “for” clause, the best thing to do is to set up a Template binary sensor.

## configuration.yaml
template:
  - binary_sensor:
      - name: "Fubar"
        state: >
          {{ states('sensor.foo') | float(0) > states('input_number.bar') | float(0) }}

Once that is set up you can use it in a state trigger exactly like your example.