Advertisement
GrzegorzM

ESPHome - save/restore counters for restart

Feb 17th, 2023 (edited)
1,495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 4.43 KB | Source Code | 0 0
  1. substitutions:
  2.   name_1: main
  3.   friendly_name_1: "Licznik główny"
  4.   name_2: garden
  5.   friendly_name_2: "Licznik ogrodowy"
  6.  
  7. esphome:
  8.   name: esp-woda
  9.   comment: Odczyt liczników wody
  10.   on_boot:
  11.     then:
  12.       - script.execute: restore_states
  13.   on_shutdown:
  14.     then:
  15.       - script.execute: backup_states
  16.      
  17. # Enable Home Assistant API
  18. api:
  19.   services:
  20.     - service: backup_data
  21.       then:
  22.         - script.execute: backup_states
  23.     - service: restore_data
  24.       then:
  25.         - script.execute: restore_states
  26.     - service: set_${name_1}_water_counter
  27.       variables:
  28.         new_total: int
  29.       then:
  30.         - pulse_counter.set_total_pulses:
  31.             id: ${name_1}_water_pulse_counter
  32.             value: !lambda 'return new_total;'
  33.     - service: set_${name_2}_water_counter
  34.       variables:
  35.         new_total: int
  36.       then:
  37.         - pulse_counter.set_total_pulses:
  38.             id: ${name_2}_water_pulse_counter
  39.             value: !lambda 'return new_total;'
  40.            
  41. #################################################################
  42. # Globals
  43. globals:
  44.   - id: ${name_1}_water_total_backup
  45.     type: int
  46.     restore_value: true
  47.   - id: ${name_2}_water_total_backup
  48.     type: int
  49.     restore_value: true
  50.  
  51. #################################################################
  52. # Scripts
  53. script:
  54. - id: backup_states
  55.   then:
  56.     - globals.set:
  57.         id: ${name_1}_water_total_backup
  58.         value: !lambda 'return id(${name_1}_water_total).state;'
  59.     - globals.set:
  60.         id: ${name_2}_water_total_backup
  61.         value: !lambda 'return id(${name_2}_water_total).state;'
  62. - id: restore_states
  63.   then:
  64.     - pulse_counter.set_total_pulses:
  65.         id: ${name_1}_water_pulse_counter
  66.         value: !lambda 'return id(${name_1}_water_total_backup);'
  67.     - pulse_counter.set_total_pulses:
  68.         id: ${name_2}_water_pulse_counter
  69.         value: !lambda 'return id(${name_2}_water_total_backup);'
  70.  
  71. #################################################################
  72. # Buttons
  73. button:
  74.   - platform: template
  75.     name: "Backup counters"
  76.     icon: "mdi:content-save-all"
  77.     on_press:
  78.       - script.execute: backup_states
  79.  
  80.   - platform: template
  81.     name: "Restore backup"
  82.     icon: "mdi:backup-restore"
  83.     on_press:
  84.       - script.execute: restore_states
  85. #################################################################
  86. # Sensors
  87. sensor:
  88.   - platform: pulse_counter
  89.     name: '${friendly_name_1} - przepływ'
  90.     id: ${name_1}_water_pulse_counter
  91.     pin: GPIO13
  92.     unit_of_measurement: "L/min"
  93.     accuracy_decimals: 0
  94.     update_interval: 1min
  95.     total:
  96.       name: '${friendly_name_1}'
  97.       id: ${name_1}_water_total
  98.       unit_of_measurement: 'L'
  99.       accuracy_decimals: 0
  100.       icon: "mdi:water"
  101.       device_class: water
  102.       state_class: total
  103.  
  104.   - platform: pulse_counter
  105.     name: '${friendly_name_2} - przepływ'
  106.     id: ${name_2}_water_pulse_counter
  107.     pin: GPIO14
  108.     unit_of_measurement: "L/min"
  109.     accuracy_decimals: 0
  110.     update_interval: 1min
  111.     total:
  112.       name: '${friendly_name_2}'
  113.       id: ${name_2}_water_total
  114.       unit_of_measurement: 'L'
  115.       accuracy_decimals: 0
  116.       icon: "mdi:water"
  117.       device_class: water
  118.       state_class: total
  119.      
  120. # Check battery level of 18650 Li-Ion (3.7V) connected to the LOLIN32 by using voltage divider
  121. # Resistors used: R1=47k and R2=100k, so max Vout is 4,2*R2/(R1+R2) == 2.857 (max 3.3v is allowed)
  122. # see: https://www.mischianti.org/2019/06/15/voltage-divider-calculator-and-application
  123. #ESP32: GPIO32 through GPIO39 can be used.
  124.   - platform: adc
  125.     id: battery_voltage
  126.     pin: GPIO34
  127.     attenuation: auto
  128.     name: Battery Voltage
  129.     update_interval: 15min
  130.     accuracy_decimals: 2
  131.     filters:
  132.       - multiply: 1.47 #revert to real voltage
  133.       - delta: 0.1 #send values if changed
  134. #Convert the Voltage to the battery level (%)
  135.   - platform: copy
  136.     source_id: battery_voltage
  137.     id: battery_level
  138.     icon: "mdi:battery"
  139.     name: Battery Level
  140.     device_class: battery
  141.     unit_of_measurement: '%'
  142.     accuracy_decimals: 1
  143.     filters:
  144.       - calibrate_linear:
  145.      # Map from voltage to Battery level
  146.           - 3.0 -> 0 #min for ESP32
  147.           - 4.2 -> 100 #max for 18650 3,7v battery
  148.       #Handle/cap boundaries
  149.       - lambda: |
  150.          if (x < 0) return 0;
  151.           else if (x > 100) return 100;
  152.           else return (x);
  153.       - delta: 1 #send values if changed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement