Coffee Maker Status

I had been struggling with creating a sensor for my coffee maker that would update its status to show if it was brewing or just keeping the coffee warm, and I finally figured it out and wanted to share it in case someone else needs something similar. I am using a switchbot to turn the coffee maker on and off, and a sonoff plug to measure power usage. The issue I ran into was that the power usage was the same whether or not it was brewing or just keeping it warm. However if it was warming, it would cycle off and on for an hour every 3 minutes or so. So after a day or so of thinking and trial and error I came up with this. I created a threshold helper that showed if the power consumption was above or below 700 and used this template sensor for the status. So if power is below 700 for 5 mins, it’s considered off. If it’s above 700, and the previous state was off for more than 5 mins then it’s brewing, otherwise it’s keeping warm. The keeping warm status should only apply if the helper has not changed to above 700 or below 700 in the last 5 minutes which is perfect because when it’s warming, it usually changes every ~3.

image
This is what the power looks like when its brewing, and then goes into its warming state.

trigger:
  - platform: state
    entity_id: binary_sensor.coffee_maker_power_onoff
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.coffee_maker_power_onoff
    from: 'on'
    to: 'off'
  - platform: state
    entity_id: binary_sensor.coffee_maker_power_onoff
    from: 'on'
    to: 'off'
    for:
      minutes: 5
sensor:
  name: Coffee Maker State
  state: >-
    {% if (is_state('binary_sensor.coffee_maker_power_onoff', 'on')) and (((now().timestamp() - trigger.from_state.last_changed.timestamp()) /60 ) | round(0) >= 5) %}
      Brewing
    {% elif (is_state('binary_sensor.coffee_maker_power_onoff', 'off')) and (((now().timestamp() - trigger.to_state.last_changed.timestamp()) /60 ) | round(0) >= 5) %}
      Off
    {% else %}
      Keep Warm
    {% endif %}
  icon: mdi:coffee-maker-outline
  unique_id: 4xnwmtKQ4tm3LEgv4fcM9jPcJKkCf57

Hope this helps, or at least sparks some ideas for someone. I’d love to do this without the helper, but I couldn’t find a way to get the last_updated attribute to only update if the power changes to above 700 or below. I don’t want it to update if it goes from 769 to 770 for example.

Thanks for this, I’ve been wanting to do the same thing. I was originally looking into a finite state machine, but there doesn’t seem to be an easy way outside of AppDaemon or Pyscript. I’m pretty new to HA and not very fluent in YAML yet. Is your trigger inside of an automation?

no its just a sensor that updates on those triggers