Template to take an average value of another sensor and save it for reference

I’m working on an ESP hydrometer for home brewing, and what I would like to do is show a button in HA that allows the user to take the Original Gravity at the beginning of the batch, then save it to calculate alcohol % later on.

So far, I have
-a button
-which runs a script
-that turns a global boolean on for 60s, then off
-a template sensor that checks if the boolean is on, then returns the Gravity reading, or else returns nothing, or {}
-the template is then filtered using the throttle_average for the same 60s to even out any noise in the gravity reading

the problem I have is that after the global boolean turns off, the throttle_average starts returning NaN, so I added a heartbeat before it. this…works, but it is working through all the template logic and sending the exact same number to HA repeatedly. Its not just kinda kludgy, this is a battery powered device, sealed inside a pressurized vessel full of beer, I’d like to avoid wasted power. Does anyone know of a better way to do this?

here’s the code I have if the word form isn’t clear

globals:
  - id: og_active
    type: bool
    restore_value: yes
    initial_value: 'false'

button:
  - platform: template
    name: "Take Original Gravity Reading"
    on_press:
      - script.execute: OG_Read

script:
  - id: OG_Read
    mode: queued
    then:
      - globals.set:
          id: og_active
          value: 'true'
      - delay: 60s
      - globals.set:
          id: og_active
          value: 'false'

sensor:
  - platform: template
    name: "Original Gravity"
    id: "original_gravity"
    icon: mdi:beaker-check-outline
    lambda: |-
      if (id(og_active)) {
        return id(hydro_gravity).state;
      } else {
        return {};
      }
    filters:
      - heartbeat: 5s
      - throttle_average: 60s

I’d set the original sensor ‘hydro_gravity’ to to take an average over 60 seconds. If not make a template sensor that takes hydro_gravity and averages the value.

If you give the template only an id: and no name: it will be hidden from HA but be available to set the global.

globals:
## Enable set global
  - id: og_set
    type: bool
    restore_value: no
    initial_value: 'false'
## og value for lambda
  - id: og_value
    type: float
    restore_value: yes

switch:
  - platform: template
    name: "og enable"
    id: og_enable
    turn_on_action:
      - globals.set:
        id: og_set
        value: 'true'
    turn_off_action:
      - globals.set:
        id: og_set
        value: 'false'

button:
  - platform: template
    name: "Store og value"
    on_press:
      then:
        - if:
            condition:
              lambda: 'return id(og_set);'
            then:
              - globals.set:
                id: og_value
                value: !lambda 'return id(hydro_gravity).state;'
                delay: 1s
                switch.turn_off: og_enable
            else:
              - logger.log: "OG enable is off!

1 Like

interesting, that does seem a lot simpler, but then it needs another sensor if I want to expose the OG value in HA, maybe I don’t need to. I was trying to avoid having a long integration time for the hydro_gravity value, but I might be able to strike a balance.

Well, it turns out my method doesn’t preserve the OG measurement when the device goes into deep sleep anyway, so I ended up with a mix of the two:

globals:
  - id: original_gravity
    type: float
    restore_value: yes
    initial_value: '0.0'

sensor:
  - platform: template
    id: "precision_gravity"
    internal: true
    update_interval: 5s
    lambda: return id(hydro_gravity).state;
    filters:
    - sliding_window_moving_average:
        window_size: 5
        send_every: 1

button:
  - platform: template
    name: "Save Original Gravity Reading"
    on_press:
      - delay: 30s
      - globals.set:
          id: original_gravity
          value: !lambda |-
            return id(precision_gravity).state;

It doesn’t expose the OG value in HA, but I can always write it in my notes like a caveman, or expose it as another sensor.

I’m glad you got it working. You mentioned that the value was used in calculating the alcohol content. Is that a template sensor in HA? You should be able to add the og percentage as an attribute.

As to the sensor itself that youare getting noisy readings from perhaps this thread might help.