Issue with Home Assistant Automation when Reading Multiple Sensors

Hello to the entire community!

I am facing a challenge with my current project involving an ESP32, multiple sensors, and Home Assistant. I have set up three sensors that monitor the soil moisture of three different plants. The idea is for the ESP32 to read the information from these sensors and send it to Home Assistant, where I have an automation configured to manage irrigation.

The issue arises when multiple sensors trigger the automation simultaneously. Instead of receiving a single notification with a summary of all the plants that need watering, I get a notification for each plant that requires attention. This results in multiple notifications instead of a single, desired consolidated notification.

My goal is to optimize this process to receive a single notification that includes all the plants in need of watering, rather than individual notifications for each plant.

Below, I provide the relevant code used in ESPHome for the sensors and the Home Assistant automation:

ESPHome Sensors Code:

sensor:
  - platform: adc
    id: $sensor_plant_01
    pin: $pin_plant_01
    name: $plant_name_01
    update_interval: 100ms
    unit_of_measurement: "%"
    attenuation: 11db
    accuracy_decimals: 0
    icon: mdi:sprout
    filters:
    - median:
        window_size: 5
        send_every: 5
        send_first_at: 5
    - calibrate_linear:
        - $value_wet -> 100.00
        - $value_dry -> 0.00
    - lambda: |-
        if (x < 1) {
          return 0;
        } else if (x > 100) {
          return 100;
        } else {
          return x;
        }
    on_value:
      then:
        - if:
            condition:
              lambda: 'return x < 30;'
            then:
              - script.execute: flash_light_01
        - if:
            condition:
              lambda: 'return x >= 30 && x < 50;'
            then:
              - light.turn_on: $ledplant_01
        - if:
            condition:
              lambda: 'return x >= 50;'
            then:
              - light.turn_off: $ledplant_01
  - platform: adc
    id: $sensor_plant_02
    pin: $pin_plant_02
    name: $plant_name_02
    update_interval: 100ms
    unit_of_measurement: "%"
    attenuation: 11db
    accuracy_decimals: 0
    icon: mdi:sprout
    filters:
    - median:
        window_size: 5
        send_every: 5
        send_first_at: 5
    - calibrate_linear:
        - $value_wet -> 100.00
        - $value_dry -> 0.00
    - lambda: |-
        if (x < 1) {
          return 0;
        } else if (x > 100) {
          return 100;
        } else {
          return x;
        }
    on_value:
      then:
        - if:
            condition:
              lambda: 'return x < 30;'
            then:
              - script.execute: flash_light_02
        - if:
            condition:
              lambda: 'return x >= 30 && x < 50;'
            then:
              - light.turn_on: $ledplant_02
        - if:
            condition:
              lambda: 'return x >= 50;'
            then:
              - light.turn_off: $ledplant_02

Home Assistant Automation Code:

alias: Water the plants test
description: Custom notification to water plants
trigger:
  - platform: device
    type: turned_on
    device_id: 56dfdfdd3c5f1e48c62143c0b2
    entity_id: 1a7acddfdfdf693527a4d565a2ab4a37
    domain: light
  - platform: device
    type: turned_on
    device_id: 5645dfdfdfdbd3c5f1e48c62143c0b2
    entity_id: 846dfdfdfa66bdf4e060350
    domain: light
condition: []
action:
  - service: notify.mobile_app_iphone_de_juanjo
    metadata: {}
    data:
      message: >
        Toca regar 
        {% set plantas = trigger.entity_id | map(attribute='attributes.friendly_name') | list %}
        {% if plantas | length > 1 %}
          {% set ultima = plantas | pop() %}
          {{ plantas | join(', ') }} y {{ ultima }}
        {% else %}
          {{ plantas | join(', ') }}
        {% endif %}.
      title: Regar las plantas
mode: parallel
max: 10

I would greatly appreciate any guidance or suggestions to resolve this issue and optimize my setup. Thank you in advance for the assistance!

Best regards,

I don’t know if this what is causing the problems but device is not the right trigger, just use the entity. See this post

Thank you so much @Mikefila

I have been checking the topic, but I have not found a solution to my problem :crying_cat_face:

Change the triggers from device to state. Use the light entity

trigger:
  - platform: state
    entity_id:
      - light.NAME_OF_LIGHT
    from: "off"
    to: "on"

Hi :raising_hand_man:,

That option does not solve my problem either, I have several sensors, in the example code, I have only included two, but I intend to manage some more, four or five.

With your proposal I do not see a way to solve the problem that I raise in the topic, sending a single notification with all the plants that need to be watered at that moment, for example, every hour, as I understand what you propose, I am managing the notification of a sensor reading.

thank you very much again.

On that note, I see the template reverences the trigger entity perhaps that needs to be the device?

What happens is that the device has different entities (sensors), and the values of these entities must be evaluated. Unless I’m wrong.

