Automation switch on if a value falls below or if value rises again

Hi,

I can not get on with a automatisin and ask for help.
I have hass.io running on a synology nas, version 0.192.

In the configuraion.yaml the automations.yaml is called:

.. 
 # Automation
 automation: !include automations.yaml
..

The following automation works good, but I would like to have the two in one - how do you do that?

..
..
- id: '1572282030760'
  alias: moisture-detection-on
  trigger:
  - below: '650'
    entity_id: sensor.wemosd1_14_feuchte
    platform: numeric_state
  action:
  - service: homeassistant.turn_on
    entity_id: switch.wemosd1_13_swi

- id: '1572282030761'
  alias: moisture-detection-off
  trigger:
  - above: '500'
    entity_id: sensor.wemosd1_14_feuchte
    platform: numeric_state
  action:
  - service: homeassistant.turn_off
    entity_id: switch.wemosd1_13_swi
..
..

You could move the second trigger to the first automation and use a service template to decide which service to call. So you’d want something like this:

- id: '1572282030760'
  alias: moisture-detection-on-off
  trigger:
  - below: '650'
    entity_id: sensor.wemosd1_14_feuchte
    platform: numeric_state
  - above: '500'
    entity_id: sensor.wemosd1_14_feuchte
    platform: numeric_state
  action:
  - service_template: >
      {% if states('sensor.wemosd1_14_feuchte') | float < 650 %}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off
      {% endif %}
    entity_id: switch.wemosd1_13_swi

Edit: Nope, wouldn’t work. I was thinking above 650, not below. The template would evaluate as true if either trigger happens :man_facepalming:

1 Like

You’d be better setting up a binary switch that had this hysteresis set and then run from that.
Edit: OR you could just use Tediore’s solution above :smiley:

Close. For a numeric state trigger you can look at the trigger variable’s above and below values. If an above trigger fired, then above will have a value and below will be None, and vice versa. So:

- id: '1572282030760'
  alias: moisture-detection-on-off
  trigger:
  - below: '650'
    entity_id: sensor.wemosd1_14_feuchte
    platform: numeric_state
  - above: '500'
    entity_id: sensor.wemosd1_14_feuchte
    platform: numeric_state
  action:
  - service_template: >
      {% if trigger.below %}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off
      {% endif %}
    entity_id: switch.wemosd1_13_swi
1 Like

Ah awesome, thank you.

Thanks for the help :beers:

My solution:

- id: '1572282030760'
  alias: moisture-detection-on-off
  trigger:
  - below: '600'
    entity_id: sensor.wemosd1_14_feuchte
    platform: numeric_state
  - above: '600'
    entity_id: sensor.wemosd1_14_feuchte
    platform: numeric_state
  action:
  - service_template: >
      {% if trigger.below %}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off
      {% endif %}
    entity_id: switch.wemosd1_13_swi

Changed the values below and above, because i believe that the first values are not logical.

In addition, I’ve even created a binary_sensor to play with different ways of evaluation and automation:

  - platform: mqtt
    name: moisture_detection
    state_topic: "tele/wemosd1_14/SENSOR"
    value_template:
      "{% if value_json['ANALOG'].A0 | float < 600 %}
        ON
      {% else %}
        OFF
      {% endif %}"
    device_class: moisture

Postscript:
The sensor provides a dry value of about 730 and about 300 dipped in water. In potting soil (normally moist) it delivers about 600.
Thought is the whole as a rain sensor to close possibly open roof window - with the current limit value 600, I’ll probably still have to play