Need help with template code

It’s my first template, Looks like all works but template. I suspect the lambda condition in the template is incorrect? My intention was reuse the condition created in the binary_sensor.

sensor:
  - platform: adc
    pin: A0
    icon: mdi:water-alert
    name: "Water leak Laundry Status"
    id: water_leak_Laundry_reading
    unit_of_measurement: "%"
    accuracy_decimals: 0
    filters:
    - calibrate_linear:
        - 1.0 -> 100
        - 0.011 -> 0
    - lambda: if (x < 1) return 0; else return (x);    
    update_interval: 30s
    
binary_sensor:
  - platform: template
    name: "Water leak Laundry Sensor"
    lambda: |-
         float analog = id(water_leak_Laundry_reading).state;
         return analog > 50; 
    on_press:
      then:
        - switch.turn_on: flood_buzzer_sw
    on_release:
      then:
        - switch.turn_off: flood_buzzer_sw

switch:
  - platform: gpio
    name: flood_buzzer_sw
    id: flood_buzzer_sw
    pin:
      number: GPIO4
    inverted: true
  
  - platform: template
    name: flood_buzzer_tm
    id: flood_buzzer_tm
    optimistic: true
    turn_on_action:
    - while:
        condition:
          lambda: 'return true;'
        then:
        - switch.turn_on: flood_buzzer_sw
        - delay: 5s 
        - switch.turn_off: flood_buzzer_sw
        - delay: 5s
    turn_off_action:
    - switch.turn_off: flood_buzzer_sw      
    #internal: true

You might want to start with explaining what you are trying to do in a little more detail.

The code doesn’t make sense to me.

You are using an ADC to measure something. It says it is a leak sensor, but seems to have an output range of 0 to more than 100 (might go to 300 or more, depending on exactly how the ADC is setup, but since the units are %, you probably want it to go to 100%). Not sure what kind of leak sensor you have, but most are binary (leak or no leak).

You then have a binary sensor (presumably trying to get to the standard yes/no for the question is there a leak or not).

The condition in the template in the switch section doesn’t make sense, since if it worked would always be true.
Pretty sure binary_sensor doesn’t have on_press or on_release.

I am guessing that your goal is to have the buzzer go on for 5s and then off for 5s as long as there is a leak. And you have a strange leak sensor that is not digital. I am sure there is a way to make the process much clearer.

looks promising and it looks like binary_sensor does support on_press at least for platform gpio

This is also why it is important to provide the log from when you run the config check.

Hi, Neel,
here what I’d like to have

  1. WIFI connection status - that is obvious
  2. Yes moisture level in % this way I can see if it getting wet or drying in graph.
  3. binary sensor - turn on buzzer on particular moisture level. In cases when location never dry but acceptable until started to flood.
  4. the template should turn the buzzer ON and OFF. I did not include Sleep yet. Intention for these to preserve the battery and get attention.
    I have no errors while compiling. #2 and #3 work as expected. I’m focusing on #4 now. And #1 should be easy, I think. (I’m debugging using logs of course) Thank you

this is yaml for wifi sensor. Was not included in the original post.

sensor:
  - platform: wifi_signal
    name: "Water leak Laundry WiFi Signal"
    update_interval: 30s
    filters:
      - sliding_window_moving_average:
          window_size: 15
          send_every: 15
          send_first_at: 15
    icon: mdi:wifi

It makes sense to me. Hes using an eap8266 which has uses 0.0v - 1.0v as its ADC range so, no it might not and wont go to 300 or more as he has configured it here and that

filters:
    - calibrate_linear:
        - 1.0 -> 100
        - 0.011 -> 0

This part maps the ADC minimum value to 0% and the max value, 1.0v to 100% so that it only returns 0-100% instead of the default which is a voltage.

depending on exactly how the ADC is setup

He posted the config of his ADC and you can see exactly how he configured it so, im not sure what other variables your looking for that depends on anything relevant here

sensor:
  - platform: adc
    pin: A0
    icon: mdi:water-alert
    name: "Water leak Laundry Status"
    id: water_leak_Laundry_reading
    unit_of_measurement: "%"
    accuracy_decimals: 0
    filters:
    - calibrate_linear:
        - 1.0 -> 100
        - 0.011 -> 0
    - lambda: if (x < 1) return 0; else return (x);    
    update_interval: 30s

Some are “digital” and use binary sensor outputs as the sensors value but, even if “most” are ineed digital, then the ones that aren’t “most”. That second type is mostly Analog, which are actually pretty widely used, just not as much in the “hobbyist” IOT devices because manufacturers try using the simplest or “made for dummies” type of devices and if some hobbyist smart home guy bought an ADC device that output floating point numbers, they’re less likely tp like or want that when a digital one says, “Wet” and anyone can figure that out…

Cool, congratulations is it a boy? Girl? Somewhere in between?

