Skip to content

Instantly share code, notes, and snippets.

@ayn
Last active August 26, 2025 04:24
Show Gist options
  • Select an option

  • Save ayn/a8b9a344197ed235a4ac2719d2203340 to your computer and use it in GitHub Desktop.

Select an option

Save ayn/a8b9a344197ed235a4ac2719d2203340 to your computer and use it in GitHub Desktop.
brew by weight automation (micra x lunar x homeasistant)
alias: Brew by Weight (Flow + Offset, v3)
description: Stop pump using (target - offset) OR flow*seconds prediction, with seconds failsafe + retry.
mode: single
triggers:
- trigger: numeric_state
entity_id: sensor.lunar_weight
above: 5
variables:
target_weight: "{{ states('input_number.spro_target_weight') | float(38) }}"
cutoff_seconds: "{{ states('input_number.spro_cutoff_seconds') | float(40) }}"
cutoff_offset: "{{ states('input_number.spro_cutoff_offset') | float(2.5) }}"
cutoff_weight: "{{ target_weight - cutoff_offset }}"
# build a dynamic timeout slightly above your seconds cap
wait_timeout: >
{{ '00:00:%02d' | format((cutoff_seconds | int(40)) + 15) }}
actions:
# stop when earliest condition trips
- wait_for_trigger:
# A) simple offset stop
- trigger: template
value_template: >
{{ (states('sensor.lunar_weight') | float(0)) >= (cutoff_weight | float(0)) }}
for:
seconds: 1
# B) predictive stop: current + flow*seconds >= target
- trigger: template
value_template: >
{% set w = states('sensor.lunar_weight') | float(0) %}
{% set flow = states('sensor.lunar_volume_flow_rate') | float(0) %}
{% set flow_clamped = ([flow, 0] | max) | round(2) %}
{% set flow_capped = [flow_clamped, 4.0] | min %}
{% set projected = w + (flow_capped * (cutoff_seconds | float(40))) %}
{{ projected >= (target_weight | float(38)) }}
for:
seconds: 1
timeout: "{{ wait_timeout }}"
continue_on_timeout: true
# Send STOP regardless (either one trigger fired or we hit timeout)
- action: switch.turn_off
target:
entity_id: switch.micra_pump
# Verify it’s stopping; if weight is still clearly rising, retry once
- delay: "00:00:01"
- variables:
w_before: "{{ states('sensor.lunar_weight') | float(0) }}"
- delay: "00:00:01"
- variables:
w_after: "{{ states('sensor.lunar_weight') | float(0) }}"
- choose:
- conditions:
- condition: template
value_template: "{{ (w_after - w_before) > 0.4 }}"
sequence:
- action: switch.turn_off
target:
entity_id: switch.micra_pump
# small settle so the logged final weight is closer to reality
- delay: "00:00:02"
@ayn
Copy link
Author

ayn commented Apr 9, 2025

the brewing_active condition has been disabled as that sensor is unavailable since new LM app/gw firmware.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment