Automation not triggering on sensor depth change

Trying to define a simple automation that is to filter a oil monitor sensor depth change. When it’s just after switching to a new measurement which is at the cm granularity, change in temperature over the next few days it can flip flop between 2 measurements making creating a utility sensor from the measurement for how much oil is used add each change.

Using a simple variable with memory integration and trying to get the automation trigger to only increase the variable if it is greater than it. But it seems the trigger never fires.

I can trigger the automation manually and it does the right thing, but just can’t see why it won’t trigger itself. Any suggestions on where to look.

I had assumed that I need to prevent unknown or unavailable states from being read as previously this was set for any depth change and the subsequent template would work out if the change was greater than 3cm and allow that to recognise the tank being filled.

alias: Oil Tank Monitor
description: ""
trigger:
  - type: value
    platform: device
    device_id: e9952ab127e3babece4e594574051267
    entity_id: 8514b4717d22992e44c0ed646529f377
    domain: sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
    below: 2
    id: from
    above: 0.5
condition:
  - condition: not
    conditions:
      - condition: or
        conditions:
          - condition: state
            state: unavailable
            entity_id: sensor.oil_sonicsmart_145565457_d
          - condition: state
            state: unknown
            entity_id: sensor.oil_sonicsmart_145565457_d
action:
  - service: variable.update_sensor
    data:
      replace_attributes: false
      value: >
        {% set sensor_value = states('sensor.oil_sonicsmart_145565457_d') |
        float(0) %}
        {% set var_value = states('sensor.sonic_watchman_depth') | float(0) %}
        {% if sensor_value > var_value %}
        {# oil in tank is lower that previous, take the sensor value #}
        {{   sensor_value }}
        {% elif (sensor_value - var_value ) <= -3.0 %}
        {# oil tank refilled so sensor is more than 3cm than previous measured
        (avoid bouncing) #}
        {{   sensor_value }}
        {% else %}
        {# keep existing value as oil level is near a switch point #}
        {{   var_value }}
        {% endif %}
    target:
      entity_id: sensor.sonic_watchman_depth
mode: queued
max: 10

May switch to using multiple automation and less template if I can figure out how to get it to trigger when the depth changes. Can see a recent change in the sensor depth in the image below.

I can see that the automation indicates it hasn’t run and there is no traces available.

But if I run the automation manually, can see the variable with memory is updated with the new value 25cm.

Question is why is the trigger not firing and how to debug?

From the numeric state trigger docs: the trigger only fires when the state crosses the threshold

If you want to trigger on every state change, you just need an entity state trigger.

Ah, thanks, I assumed the above/below referred to the amount of change based on the what the trigger description

Didn’t think it meant current value. That will still be useful for some things, but this helps a lot. Thanks.

If you’re looking for a “delta” type trigger (new_value - old_value > some_number) then you’d have to do that with a state trigger and a template condition:

{{ trigger.to_state.state | float(0) - trigger.from_state.state | float(0) > 0.5 }}

I’d also recommend avoiding device triggers entirely; here’s a link to a good post showing why.