#How do I use the previous state of an entity in the conditions?

1 messages · Page 1 of 1 (latest)

sand cloud
#

I would like to run some automations triggered by change on my 3D printer. For the things I would like to run, I want it to trigger when some entity's value changes from a value greater than 0 to exactly 0. It should also filter out when the previous state was unavailable. how do I set the automation trigger?

tawdry plaza
#

You should use the numeric_state trigger with below: 1 option, to trigger the transition from above 0 to 0.

#

Then, you should add the template condition to filter out that previous state was also a number.

#
triggers:
  - trigger: numeric_state
    entity_id: sensor.your_sensor_to_trigger_on
    below: 5
conditions:
  # Pick one of these 2 options
  - condition: template
    value_template: "{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}"
  - condition: template
    value_template: "{{ trigger.from_state.state | is_number }}"
#

When automation triggers, there is a trigger variable available for you, which holds the previous and new state. So you can use that.

fringe minnow
#

You’d also need a condition to make sure the state isn’t less than zero.

#

I’d recommend a state trigger if it’s known what state “exactly zero” is. For example 0.0 and 0 are different possibilities.

If it’s not know what the state will be, or if multiple options need to be handled, a template trigger can be used. This would trigger when the state is exactly zero: {{ states('sensor.some_sensor') | float (-1) == 0 }}

Then just use a condition to check the previous state was greater than zero:
{{ trigger.from_state.state | float(-1) > 0 }}