Newbie could use automation help

Hello!

So I’m working on a project using an RPi and a binary water sensor connection to the gpio. I’ve been able to get HA to see the sensor and correctly read it’s state. I’ve got the automation working where it will trigger an IFTTT event when water is detected for 1 minute. The issue I’m having is that if I take the sensor out of the water and then put it back in it doesn’t always trigger the event again. I also need to figure out how to set it up so that if it detects water and triggers an event it will continue to trigger the event let’s say every hour until the sensor no longer detects water. I also need it to trigger an event when the sensor no longer detects water. I’ll include what I have currently, can any of you automation gurus help me out? Here’s my current code:

automation:
  alias: Water Detection Notification
  trigger:
    platform: state
	entity_id: binary_sensor.water_detection_sensor
	state: 'on'
	for:
	  minutes: 1
  action:
    service: ifttt.trigger
	data: {"event":"water_detected"}

Thanks!

Are you seeing the binary_sensor.water_detection_sensor 's state go to “off” when you take it out of the water? The trigger you have there will only fire the first time it reaches one minute of ‘on’. It’d have to turn off before turning on again for the trigger to be true.

For the other issues/questions, start here: you can have multiple triggers for an automation. So you could do something like have the event trigger on state change of the water_detection_sensor and trigger every five minutes. Then, add a condition that confirms the on state of the sensor. That way, the automation will trigger immediately when water is detected, then every 5 minutes it’ll trigger again and you can do something if water is still detected.

Depending on what entities you have available, you could also add a condition that it’s been X minutes since something’s happened/changed. You might need to throw an input_boolean into the mix to handle that - IFTT doesn’t create an entity that you can watch.

The alert component does regular interval notifications, encapsulating some of this.

Thanks for the quick reply, I am seeing the binary_sensor.water_detection_sensor’s state go to “off” when it’s taken out of the water 9 times out of 10. Once and awhile it’s state doesn’t change when it’s out of the water and I haven’t determined why yet.

Pardon my newbness, you say I can have multiple triggers for an automation but how does that look code wise? Do I just write another trigger statement below my current one? I do I write the trigger statement so it operates on an interval?

If it turns “off” at some point, then the “on” state should trigger after a minute.

Ok so I’ve worked on this a little more and this is what I’ve come up with for multiple triggers based on the length of time the sensor has been in water. Will this work? I feel like this will accomplish my first task which is sending out a message that water has been detected at different time intervals but that doing it this way I won’t be able to send a notification when the sensor no longer detects water. Here’s my current code

automation:
  alias: Water Detection Notification
   -trigger:
    platform: state
    entity_id: binary_sensor.water_detection_sensor
    state: 'on'
    for:
     minutes: 1
   action:
   service: ifttt.trigger
   data: {"event":"water_detected"}
   -trigger:
    platform: state
    entity_id: binary_sensor.water_detection_sensor
    state: 'on'
    for:
     minutes: 15
    action:
    service: ifttt.trigger
    data: {"event":"water_detected"}
    -trigger:
    platform: state
    entity_id: binary_sensor.water_detection_sensor
    state: 'on'
    for:
     minutes: 30
    action:
    service: ifttt.trigger
    data: {"event":"water_detected"}

From my reading it seems like I would be better off handling the state of the sensor using condition statements to trigger my events. Am I understanding this correctly. Sorry for the simple questions I’m just very new to HA and YAML

You need to work on the structure a bit. Automations have 3 parts. Trigger, condition, action. Trigger starts things. A condition is basically a filter - if it’s false, the automation stops. The action is what happens when everything is true.

So the additional triggers need to be combined in a list with the first one. In YAML, the dashes (-) are a list, if that helps you get your brain around it. And you want a list of triggers with their associated settings.

(note - see how I made the service call of the action a list, too, by using a dash. Even though it’s a list of one, it’s fine. In practice, I usually format things this way so that if I add another trigger, action, etc, there’s less refactoring)

automation:
  alias: Water Detection Notification
  trigger:
    - platform: state
      entity_id: binary_sensor.water_detection_sensor
      state: 'on'
      for:
        minutes: 1
    - platform: state
      entity_id: binary_sensor.water_detection_sensor
      state: 'on'
      for:
       minutes: 15   
    - platform: state
      entity_id: binary_sensor.water_detection_sensor
      state: 'on'
      for:
       minutes: 30  
   action:
    - service: ifttt.trigger
      data: {"event":"water_detected"}

Here, you don’t need a condition because you’re reacting to water being detected, so no need to check that as a condition, too.

1 Like

Ok I see how lists work now I thought I was accomplishing that by using -trigger but that makes sense now. So if I wanted to trigger an action for when state goes from on to off is there an attribute of the state for change? Also how can I tell what states are available for a given sensor/components. Thanks again you’ve been extremely helpful and patient with this newb!

The docs show an example for using “to” and “from”:

It’s not always easy to get the possible states for an entity. Typically, I’ll manipulate the device (open/close a door, play/pause a media device…) and watch the states dev tool to see what happens. Then use those states as a basis for your automation.