Ok… I get that your new and bouncing back and forth between excited and overwhelmed without being sure what to do so, I’ll give you a pass this one and only time! Next time though… Too the Dungeon!!

The way that you find out and know for sure is by clicking that “Install” button up ontop and to the right. If something isnt right, i promise you it will let you know it’s not right and it will specifically tell you the line number and what the issue is at line 56 for example.

You dont have to be sure its perfect before you try to install/flaah the esp board. You also shouldn’t even try to do that in the fist flace either!

The more you add in a single modification, the more difficult it can be to troubleshoot more complex logic or configurations. IMO it’s a good habit to have if you instead break it down into pieces and fix one thing at a time while simultaneously verifying it’s working.

If for example you only did this.

sensor:
  - platform: adc
    pin: A0
    icon: mdi:water-alert
    name: "Water leak Laundry Status"
    id: water_leak_Laundry_reading
    unit_of_measurement: "%"
    accuracy_decimals: 0
    filters:
    - calibrate_linear:
        - 1.0 -> 100
        - 0.011 -> 0
    - lambda: if (x < 1) return 0; else return (x);    
    update_interval: 30s

You would know if there are any errors because it wont compile and if it works as is, you can verify that and you can see/confirm in the logs that it is calibrated and you can see the value changing as you test it with some water or by simply licking your finger and verifying it picks up that change.

So, before i jump into your code, i have to insist that you try installing it first and knowing for sure which parts are incorrect and need fixed. This is how you learn and how you learn how to troubleshoot your own projects! Its a good skill to learn and IMO its an absolutely necessary skill you need to learn! Or you can go to the Dungeon! Muhahaha… Thats my evil laugh btw.

Yes it is esp8266, Sorry for missing info. Just two wires for now: one on Vcc and second on A0 would give 30% on almost dry paper towel. Of course 100% on wet. Other option is capacitance moisture sensor.
By the way, If I manually toggle the “Flood_buzzer_tm” ON
the template will be activated, “Flood_buzzer_sw” would go ON/OF. But the template would still active with “Water Leak sensor” is OFF
If I turn off the “Flood_buzzer_tm” all will be turn OFF. And turning it back ON will turn the template back ON
So the logic is broken in yaml, just I can not figure out where.

WTF, it’s boy. appreciate you ask and no its not easy to offend me.

It compiles fine. And by the way if I remove the code for the template, all works, of course the Flood_buzzer_tm missing.

Dry

Wet

this code removed

#- platform: template
  #  name: flood_buzzer_tm
  #  id: flood_buzzer_tm
  #  optimistic: true
  #  turn_on_action:
  #  - while:
  #      condition:
  #        lambda: 'return true;'
  #      then:
  #      - switch.turn_on: flood_buzzer_sw
  #      - delay: 5s 
  #      - switch.turn_off: flood_buzzer_sw
  #      - delay: 5s
  #  turn_off_action:
  #  - switch.turn_off: flood_buzzer_sw      
    #internal: true

all code. Hope community can use it as example.
Thank you

sensor: 
  
  - platform: wifi_signal
    name: "Water leak Laundry WiFi Signal"
    update_interval: 30s
    filters:
      - sliding_window_moving_average:
          window_size: 15
          send_every: 15
          send_first_at: 15
    icon: mdi:wifi

  - platform: adc
    pin: A0
    icon: mdi:water-alert
    name: "Water leak Laundry Status"
    id: water_leak_Laundry_reading
    unit_of_measurement: "%"
    accuracy_decimals: 0
    filters:
    - calibrate_linear:
        - 1.0 -> 100
        - 0.011 -> 0
    - lambda: if (x < 1) return 0; else return (x);    
    update_interval: 5s
    
binary_sensor:
  - platform: template
    name: "Water leak Laundry Sensor"
    lambda: |-
         float analog = id(water_leak_Laundry_reading).state;
         return analog > 50; 
    on_press:
      then:
        - switch.turn_on: flood_buzzer_sw
    on_release:
      then:
        - switch.turn_off: flood_buzzer_sw

switch:
  - platform: gpio
    name: flood_buzzer_sw
    id: flood_buzzer_sw
    pin:
      number: GPIO4
    inverted: true
  
  #- platform: template
  #  name: flood_buzzer_tm
  #  id: flood_buzzer_tm
  #  optimistic: true
  #  turn_on_action:
  #  - while:
  #      condition:
  #        lambda: 'return true;'
  #      then:
  #      - switch.turn_on: flood_buzzer_sw
  #      - delay: 5s 
  #      - switch.turn_off: flood_buzzer_sw
  #      - delay: 5s
  #  turn_off_action:
  #  - switch.turn_off: flood_buzzer_sw      
    #internal: true

bit late, see you guys and girls and between tomorrow :slight_smile:

I could tell, it’s got a tiny little package like Daddy! Lol, sounds like permission to try harder ; )

