Automation trigger if numeric state falls below a list of values?

I’ve got an automation to tell me to water my plants if the zigbee moisture sensors detect them drying out. The automation sends me a Telegram message.

To avoid multiple triggers at, say, <90%, <80% and <70% i wondered if I could set the below: to a list of values. I asked the question in Goole and this is what its AI thing said:-

Yes, you can configure Home Assistant automations to trigger when a value falls below a list of values using the numeric_state trigger and the below keyword with multiple values.

Here’s how you can achieve this:

  • Trigger: Use the numeric_state trigger platform and specify the entity_id of the sensor you want to monitor.
  • Below: Use the below keyword and list the values you want to trigger on. For instance, below: [10, 20, 30] will trigger if the value drops below 10, 20, or 30.
  • Actions: Define the actions you want to perform when the trigger condition is met.

Example Automation YAML:

alias: "Trigger on Value Below List"
trigger:
  - platform: numeric_state
    entity_id: sensor.my_sensor
    below: [10, 20, 30]
action:  - service: notify.my_notification
    data:      message: "Value below threshold!"

It doesn’t work!

My code:

trigger: numeric_state
entity_id:
  - sensor.moisture_3_soil_moisture
for:
  hours: 0
  minutes: 5
  seconds: 0
id: anthurium
below: [ 90, 85, 80, 75, 70, 65, 60 ]

Also tried:

trigger:
  - platform: numeric_state
    entity_id: sensor.moisture_3_soil_moisture
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: anthurium
    below: [ 90, 85, 80, 75, 70, 65, 60 ]

Am I missing something, other than AI is not all it’s cacked up to be?

Any other way I can achieve this to save me having to write 42 triggers?

You are not showing the whole automation, so your action is a mystery. Why do you care if the sensor.moisture_3_soil_moisture is below 90 or below 60? No matter what sensor.moisture_3_soil_moisture is, if it’s below 90, it triggers the action.

If the sensor is below 90, send a telegram with the sensor value.

There’s nothing in the documentation indicating the value of above and below can be anything other than a single number

That’s why using a list of numbers doesn’t work.

You’ll need to make separate triggers. If you want different actions depending on which trigger was triggered, use choose.

triggers:
  - trigger: numeric_state
    entity_id: sensor.moisture_3_soil_moisture
    for:
      minutes: 5
    id: '90'
    below: 90
  - trigger: numeric_state
    entity_id: sensor.moisture_3_soil_moisture
    for:
      minutes: 5
    id: '85'
    below: 85
conditions: []
actions:
  - choose:
      - conditions: "{{ trigger.id == '90' }}"
        sequence:
           ... etc ...
      - conditions: "{{ trigger.id == '85' }}"
        sequence:
           ... etc ...

The action is not the problem, but here is the full automation that does work (although I’ve not included the other 5 plants to keep it short)…

alias: Plants Watering
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.moisture_3_soil_moisture
    below: 90
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: anthurium
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - anthurium
        sequence:
          - action: telegram_bot.send_message
            metadata: {}
            data:
              message: >-
                The Anthurium in the Dining Room needs watering. Moisture level
                is reported as {{trigger.to_state.state}}%
              title: Water Alert

I want it to do multiple triggers on each plant as I forget and need badgering!

When I try the above 2 examples, HA throws a wobbly saying “Error” or “Unknown trigger” and refuses to save.

I’d also like to able to cut the 6 chosen actions down to 1 by replacing the plant name and room with templates. The plant name I think I can do with {{trigger.id}} but I’ve not worked out how to get the Area.

Yes, I’ve been looking. You cannot trust AI.

alias: Plants Watering
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.moisture_3_soil_moisture
    below: 90
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - trigger: numeric_state
    entity_id:
      - sensor.moisture_3_soil_moisture
    below: 85
    for:
      hours: 0
      minutes: 5
      seconds: 0
actions:
  - action: telegram_bot.send_message
    metadata: {}
    data:
      message: >-
        The Anthurium in the Dining Room needs watering. Moisture level is reported as {{trigger.below}}%
      title: Water Alert
1 Like

If you don’t need full resolution, you could use some, admittedly kind of silly, math. Basically, round the value to the nearest multiple of 5 then trigger when that value changes from even (multiple of ten) to odd…

