#Trigger when sensors increases

1 messages · Page 1 of 1 (latest)

keen adder
#

If I have a template sensor that returns count of some things and I want to trigger when values are increasing, what's the best way to do it?

  • Create derivative sensor and trigger when value goes positive? Such sensor would not fire again if template goes from 0 to 1 and in one minute from 1 to 2, because delta is always the same, or would it?
  • Trigger on any state change and use condition with from_state and to_state to compare both are valid and delta to_state - from_state > 0?

Or is there any other more universal way?

Edit: Option #2.

My template sensor that holds number of update. entities with state set to on:

- sensor:
    - unique_id: 285e2fde-4277-4c26-8746-07ebd059b7c2
      name: "Number of updates available"
      state: >
        {{ 
          states.update
            | selectattr('state', 'eq', 'on') 
            | map(attribute='entity_id')
            | list
            | count
          }}
      state_class: total

And then automation that fires on every state change but only advances if the new state is greater than previous one. Invalid states are set to 0 and thus it still works properly.

triggers:
  - trigger: state
    entity_id: sensor.number_of_updates_available
conditions:
  - condition: template
    value_template: |
      {{ trigger.to_state.state | int(0) > trigger.from_state.state | int(0) }}
vital pine
#

I use the second option