The way I know how to do this in HA is to make a group with all the lights.

Use this as the trigger

  trigger:
    - platform: state
      entity_id: light.all
      to: "on"

This should trigger every time one of the lights comes on.

Use this in the message and it will return the entities in the group that are on

{{ expand('light.all')|selectattr('state','eq','on')|map(attribute='name')|join(', ') }}

what’s in the script your executing, flash_light_01?

What are you wanting to evaluate from each plant/sensor? Is it just the value 0-100?

What are these? ledplant_01 lights? are these physical LED’s just for visual signs to show that it’s working?

It looks like you are using the state of these lights and using them for multiple triggers. The reason you’re getting multiple notifications is because you’ve made a trigger for each one of the lights turning on, so each one fires the automation…

trigger:
  - platform: device
    type: turned_on
    device_id: 56dfdfdd3c5f1e48c62143c0b2
    entity_id: 1a7acddfdfdf693527a4d565a2ab4a37
    domain: light
  - platform: device
    type: turned_on
    device_id: 5645dfdfdfdbd3c5f1e48c62143c0b2
    entity_id: 846dfdfdfa66bdf4e060350
    domain: light

Personally I would just do it with a Time trigger. Maybe something like twice a day, once in the morning and another in the afternoon. So, your automation triggers twice a day and each time your going to use a condition to check the state of each LED and report which ones are ON/OFF and do the same thing for the afternoon one.

  1. I don’t know what you could be growing that needs a moisture level reading every 100ms, that’s likely way to frequent and you’re just gonna ruin your sensors like doing that. Are you using capacitive soil moisture sensors? If you are, you definately don’t want to be doing ADC measurements very often at all, maybe a couple 2 or 3 times a day at most. I have lots of plants and trees in pots and most get watered once a week or twice a week at the most. You only need a couple readings a day.

  2. IDK what plants you have but, plants have water requirements, like some like it moist and some absolutely don’t like excess water and will get root-rot. For most plants you should allow them to dry out some in-between watering cycles. An automation that for example checks to see if a sensor was < 30 on Monday but got watered and went to 100 the same day. If it’s <= 30 again, then check to see if it was less than 2 days ago. If it was less than 2 days then don’t turn on light(needs watering) for today but, tomorrow it can. Something like that, that helps you keep track of when you water them so you’re not constantly watering them if they dry out a little because that’s actually a good thing for most plants.

  3. depending on the plant, I wouldn’t take moisture readings or trigger watering alerts If there’s less than 2 hours of sunlight. This is if they’re house plants or garden potted plants. Leaving plants with excess water overnight is inviting fungii problems.

These are just some ideas of things to add create a proper watering automation. You don’t want to just use some arbitrary (< 30) number and water every time it dips that low. Plants are more complicated than that.

Ah, I see! Thanks for explaining how this can be done in Home Assistant. Creating a group with all the lights and using it as a trigger seems like a practical and effective solution. I’ll definitely keep that in mind for my project.

Additionally, I really appreciate you sharing the code to retrieve the entities in the group that are turned on. This will be very helpful for me.

Thanks again for your assistance and for sharing your knowledge!

Thank you so much for your response! I’m very new to programming with esphome and also a beginner in caring for my indoor plants. I’ve always struggled to keep them alive for long periods, which has been a bit disheartening. However, since delving into home automation for my house and programming with esphome, I realized that I could potentially find a solution to my dilemma with my home plants.

I really appreciate all the advice and pointers you’ve given me regarding plant care and programming. I’ll make sure to take note of everything and reconsider my project, taking into account your valuable insights. I truly appreciate you sharing your knowledge with me. Thanks for everything!

We were all there at one point or another, just stick with it and you will start making large leaps of progress. I totally get the desire to automate something like this but, if you think about it logically. How do you automate something that you can’t successfully do manually? Meaning, if you can’t keep them alive now, how does automating something you already can’t do successfully make much sense? I don’t mean to discourage you at all. If you want to do it then by all means, go for it. I’m just trying to point out the bigger picture, thats all.

Thanks for the encouragement, buddy! Although I have to say, I don’t quite see eye to eye with your perspective. You know, everyone has their own story and circumstances, right? And in my case, I’m firm on the idea that we should do whatever it takes to overcome the ‘limitations’ that come our way. It’s like that example of running 60 km: if you can’t do it on foot, why not hop on a bike to reach the finish line anyway? Exactly! It’s not about sticking to the traditional path at all costs, but rather adapting and making the most of what we have.

So yeah, I appreciate the positive vibes, but that last comment didn’t quite hit the mark. Everyone finds their own way, and I’m here to do it my way, with the tools I have at my disposal. Let’s keep moving forward with all the energy, buddy! If you need more pep talks or advice, you know where to find me. Let’s go for it! :rocket:

Ya, I guess you can live in an alternate reality but, facts are facts and facts don’t care about feelings.