Matt Horan's Blog

Coffee Bean Counter

As I wrote in my last post about setting up a remote door buzzer, Shelly’s WiFi-enabled relays caused me to catch the home automation bug. In addition to the relay used for that project, I picked up a Shelly 1PM relay (and bought four more since then!) A fellow espresso enthusiast told me about their Home Assistant setup to estimate how many espresso shots they could pull before needing to restock on beans. Having limited space at my coffee station, I wanted something compact. I’ve been using TPLink Kasa devices with power metering elsewhere in my home, but the Shelly 1PM was the prefect choice for this project since it could be tucked away in the junction box.

Espresso machine and grounder on counter

The goal of this project was to leverage the power metering functionality of the Shelly 1PM to decrement a counter when the coffee grinder has run. I would reset the counter whenever I picked up a new bag of beans. I first attempted to set this up with a Kasa duplex outlet, but unfortunately I found out after the fact that there was no way to determine when the outlet was in use without power metering functionality. That’s when I picked up a Shelly 1PM, which ended up being perfect for the job.

My coffee station has a duplex outlet. Normally an outlet is wired with a single neutral and hot wire. Outlets typically have two sets of terminals on the neutral and hot sides, which are bonded together and can be used to extend the circuit to other receptacles. However, so long as you aren’t using the second set of terminals for that purpose, there’s a small tab that can be broken off of the (hot or neutral) terminal. This allows the outlets to be controlled (and in my case, metered) independently. That’s exactly what I wanted since I didn’t want the other outlet to decrement my counter (the other outlet powers the espresso machine, which uses a lot more power than the grinder!)

Removable tab on the hot side of an outlet

One thing I spent an inordinate amount of time looking into was whether there would be any issue using the Shelly 1PM on a 20 amp circuit. While the outlet supplying the coffee grinder is a 15 amp NEMA 15-R socket, it is on a 20 amp circuit behind a GFCI on the other side of my kitchen. The terminals on Shelly relays can only handle up to 16 amps, and officially only support 14 gauge wire. 20 amp circuits are wired with 12 gauge wire, and you’re not allowed to down size the wire inside a junction box on a 20 amp circuit.

The first thought I had was to install an inline fuse in the junction box to allow the use of 14 gauge wire and also eliminate the concerns of using a 16 amp relay on a 20 amp circuit. However, once I had the Shelly 1PM in hand, I found that if I was very careful, it could accommodate 12 gauge wire. NEC 404.14(F) addresses the latter concern, so long as the control device is used to control not more than one receptacle and the rating of the control device is not less than the rating of the receptacle.

The Shelly relay requires both neutral and hot in order to power itself, so I left the neutral tab connected on the outlet to simplify running neutral to the relay. However, I broke off the tab on the hot side of the outlet so I could hook up the relay to the bottom outlet and leave the top outlet connected directly to the branch circuit. I bought a Wago connector to wire the hot to the outlet and the relay. I left neutral wired directly to the outlet. The Shelly relay has a switched (and metered) output and which was wired to the bottom outlet terminal.

Outlet and Shelly 1PM relay installed in junction box

With all the wiring in place it was time to boot up the relay. Getting started with Shelly devices is super easy and covered in my previous post. However, if you haven’t read that one yet, you just need to connect a device to the WiFi network created by the relay, and either navigate to http://192.168.33.1 in a browser or use the Shelly app. You can then connect it to another WiFi network and you’ll be good to go.

For this use case it’s important to make sure things are configured properly on the relay itself. First, as with the door opener, I had to change the switch mode to keep state. Then I turned the relay on, and intended to leave it on – since I’m only using the power metering functionality of the Shelly 1PM. Also, I found that it was imperative to enable unicast CoIoT for this purpose. Otherwise, Home Assistant did not reliably receive power metering events.

With the relay itself configured, it was time to set everything up in Home Assistant.

First, I created two “helpers”: a counter indicating the number of espresso shots left, and a counter for the total number of shots ground ever. The counters are named counter.espresso_shots_left and counter.espresso_shots_ground_ever.

With the counters in place it was time to wire it all together with an automation. I created an automation that watched for Shelly 1PM power metering events as follows. It waits for the power to be above 10 watts for 10 seconds before decrementing the espresso_shots_left counter and incrementing the espresso_shots_ground_ever counter.

  alias: Espresso shot was ground
  description: ''
  trigger:
  - type: power
    platform: device
    device_id: bb0561e3cbb768a70842af1b402852fe
    entity_id: sensor.shelly1pm_8caab55f3484_power
    domain: sensor
    above: 10
    for:
      hours: 0
      minutes: 0
      seconds: 10
  condition: []
  action:
  - service: counter.decrement
    target:
      entity_id: counter.espresso_shots_left
    data: {}
  - service: counter.increment
    target:
      entity_id: counter.espresso_shots_ground_ever
    data: {}
  mode: single

Finally, I added a script that reset the counter based on the number of shots in a single bag of beans. I estimated this to be 19 shots per 12 ounce bag and so far this has held up well.

  alias: Add 12oz espresso beans
  sequence:
  - service: counter.configure
    target:
      entity_id: counter.espresso_shots_left
    data:
      step: 19
  - service: counter.increment
    target:
      entity_id: counter.espresso_shots_left
    data: {}
  - service: counter.configure
    target:
      entity_id: counter.espresso_shots_left
    data:
      step: 1
  mode: single

This project was a lot of fun to build and in the month or so since I set it up, I haven’t run out of beans once! Every time I hit the “Add 12oz espresso beans” button I can’t help but laugh.