alias: Plants Watering
description: ""
triggers:
  - id: anthurium
    trigger: template
    value_template: >-
      {% set s_state = states('sensor.moisture_3_soil_moisture') | round(0, default=50) | int %}
      {{ ((s_state //5)*5) is odd and 60 < s_state < 90 }}
    for: "00:01:00"
conditions:
  - alias: Check that value is decreasing
    condition: template
    value_template: "{{ trigger.to_state.state|float < trigger.from_state.state|float }}"
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - anthurium
        sequence:
          - action: telegram_bot.send_message
            metadata: {}
            data:
              message: >-
                The Anthurium in the Dining Room needs watering. Moisture level
                is reported as {{trigger.to_state.state}}%
              title: Water Alert
3 Likes

I see how that would trigger ok but I’d lose the trigger.to_state.state in the message.

I’m sort of resigned to having loads of trigger (easy to copy/paste/edit the yaml. I’ll limit it to 90,80,70,60,40,20.

No you wouldn’t… If that was the case, I couldn’t use it in the condition to check that the value is decreasing.

Available Trigger Data - Template

My mistake. I just looked at the Automations documentation and see that those states are present for a Template trigger, as well as a State trigger. I just assumed they wouldn’t be. I’ll work on it.

The trigger could be

{% set s = states('sensor.moisture_3_soil_moisture') | int %}
{{ (s % 10 == 0 and 60 <= s <= 90) or s % 20 == 0 }}

If this is the requirement

I’ve settled on the following complete automation for my 6 plants with sensors. It should trigger all the way down to 9% in 10% intervals on any of the plants.

The message template, I’m sure could be simplified if i knew the right syntax for area_name(trigger.id). That might be it, but I haven’t tried it because it’s difficult to test without having the trigger go off naturally.

alias: Plants Watering
description: ""
triggers:
  - trigger: template
    value_template: >-
      {% set s_state = states('sensor.moisture_1_soil_moisture') | round(0,
      default=50) | int %} {{ (s_state //5) is odd and s_state < 90 }}
    for: "00:01:00"
    id: croton
  - trigger: template
    value_template: >-
      {% set s_state = states('sensor.moisture_2_soil_moisture') | round(0,
      default=50) | int %} {{ (s_state //5) is odd and s_state < 90 }}
    for: "00:01:00"
    id: dracaenna
  - trigger: template
    value_template: >-
      {% set s_state = states('sensor.moisture_3_soil_moisture') | round(0,
      default=50) | int %} {{ (s_state //5) is odd and s_state < 90 }}
    for: "00:01:00"
    id: anthurium
  - trigger: template
    value_template: >-
      {% set s_state = states('sensor.moisture_4_soil_moisture') | round(0,
      default=50) | int %} {{ (s_state //5) is odd and s_state < 90 }}
    for: "00:01:00"
    id: ficus
  - trigger: template
    value_template: >-
      {% set s_state = states('sensor.moisture_5_soil_moisture') | round(0,
      default=50) | int %} {{ (s_state //5) is odd and s_state < 90 }}
    for: "00:01:00"
    id: monstera
  - trigger: template
    value_template: >-
      {% set s_state = states('sensor.moisture_dracaena_en_suite_soil_moisture')
      | round(0, default=50) | int %} {{ (s_state //5) is odd and s_state < 90
      }}
    for: "00:01:00"
    id: en-suite
conditions:
  - condition: template
    value_template: "{{ trigger.to_state.state|float < trigger.from_state.state|float }}"
actions:
  - action: telegram_bot.send_message
    metadata: {}
    data:
      message: >-
        The {% if trigger.id != "en-suite" %}{{trigger.id}}{%else%}Draceana{%
        endif %}     in the {% if (trigger.id == "anthurium") or (trigger.id ==
        "draceana") %}Dining     Room {% elif (trigger.id == "croton") or
        (trigger.id == "monstera") %}Lounge {%     elif trigger_id == "en-suite"
        %}En-Suite {% elif trigger.id == "ficus"     %}Hall {% endif %} needs
        watering. Moisture level is reported as     {{trigger.to_state.state}}%
      title: Water Alert
mode: single

I’ve just got wait now to see if it works. 2 of my plants are currently at 90% so it shouldn’t be long. They will trigger at 89%.

No they won’t.
But you can use the developer tools to set a state of an entity, this should trigger the automation.

Can’t you set the room name as the id and just add this to the message.
You’re not using the plant name, or are you?
Edit, I see you do…

But if you edit the trigger name to name,plant then you can split the room and name and use them in the code.
Or just make the trigger id longer.

anthurium in the dining room and the rest could be hard coded.

Use time as the trigger and sensor.moisture_3_soil_moisture <90as the condition.

As long as the sensor is below 90, you will get a nag every 10 minutes.

    trigger:
      - platform: time_pattern
        minutes: "/10"

I never knew you could override the actual state of an entity. I just set one of the plants to 89 (was 90) and it triggered and i got the Telegram message :grinning:

But if you edit the trigger name to name,plant then you can split the room and name and use them in the code.

Yes, that would work, but I’m a believer in just entering data once ie at the the Device level. I think I will start another thread on that specific issue.

Edit: Solved the area problem with area_name(trigger.entity_id) so the action part of the automation now looks like this…

actions:
  - action: telegram_bot.send_message
    metadata: {}
    data:
      message: >-
        The {{ trigger.id  }}in the {{area_name(trigger.entity_id)}} needs
        watering. Moisture level is reported as {{trigger.to_state.state}}%
      title: Water Alert

Note that I changed the trigger_id: of the dracaena in the en-suite to dracaena.