Trigger automation on value increase

I want to run a script when I get new mail. So I want it to run when my unread counter goes from 0 to 1, or from 1 to 2. But not when I read a mail and it goes back from 2 to 1.

However, I cannot figure out how to get the previous value to compare against. This is my current (partial) attempt:

  - alias: "New mail"
    trigger:
      platform: template
      value_template: '{{ states.sensor.emailcount.state | int > old_count??? }}'
1 Like

Use the Trend Binary Sensor (https://home-assistant.io/components/binary_sensor.trend/)

binary_sensor:
  - platform: trend
    sensors:
      mailcount_increase:
        entity_id: sensor.emailcount

automation:
  - alias: "New Mail"
    trigger:
      platform: state
      entity_id: binary_sensor.mailcount_increase
      to: 'on'
1 Like

Thanks, I will admit to having missed that sensor completely.

It should trigger each time I get an email. So I moved the trend into a condition, and now it seems to work the way I want:

binary_sensor:
  - platform: trend
    sensors:
      mailcount_increase:
        entity_id: sensor.emailcount

automation:
  - alias: "New mail"
    trigger:
      platform: state
      entity_id: binary_sensor.mailcount_increase
    condition:
      - condition: state
        entity_id: binary_sensor.mailcount_increase
        state: 'on'
    action:
      ...

(updated for current trend sensor, June 2019)

2 Likes

thanks worked a treat!

Anyone coming across should also probably look at the option here:

an approach using an automation condition comparing trigger.to_state.state with the triggering entities previous state trigger.from_state.state (thus avoiding the need for an extra binary sensor).

4 Likes

Thanks, that is probably a better solution and actually answers my original question :slight_smile: