Ahoj skúšal som všeličo možné nakoniec som zostal pri upravenom kode od paná menom hugokernel
https://github.com/hugokernel/esphome-rain-detector
xaml:
Kód: Vybrat vše
substitutions:
friendly_name: ESP-Monitoring
resistor_value: "9.9kOhm"
# TODO: Handle case where resistance higher than max_resistance
# Value of the resistance at which a significant change is considered
# to occur
rain_detection_threshold: "4000"
# If the resistance increases by this value, the sensor is considered
# to be dry
dry_detection_threshold: "4000"
# When booting, if the resistance is lower this value, assume sensor is wet
wet_resistance_when_booting: "30000"
# +------------------------------+
# | Delay between 2 rain measure |
# +------------------------------+
# In dry mode
measure_interval_dry: "5000"
# In wet mode
# Must be large enough not to damage the tracks prematurely
# but small enough to be reactive enough.
measure_interval_wet: "10000"
# Stabilization before reading the resistance
# A too short delay will not allow to read the low resistances
stabilization_delay: 3sec
esphome:
name: esp32-monitoring
on_boot:
then:
- script.execute: test_resistance
- script.execute: measure_loop
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: "c52839b4e05b4370da83f34a7796b54f"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp32-Monitoring"
password: "IiFLGrSG6reK"
captive_portal:
globals:
- id: measure_delay
type: int
restore_value: yes
initial_value: $measure_interval_dry
script:
- id: begin_measure
mode: single
then:
- switch.turn_on: resistance_bias
- delay: $stabilization_delay
- id: end_measure
mode: single
then:
- switch.turn_off: resistance_bias
- id: test_resistance
mode: single
then:
- script.execute: begin_measure
- script.wait: begin_measure
- if:
condition:
lambda: "return id(resistance_sensor).state < $wet_resistance_when_booting;"
then:
- script.execute: its_raining
- script.execute: end_measure
- script.wait: end_measure
- id: save_current_resistance
then:
- sensor.template.publish:
id: latest_resistance_sensor
state: !lambda "return id(resistance_sensor).state;"
- sensor.template.publish:
id: latest_average_resistance_sensor
state: !lambda "return id(average_resistance_sensor).state;"
# Currently:
# * Rain is detected with the most recent value compared to a threshold
# * To detect when it dries, we use the average
# To be tested:
# The lowest resistance that corresponds to a complete saturation
# of the sensor is permanently recorded: If this value is not reached
# but the resistance does not decrease during a period of time,
# we know that it is not raining anymore.
- id: update_average_values
mode: single
then:
# Set the average resistance
- if:
condition:
lambda: "return (id(resistance_sensor).state >= 0 && id(resistance_sensor).state <= 50000);"
then:
- sensor.template.publish:
id: average_resistance_sensor
state: !lambda "return id(resistance_sensor).state;"
- id: measure
mode: single
then:
- script.execute: begin_measure
- script.wait: begin_measure
# Init latest resistance value if not a number
- if:
condition:
lambda: "return isnan(id(latest_resistance_sensor).state);"
then:
- script.execute: save_current_resistance
- script.wait: save_current_resistance
- if:
condition:
lambda: "return isnan(id(latest_average_resistance_sensor).state);"
then:
- script.execute: save_current_resistance
- script.wait: save_current_resistance
- script.execute: update_average_values
# Test for raining
- if:
condition:
lambda: "return id(resistance_sensor).state + $rain_detection_threshold < id(latest_resistance_sensor).state;"
then:
- script.execute: its_raining
# Test for drying
- if:
condition:
lambda: "return id(average_resistance_sensor).state - $dry_detection_threshold > id(latest_average_resistance_sensor).state;"
then:
- script.execute: its_drying
# Test for dry
# We assume sensor is dry when current resistance == max resistance
- if:
condition:
lambda: "return id(resistance_sensor).state == 50000;"
then:
- script.execute: its_dry
- script.execute: end_measure
- script.wait: end_measure
- id: measure_loop
mode: single
then:
- while:
condition:
lambda: "return true;"
then:
- script.execute: measure
- script.wait: measure
- delay: !lambda "return id(measure_delay);"
- id: its_raining
mode: single
then:
- script.execute: save_current_resistance
- script.wait: save_current_resistance
- homeassistant.event:
event: esphome.its_raining
- text_sensor.template.publish:
id: text_status
state: "Raining"
- globals.set:
id: measure_delay
value: $measure_interval_wet
- id: its_drying
mode: single
then:
- script.execute: save_current_resistance
- script.wait: save_current_resistance
- text_sensor.template.publish:
id: text_status
state: "Drying"
- globals.set:
id: measure_delay
value: $measure_interval_wet
- id: its_dry
mode: single
then:
- script.execute: save_current_resistance
- script.wait: save_current_resistance
- text_sensor.template.publish:
id: text_status
state: "Dry"
- globals.set:
id: measure_delay
value: $measure_interval_dry
text_sensor:
- platform: template
id: text_status
name: "${friendly_name} text status"
lambda: |-
return {"Wet"};
sensor:
- platform: adc
id: source_sensor
pin: GPIO33
name: ADC
attenuation: 11db
internal: true
# It is important to have a low update interval so that
# the measurement has time to be done correctly during
# the activation of the voltage AND taking into account the median filter
update_interval: 500ms
filters:
# - multiply: 0.846153 # 3.9 (11db attenuation full-scale voltage) -> 3.3V
- median:
window_size: 7
send_every: 4
send_first_at: 3
- platform: resistance
sensor: source_sensor
id: real_resistance_sensor
#name: "${friendly_name} resistance"
configuration: DOWNSTREAM
resistor: $resistor_value
reference_voltage: 3.3V
internal: true
icon: "mdi:omega"
filters:
# No value lower than 0
- lambda: 'return max((float)1000, x);'
# No value greater than $max_resistance
- lambda: 'return min((float)50000, x);'
on_value:
then:
- if:
condition:
lambda: |-
return (
id(real_resistance_sensor).state > 1000
&&
// <= is important to force the resistance to the max
// in order to have a value to compare if the
// resistance drops
id(real_resistance_sensor).state <= 50000
);
then:
- sensor.template.publish:
id: resistance_sensor
state: !lambda "return id(real_resistance_sensor).state;"
- platform: template
id: latest_resistance_sensor
name: "${friendly_name} latest resistance"
icon: "mdi:omega"
unit_of_measurement: 'Ω'
internal: true
- platform: template
id: latest_average_resistance_sensor
name: "${friendly_name} latest average resistance"
icon: "mdi:omega"
unit_of_measurement: 'Ω'
internal: true
- platform: template
id: resistance_sensor
name: "${friendly_name} resistance"
icon: "mdi:omega"
unit_of_measurement: 'Ω'
- platform: template
id: average_resistance_sensor
name: "${friendly_name} average resistance"
icon: "mdi:omega"
unit_of_measurement: 'Ω'
filters:
- sliding_window_moving_average:
window_size: 5
send_every: 5
#- heartbeat: 2min
internal: true
switch:
- platform: gpio
id: resistance_bias
name: "${friendly_name} resistance bias"
icon: "mdi:power"
pin:
number: GPIO26
mode: OUTPUT
##DEEP SLEEP
deep_sleep:
run_duration: 30sec
sleep_duration: 30sec
Ale vznikol mi ďalši problém s deep sleep. Bez neho funguje senzor s kódom presne tak ako potrebujem.
Akonáhle zapnem DeepSleep tak senzor hlasí vždy na začiatku že prši ak je vlhký pretože si neviem zapamätať posledné hodnoty repektíve počita ich od začiatku.
Je to metostanica na batérií takže bez uspania to nepojde.
Nie som vobec zadtný v kódovaní skôr dokažem iba upravovať. Tak ak tu je nejaký macher tak poradte. Ďakujem