Numeric state trigger: Possibility to use template as Above and Below

Would it be possible to add the option to use a template value as ‘above’ and ‘below’ in numeric value trigger if for whatever reason an absolute value isn’t suitable please?

I.e something like this:

automation:
  trigger:
    - platform: numeric_state
      entity_id: sensor.temperature
      # At least one of the following required
      above: "{{ states('input_number.low_threshold') }}"
      below: "{{ states('input_number.high_threshold') }}"
      for:
        hours: 1
        minutes: 10
        seconds: 5

Cheers!

The example you posted:

      above: "{{ states('input_number.low_threshold') }}"
      below: "{{ states('input_number.high_threshold') }}"

is already possible like this:

      above: input_number.low_threshold
      below: input_number.high_threshold

From the documentation:

Number helpers (input_number entities), number and sensor entities that contain a numeric value, can be used in the above and below thresholds, making the trigger more dynamic

1 Like

That’s… …embarrasing. :man_facepalming: Thanks @123 for pointing out what should have been obvious to me, especially since I spent quite some time reading that very documentation page.

1 Like

Just trying this personally, and looks like you have to create the numeric state trigger and then edit in YAML in order to use an input_number as an above/below value?

Wanted to ensure I’m not missing anything and possibly create a WTH to allow UI selection of these going forward.

Correct. The Automation Editor’s visual mode doesn’t implement everything that is possible.

Do you have an idea how to solve something very similar :
I need
above: sensor.different_sensor + input_number.offset

I want to compare two sensors plus an offset. Any idea ?

It may seem similar but the solution will be quite different.

A Numeric State Trigger’s above and below options don’t support templates so that rules out simply adding the offset directly in either option.

You have two choices:

  1. Create a new Template Sensor that performs the addition then use that new Template Sensor’s entity_id in above.

  2. Replace the Numeric State Trigger with a Template Trigger containing a template that performs the addition and numeric comparison.

I agree with @123’s comment… doing this in a Numeric State trigger is actually more complicated than just doing it in a Template trigger, so that is what I would suggest.

If, for some reason, you just want to use a Numeric State trigger, you need to move the components of the comparison around following the normal mathematical rules, with the goal of getting one side of the comparison to be a numeric value.

Is the state of Sensor A greater than (above) Sensor B plus an offset X:

A > (B + X)
A - (B + X) > 0

The numeric value (in this case 0) is used as the value for above and the left side of the comparison is converted into the value_template:

condition: numeric_state
entity_id: sensor.main
above: 0
value_template: |
  {% set offset_sensor = states('sensor.different_sensor') | float(0) +
  states('input_number.offset') | float(0) %}
  {{ state.state | float - offset_sensor }}
The Equivalent Template trigger, for comparison.
condition: template
value_template: |
  {% set offset_sensor = states('sensor.different_sensor') | float(0) +
  states('input_number.offset') | float(0) %}
  {{ states('sensor.main') | float(0) > offset_sensor }}
1 Like

Also worth noting that if you do reference another entity in above: or below:, changes to that entity won’t cause a trigger.