Why Not? Logic Apps + Home Assistant to Automate Your Cats Treat Dispenser

Alright, buckle up, folks – we’re diving into the absurd side of automation. Sure, Home Assistant can make your home smarter, but what if it could make your cat happier and financially savvy? Let’s build a system that dispenses treats based on stock market performance. Because why not tie your feline friend’s snack schedule to the whims of Wall Street?

This is overkill. It’s ridiculous. It’s everything you didn’t know you needed.

The Plan: Cats, Treats, and Capitalism

Here’s what we’re doing:

  1. Stock Price Check – Use Azure Logic Apps to pull daily stock prices for your favorite company (or your cat’s favorite, obviously). I prefer something like Alpha Vantage – it’s got a free tier and it’s easy to setup. https://www.alphavantage.co/
  2. Treat Dispensing Logic – Determine whether the stock is up or down and decide if treats should rain upon your furry roomate.
  3. Home Assistant Integration – Trigger a smart treat dispenser via a webhook.

Let’s get started!

If you don’t have a smart treat dispenser, DIY it. A smart plug attached to a motorized dispenser (or a cheap automatic feeder) works perfectly. I prefer these TPLink Matter Plugs. Connect it to Home Assistant and set up an automation:

alias: "Dispense Treats"
trigger:
  - platform: webhook
    webhook_id: "dispense_treats"
action:
  - service: switch.turn_on
    target:
      entity_id: switch.treat_dispenser
  - delay: "00:00:05"  # Dispense treats for 5 seconds
  - service: switch.turn_off
    target:
      entity_id: switch.treat_dispenser

Make sure you note the “webhook_id” – you will need it soon.

Create the Logic App

In Azure Portal, create a new Logic App (Consumption). Call it something fun, like WallStreetKitty. Add a Reoccurrance Trigger – maybe every 5 minutes or something like that. If you use a different API for the stock prices, make sure you know the number of calls you can make on your pricing tier. These sample below just checks once a day at 5pm (after the market closes).

{
  "recurrence": {
    "frequency": "Day",
    "interval": 1,
    "schedule": {
      "hours": [17],
      "minutes": [0]
    }
  }
}

Fetch Stock Prices

Add an HTTP action to call a stock API (e.g., Alpha Vantage). Example setup:

  1. Method: GET
  2. URI: https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=YourAPIKey

Replace MSFT with the ticker symbol of your choice – or your cat’s favorite stock. The actual json you get from Alpha Vantage might be something like this:

{
  "Meta Data": {
    "1. Information": "Intraday (1min) open, high, low, close prices and volume",
    "2. Symbol": "MSFT",
    "3. Last Refreshed": "2024-05-01 16:00:00",
    "4. Interval": "1min",
    "5. Output Size": "Compact",
    "6. Time Zone": "US/Eastern"
  },
  "Time Series (1min)": {
    "2024-05-01 16:00:00": {
      "1. open": "309.6200",
      "2. high": "310.0000",
      "3. low": "309.1500",
      "4. close": "309.9200",
      "5. volume": "1234567"
    },
    "2024-05-01 15:59:00": {
      "1. open": "309.5000",
      "2. high": "309.8000",
      "3. low": "309.1000",
      "4. close": "309.6200",
      "5. volume": "987654"
    }
  }
}

So your next step is to parse the json – add a parse JSON step and use this schema:

{
  "type": "object",
  "properties": {
    "Meta Data": {
      "type": "object",
      "properties": {
        "2. Symbol": { "type": "string" },
        "3. Last Refreshed": { "type": "string" }
      }
    },
    "Time Series (1min)": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "properties": {
          "1. open": { "type": "string" },
          "2. high": { "type": "string" },
          "3. low": { "type": "string" },
          "4. close": { "type": "string" },
          "5. volume": { "type": "string" }
        }
      }
    }
  }
}

We want 4 – the CLOSE value. We just need to extract it from the JSON –

@body('Parse_JSON')?['Time Series (1min)']?[body('Parse_JSON')?['Meta Data']?['3. Last Refreshed']]?['4. close']

Now we need to add a condition – we don’t want Tabby to get fat if the stocks aren’t doing well, right??

@greater(float(body('Extract_Stock_Price')),float(variables('Previous_Price')))

If True – continue. If False, well someones going to not be happy. Let’s assume that the stocks are going to do well, so let’s call out to Home Assistant.

Call Home Assistant

In the True branch, add an HTTP action to trigger the treat dispenser:

  1. Method – POST
  2. URI – https://your-homeassistant-instance/api/webhook/<<that webhook id I told you to remember>>. For example – https://your-homeassistant-instance/api/webhook/dispense_treats
  3. Headers – Add your Home Assistant authentication token.

Bonus Round: Go Big or Go Home

  • Track Multiple Stocks – Add a loop to monitor multiple ticker symbols and dispense extra treats for each one in the green.
  • Add Notifications – Send a Teams message or email with the stock performance and treat status.
  • Cat Mood Analytics – Track treat frequency and correlate it with your cat’s nap times in a Power BI dashboard.

Did we need to do this? Nope. Is this useful? Also No. Do I currently have a cat? Also No. Do I have too much time on my hands? I will let you decide.