Im just messing around with ya man, its all in good fun! Others would have already been calling my manager so, a sense of humor is great!

So, you got it all working then? If so, i’ll put your bill in the mail for my tech support. Dont make me come track you down in order to get it paid! The last guy didnt walk right for a week. ; )

Have fun on your journey into this hobby and it’s quite addictive if you havnt found out already!

FYI in the future if you post questions, the more info you can provide the better people are able to help you. Pin AO is only for esp8266 so it was easy to determine your board but, try to make sure you add things like which board and if its a D1 mini for example, make sure to specify which one because D1 mini comes in esp8266 and esp32. Post config just like you did here. It also helps if you specify which specific sensor you’re using with either a name w/model # or the link for Amazon or wherever you bought it. It just helps us rule out simple things by knowing and not just assuming we know what your using because there are so many options available and they’re not all identical. Pictures arent always necessary but, if its no trouble for you to take one of your wiring, that can be very helpful too sometimes and last, adding a little flattery wouldn’t hurt here! Just because I’ve got a snaggletooth doesn’t mean i dont like hearing how handsome i am. : )

Well, the handsome one, the problem is not solved, And I did not mentioned that English my second language. So let re-hash my situation.

my goals:

  • measure in % this way I can see if it getting wet or drying in graph.
  • binary sensor - turn on buzzer on particular moisture level. In cases when location never dry but acceptable until started to flood.
  • the template should turn the buzzer ON and OFF. I did not include Sleep yet. Intention for these to preserve the battery and get attention.
  1. The system behaves according to the configuration yaml. that is without without the “template”. Of course the buzzer stays ON when sensor is triggered. Buzzer follows the trigger, goes OFF with the trigger.
    It behaves as I described in the post that start with WTF :slight_smile:
    This is the code I removed.
#- platform: template
  #  name: flood_buzzer_tm
  #  id: flood_buzzer_tm
  #  optimistic: true
  #  turn_on_action:
  #  - while:
  #      condition:
  #        lambda: 'return true;'
  #      then:
  #      - switch.turn_on: flood_buzzer_sw
  #      - delay: 5s 
  #      - switch.turn_off: flood_buzzer_sw
  #      - delay: 5s
  #  turn_off_action:
  #  - switch.turn_off: flood_buzzer_sw      
    #internal: true
  1. If the template is present, actions in the template would not activate. But if activated manually, It never stops. I will provide details in the next post.

Here is how system behaves when the template is present in the yaml.
Action associated with the code in the template never starts even when “Water Leak sensor” is triggered. If I manually toggle the “Flood_buzzer_tm” ON
the template will be activated, “Flood_buzzer_sw” would go ON/OF. But the template would still active with “Water Leak sensor” is OFF
If I turn off the “Flood_buzzer_tm” all will be turn OFF. And turning it back ON will turn the template back ON
So the logic is broken in yaml associated with the template, just I can not figure out where.

just an update. I changed labda condition in the flood_buzzer_tm and it is activated in the binary sensor template. Not very elegant but all appear to be working but WIFI sensor. it’s next.

captive_portal:

sensor: 
  - platform: wifi_signal
    name: "Water leak Laundry WiFi Signal"
    update_interval: 30s
    filters:
      - sliding_window_moving_average:
          window_size: 15
          send_every: 15
          send_first_at: 15
    icon: mdi:wifi
    #internal: true

  - platform: adc
    pin: A0
    icon: mdi:water-alert
    name: "Water leak Laundry Status"
    id: water_leak_Laundry_reading
    unit_of_measurement: "%"
    accuracy_decimals: 0
    filters:
    - calibrate_linear:
        - 1.0 -> 100
        - 0.011 -> 0
    - lambda: if (x < 1) return 0; else return (x);    
    update_interval: 5s
    
binary_sensor:
  - platform: template
    name: "Water leak Laundry Sensor"
    lambda: |-
         float analog = id(water_leak_Laundry_reading).state;
         return analog > 50; 
    on_press:
      then:
        - switch.turn_on: flood_buzzer_sw
        - switch.turn_on: flood_buzzer_tm
    on_release:
      then:
        - switch.turn_off: flood_buzzer_sw
        - switch.turn_off: flood_buzzer_tm

switch:
  - platform: gpio
    name: flood_buzzer_sw
    id: flood_buzzer_sw
    pin:
      number: GPIO4
    inverted: true
  
  - platform: template
    name: flood_buzzer_tm
    id: flood_buzzer_tm
    optimistic: true
    turn_on_action:
    - while:
        condition:
          lambda: |-
               float analog = id(water_leak_Laundry_reading).state;
               return analog > 50;
        then:
        - switch.turn_on: flood_buzzer_sw
        - delay: 10s 
        - switch.turn_off: flood_buzzer_sw
        - delay: 20s
    turn_off_action:
    - switch.turn_off: flood_buzzer_sw      
    #internal: true