Use a History value with the current value to trigger a input_boolean

Hi, I’ve got a question.
I’m searching and trying fore days to solve or start with it. But without a good solution or way to start.

I’ve got a sensor.rain_sensor, for the best result to use it I want to make a automation what I try to describe below. Note: the value is update each seconde. And the value start with 0 till 1024
I want to compare the current value from the sensor.rain_sensor with the value from five second ago (or with the last know valeau ago).
Went the current valeau is 5 lower then the current, then the automation need to switch the input_boolean.regen on.

Hopefully can anyone helps me with it. Or have a good tip where I can’t start with.

Mine other ideas with this kind of automation can I than make on my own.

things to know:
System Health

version: core-2021.5.4
installation_type: Home Assistant OS
dev: false
hassio: true
docker: true
virtualenv: false
python_version: 3.8.9
os_name: Linux
os_version: 5.4.83-v8
arch: aarch64

host_os: Home Assistant OS 5.13
update_channel: stable
supervisor_version: supervisor-2021.04.3
docker_version: 19.03.15
disk_total: 13.9 GB
disk_used: 7.8 GB
healthy: true
supported: true
board: rpi4-64
supervisor_api: ok
version_api: ok

The State Trigger permits you to compare the entity’s current state value with its previous state value. For example, this automation’s condition checks if the new value is greater than the previous value.

alias: example
trigger:
- platform: state
  entity_id: sensor.my_sensor
condition:
- condition: template
  value_template: "{{ trigger.to_state.state | float > trigger.from_state.state | float }}"
action:
... etc ..

If you want an entity’s state value from 5 minutes ago (or some other amount of time), that information is stored in Home Assistant’s database. The only integration I can think of that allows you to query the database is SQL Sensor.

Here is an idea to get you started.

I have a system set up to calculate the estimated time that the food I’m cooking in my smoker will be done. For that I need to calculate the rate of change of the food temperature and it’s “averaged” over 5 minutes (not exactly but close enough) by taking a temp reading every 5 minutes and storing the previous 5 minute reading in a variable attribute then comparing the current food temp to that 5 minute old reading. Then I divide by 5 to get the “degrees per minute” rate of change.

to do this you could change the trigger time to every 5 seconds and save the current value as the previous value. Then if positive (>0) turn on the boolean. if 0 or negative (<=0) then either turn off the boolean or do nothing.

I use the custom “hass-variables” integration (available in HACS - if you go that route make sure you get the “hass-variables” integration, tho. there is another variables integration that doesn’t support attributes).

Or you could do the same using input_number to store the previous reading.

here is the automation to set the variable current and history values:

  - alias: Calc Rate of Change Smoker Food Temp
    trigger:
      - platform: time_pattern
        seconds: "/5"
    action:
      - service: variable.set_variable
        data:
          variable: smoker_food_temp_history
          attributes: 
            history_01: "{{ states('variable.smoker_food_temp_history') | float }}"
      - service: variable.set_variable
        data:
          variable: smoker_food_temp_history
          value: "{{ states('sensor.smoker_food_temperature') | float}}"

then you can add the following to the action section to turn on the boolean if the value is positive:

- condition: "{{ (((states('variable.smoker_food_temp_history') | float - state_attr('variable.smoker_food_temp_history', 'history_01') | float) / 5)) > 0 }}"
- service: input_boolean.turn_on
  entity_id: input_boolean.your_boolean

Or since you only care about the absolute value difference (>5 between readings) then it can be changed to this:

- condition: "{{ ((states('variable.smoker_food_temp_history') | float - state_attr('variable.smoker_food_temp_history', 'history_01') | float)) > 5 }}"
- service: input_boolean.turn_on
  entity_id: input_boolean.your_boolean

so the whole thing together (using condition version 2 above) would be:

  - alias: Calc Rate of Change Smoker Food Temp
    trigger:
      - platform: time_pattern
        seconds: "/5"
    action:
      - service: variable.set_variable
        data:
          variable: smoker_food_temp_history
          attributes: 
            history_01: "{{ states('variable.smoker_food_temp_history') | float }}"
      - service: variable.set_variable
        data:
          variable: smoker_food_temp_history
          value: "{{ states('sensor.smoker_food_temperature') | float}}"
      - condition: "{{ ((states('variable.smoker_food_temp_history') | float - state_attr('variable.smoker_food_temp_history', 'history_01') | float)) > 5 }}"
      - service: input_boolean.turn_on
        entity_id: input_boolean.your_boolean
1 Like

Thank you all. You guys help me to starting up with it. I don’t get it worked so you describe it but It works.
For te people who’ve intrested.

automation on:

alias: Regenschakelaar aan
description: ''
trigger:
  - platform: time_pattern
    seconds: /5
condition:
  - condition: template
    value_template: >-
      {{ (((states('sensor.rain_sensor') | float -
      states('sensor.regen_history') | float) / -2)) > 0 }}
action:
  - scene: scene.regenschakelaar_aan
mode: single

Automation off

alias: Regenschakelaar uit
description: ''
trigger:
  - platform: time_pattern
    seconds: /5
condition:
  - condition: state
    entity_id: input_boolean.regen
    state: 'on'
  - condition: template
    value_template: >-
      {{ (((states('sensor.rain_sensor') | float -
      states('sensor.regen_history') | float) / 5)) > 0 }}
action:
  - scene: scene.regenschakelaar_uit
mode: single

extra configuration setting:

#sensor om de huidige waarde mee te vergelijken    
  - platform: sql
    queries:
      - name: regen history
        query: "SELECT * FROM states WHERE entity_id = 'sensor.rain_sensor' ORDER BY state_id DESC LIMIT 1;" 
        column: "state"

I had a similar problem and solved it with a trend-helper.

As soon, as it starts raining, your values increase, so the trend is positive ( > =0 ). When rain stops, your trend would fall ( → negative values ). After some time, it should be stable at 0.

With a trend, you can then decide, how fast you would react on rainfall.