Grouping and automating Soil Sensors

Hey folks. First post here. I am scratching my head and I cannot get this to work.

I have several Soil moisture sensors which are monitoring moisture.

I groupped them into one sensor group.


I spend already few hours prototyping with many LLMs but I can’t get automation to work.

Each sensor has three entities:

  • sensor.bedroom_soil_sensor_1_battery
  • sensor.bedroom_soil_sensor_1_soil_moisture
  • sensor.bedroom_soil_sensor_1_temperature
    I care about sensor.bedroom_soil_sensor_1_soil_moisture :slight_smile:
    This is how the sensor looks.

Following code doesn’t work

alias: Low Soil Moisture Notification
description: "Notify when soil moisture falls below 30%"
trigger:
  - platform: time_pattern
    hours: "/6"
  - platform: state
    entity_id: sensor.soil_moisture_group
condition:
  - condition: template
    value_template: >-
      {% set low_sensors = namespace(count=0) %}
      {% set sensors = states.sensor | selectattr('entity_id', 'search', 'soil_sensor') | selectattr('entity_id', 'search', 'soil_moisture') | list %}
      {% for sensor in sensors %}
        {% if sensor.state not in ['unavailable', 'unknown'] and sensor.state | float < 30 %}
          {% set low_sensors.count = low_sensors.count + 1 %}
        {% endif %}
      {% endfor %}
      {{ low_sensors.count > 0 }}
action:
  # Mobile app notification (persistent)
  - service: notify.notify
    data:
      message: >-
        {% set low_moisture_plants = [] %}
        {% set sensors = states.sensor | selectattr('entity_id', 'search', 'soil_sensor') | selectattr('entity_id', 'search', 'soil_moisture') | list %}
        {% for sensor in sensors %}
          {% if sensor.state not in ['unavailable', 'unknown'] and sensor.state | float < 30 %}
            {% set low_moisture_plants = low_moisture_plants + [sensor.attributes.friendly_name] %}
          {% endif %}
        {% endfor %}
        Soil moisture is low! Plants needing water: {{ low_moisture_plants | join(', ') }}
      data:
        persistent: true
        tag: soil_moisture_alert
        priority: high
        ttl: 0
        actions:
          - action: PLANTS_WATERED
            title: Mark as Watered
  
  # Persistent notification in Home Assistant UI
  - service: persistent_notification.create
    data:
      title: "Low Soil Moisture Alert"
      message: >-
        {% set low_moisture_plants = [] %}
        {% set sensors = states.sensor | selectattr('entity_id', 'search', 'soil_sensor') | selectattr('entity_id', 'search', 'soil_moisture') | list %}
        {% for sensor in sensors %}
          {% if sensor.state not in ['unavailable', 'unknown'] and sensor.state | float < 30 %}
            {% set low_moisture_plants = low_moisture_plants + [sensor.attributes.friendly_name] %}
          {% endif %}
        {% endfor %}
        Soil moisture is low! Plants needing water: {{ low_moisture_plants | join(', ') }}
      notification_id: "low_soil_moisture"
mode: single

My automation should notify me if the soil moisture is below decimal defined %. How can I make this thing work?

best regards
Wojciech

Seems like you’re WAY over complicating this. Why don’t you just use a numeric state trigger and throw them all in there?

  - trigger: numeric_state
    entity_id:
      - sensor.moisture_1
      - sensor.moisture_2
    below: 30

Additionally, if you have lots of soil sensors/plants, check out the Home Assistant Plant custom integration: