Trigger by history sensor value

Hi

I’m using Mi Flora sensor for the flowers and I’m looking for a way to trigger the automation if the flowers weren’t watered more than two days.

The graph/data is as follows:

Probably I need to configure the template trigger with some loop that checking for the last 2 days sensor data.
Any ideas how it can be done?

Thanks!

Any reason you don’t just trigger it based on the value of the ‘Flower Moisture’ sensor directly?

It’s done like this now.
It’s not always accurate and can show bit different value, so I prefer to water the plants every X days…

Just asking because I set up an (overly?!) complicated set of automations based on time and actual precipitation because I didn’t want to go for a humidity sensor in the ground (for cost and reliability reasons). Your skepticism seems to confirm my approach :wink:

It resulted in this, but the convoluted way I’ve coded it makes it un-shareable at the moment :frowning:

The drip system turns on every 2 days (can be changed) at 5:55am but only if the precipitation has been too low (in the current setup the threshold is 100U).
You can see that it was scheduled to run on 4/7, i.e. 2 days after it ran last time, but the threshold hasn’t been reached since then yet.

What I would recomment - without having it coded that way - would be to:

  1. Set up a datetime variable that stores the value when the system was run last.
#Test for storing last runtime for the sprinkler
input_datetime:
  sprinkler_last_runtime: 
    name: Sprinkler Stored Runtime 
    has_date: true
    has_time: true
  1. Build an automation that runs at a specific time each day and checks if the value for the _sprinkler_last_runtime is older than e.g. 47h59m. (mine has the additional 100U-condition built in).
######################################################################
# Check Sprinkler Conditions at 05:55h each day
######################################################################
- alias: Check Sprinkler Conditions at 05:55h each day
  trigger:
    platform: time
    hours: 05
    minutes: 55
    seconds: 00
  condition:
    condition: and 
    conditions:
      - condition: template
        value_template: >-
          {% set next_runval = states('sensor.sprinkler_next_runval') | float %}
          {% set now_val = as_timestamp(now()) | float %}
          {{ now_val > next_runval }}
      - condition: template
        value_template: '{{ (states.sensor.preciptotal4sprinkler.state | int) < 100 }}'
  action:
    - service: notify.mypushbullet
      data_template:
        title: "Home Assistant says:"
        message: 'Time to water!'
    - service: homeassistant.turn_on
      entity_id: input_boolean.time_to_water
    - service: homeassistant.turn_on
      entity_id: input_boolean.sprinkler
  1. As part of the last automation that actually runs the drip system, I also store the time it was last run in the datetime variable.
- alias: sprinkler run time
  trigger:
    platform: state
    entity_id: input_boolean.sprinkler
    from: 'off'
    to: 'on'
  action:
#    - service: shell_command.save_sprinkler_last_runtime
    - service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.sprinkler_last_runtime
        time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
        date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'

I was told that some people have a trigger that works on the principle of now() - 48h, but I couldn’t get that to work so I made it a condition instead:

https://community.home-assistant.io/t/change-an-input-boolean-once-a-certain-point-in-time-has-come/48275

Thanks for the detailed reply!

I think for me actually will be enough just storing the last watering date (it’s pretty easy to detect) and the other automation as you mentioned, I will base on the this date and moisture sensor value as well.

1 Like