Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- substitutions:
- name_1: main
- friendly_name_1: "Licznik główny"
- name_2: garden
- friendly_name_2: "Licznik ogrodowy"
- esphome:
- name: esp-woda
- comment: Odczyt liczników wody
- on_boot:
- then:
- - script.execute: restore_states
- on_shutdown:
- then:
- - script.execute: backup_states
- # Enable Home Assistant API
- api:
- services:
- - service: backup_data
- then:
- - script.execute: backup_states
- - service: restore_data
- then:
- - script.execute: restore_states
- - service: set_${name_1}_water_counter
- variables:
- new_total: int
- then:
- - pulse_counter.set_total_pulses:
- id: ${name_1}_water_pulse_counter
- value: !lambda 'return new_total;'
- - service: set_${name_2}_water_counter
- variables:
- new_total: int
- then:
- - pulse_counter.set_total_pulses:
- id: ${name_2}_water_pulse_counter
- value: !lambda 'return new_total;'
- #################################################################
- # Globals
- globals:
- - id: ${name_1}_water_total_backup
- type: int
- restore_value: true
- - id: ${name_2}_water_total_backup
- type: int
- restore_value: true
- #################################################################
- # Scripts
- script:
- - id: backup_states
- then:
- - globals.set:
- id: ${name_1}_water_total_backup
- value: !lambda 'return id(${name_1}_water_total).state;'
- - globals.set:
- id: ${name_2}_water_total_backup
- value: !lambda 'return id(${name_2}_water_total).state;'
- - id: restore_states
- then:
- - pulse_counter.set_total_pulses:
- id: ${name_1}_water_pulse_counter
- value: !lambda 'return id(${name_1}_water_total_backup);'
- - pulse_counter.set_total_pulses:
- id: ${name_2}_water_pulse_counter
- value: !lambda 'return id(${name_2}_water_total_backup);'
- #################################################################
- # Buttons
- button:
- - platform: template
- name: "Backup counters"
- icon: "mdi:content-save-all"
- on_press:
- - script.execute: backup_states
- - platform: template
- name: "Restore backup"
- icon: "mdi:backup-restore"
- on_press:
- - script.execute: restore_states
- #################################################################
- # Sensors
- sensor:
- - platform: pulse_counter
- name: '${friendly_name_1} - przepływ'
- id: ${name_1}_water_pulse_counter
- pin: GPIO13
- unit_of_measurement: "L/min"
- accuracy_decimals: 0
- update_interval: 1min
- total:
- name: '${friendly_name_1}'
- id: ${name_1}_water_total
- unit_of_measurement: 'L'
- accuracy_decimals: 0
- icon: "mdi:water"
- device_class: water
- state_class: total
- - platform: pulse_counter
- name: '${friendly_name_2} - przepływ'
- id: ${name_2}_water_pulse_counter
- pin: GPIO14
- unit_of_measurement: "L/min"
- accuracy_decimals: 0
- update_interval: 1min
- total:
- name: '${friendly_name_2}'
- id: ${name_2}_water_total
- unit_of_measurement: 'L'
- accuracy_decimals: 0
- icon: "mdi:water"
- device_class: water
- state_class: total
- # Check battery level of 18650 Li-Ion (3.7V) connected to the LOLIN32 by using voltage divider
- # Resistors used: R1=47k and R2=100k, so max Vout is 4,2*R2/(R1+R2) == 2.857 (max 3.3v is allowed)
- # see: https://www.mischianti.org/2019/06/15/voltage-divider-calculator-and-application
- #ESP32: GPIO32 through GPIO39 can be used.
- - platform: adc
- id: battery_voltage
- pin: GPIO34
- attenuation: auto
- name: Battery Voltage
- update_interval: 15min
- accuracy_decimals: 2
- filters:
- - multiply: 1.47 #revert to real voltage
- - delta: 0.1 #send values if changed
- #Convert the Voltage to the battery level (%)
- - platform: copy
- source_id: battery_voltage
- id: battery_level
- icon: "mdi:battery"
- name: Battery Level
- device_class: battery
- unit_of_measurement: '%'
- accuracy_decimals: 1
- filters:
- - calibrate_linear:
- # Map from voltage to Battery level
- - 3.0 -> 0 #min for ESP32
- - 4.2 -> 100 #max for 18650 3,7v battery
- #Handle/cap boundaries
- - lambda: |
- if (x < 0) return 0;
- else if (x > 100) return 100;
- else return (x);
- - delta: 1 #send values if changed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement