{"id":703,"date":"2024-05-10T01:05:54","date_gmt":"2024-05-10T01:05:54","guid":{"rendered":"https:\/\/draith.com\/?p=703"},"modified":"2025-01-05T02:04:18","modified_gmt":"2025-01-05T02:04:18","slug":"why-not-logic-apps-home-assistant-to-automate-your-cats-treat-dispenser","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=703","title":{"rendered":"Why Not?  Logic Apps + Home Assistant to Automate Your Cats Treat Dispenser"},"content":{"rendered":"\n<p>Alright, buckle up, folks &#8211; we\u2019re 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&#8217;s build a system that dispenses treats based on stock market performance. Because why not tie your feline friend\u2019s snack schedule to the whims of Wall Street?<\/p>\n\n\n\n<p>This is overkill. It\u2019s ridiculous. It\u2019s everything you didn\u2019t know you needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The Plan: Cats, Treats, and Capitalism<\/strong><\/h2>\n\n\n\n<p>Here\u2019s what we\u2019re doing:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Stock Price Check<\/strong> &#8211; Use Azure Logic Apps to pull daily stock prices for your favorite company (or your cat\u2019s favorite, obviously).  I prefer something like Alpha Vantage &#8211; it&#8217;s got a free tier and it&#8217;s easy to setup.  <a href=\"https:\/\/www.alphavantage.co\/\">https:\/\/www.alphavantage.co\/<\/a><\/li>\n\n\n\n<li><strong>Treat Dispensing Logic<\/strong> &#8211; Determine whether the stock is up or down and decide if treats should rain upon your furry roomate.<\/li>\n\n\n\n<li><strong>Home Assistant Integration<\/strong> &#8211; Trigger a smart treat dispenser via a webhook.<\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s get started!<\/p>\n\n\n\n<p>If you don\u2019t 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 <a href=\"https:\/\/www.tp-link.com\/us\/home-networking\/smart-plug\/tapo-p110m\/\">TPLink Matter Plugs.<\/a>  Connect it to Home Assistant and set up an automation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alias: \"Dispense Treats\"\ntrigger:\n  - platform: webhook\n    webhook_id: \"dispense_treats\"\naction:\n  - service: switch.turn_on\n    target:\n      entity_id: switch.treat_dispenser\n  - delay: \"00:00:05\"  # Dispense treats for 5 seconds\n  - service: switch.turn_off\n    target:\n      entity_id: switch.treat_dispenser\n<\/code><\/pre>\n\n\n\n<p>Make sure you note the &#8220;webhook_id&#8221; &#8211; you will need it soon.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Create the Logic App<\/strong><\/h3>\n\n\n\n<p>In Azure Portal, create a new <strong>Logic App (Consumption)<\/strong>. Call it something fun, like <em>WallStreetKitty<\/em>.  Add a Reoccurrance Trigger &#8211; 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).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"recurrence\": {\n    \"frequency\": \"Day\",\n    \"interval\": 1,\n    \"schedule\": {\n      \"hours\": &#91;17],\n      \"minutes\": &#91;0]\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Fetch Stock Prices<\/strong><\/h3>\n\n\n\n<p>Add an <strong>HTTP<\/strong> action to call a stock API (e.g., Alpha Vantage). Example setup:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Method<\/strong>: GET<\/li>\n\n\n\n<li><strong>URI<\/strong>: <a href=\"https:\/\/www.alphavantage.co\/query?function=TIME_SERIES_INTRADAY&amp;symbol=MSFT&amp;interval=1min&amp;apikey=YourAPIKey\">https:\/\/www.alphavantage.co\/query?function=TIME_SERIES_INTRADAY&amp;symbol=MSFT&amp;interval=1min&amp;apikey=YourAPIKey<br><\/a><\/li>\n<\/ol>\n\n\n\n<p>Replace MSFT with the ticker symbol of your choice &#8211; or your cat\u2019s favorite stock. The actual json you get from Alpha Vantage might be something like this:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"Meta Data\": {\n    \"1. Information\": \"Intraday (1min) open, high, low, close prices and volume\",\n    \"2. Symbol\": \"MSFT\",\n    \"3. Last Refreshed\": \"2024-05-01 16:00:00\",\n    \"4. Interval\": \"1min\",\n    \"5. Output Size\": \"Compact\",\n    \"6. Time Zone\": \"US\/Eastern\"\n  },\n  \"Time Series (1min)\": {\n    \"2024-05-01 16:00:00\": {\n      \"1. open\": \"309.6200\",\n      \"2. high\": \"310.0000\",\n      \"3. low\": \"309.1500\",\n      \"4. close\": \"309.9200\",\n      \"5. volume\": \"1234567\"\n    },\n    \"2024-05-01 15:59:00\": {\n      \"1. open\": \"309.5000\",\n      \"2. high\": \"309.8000\",\n      \"3. low\": \"309.1000\",\n      \"4. close\": \"309.6200\",\n      \"5. volume\": \"987654\"\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>So your next step is to parse the json &#8211; add a parse JSON step and use this schema:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"type\": \"object\",\n  \"properties\": {\n    \"Meta Data\": {\n      \"type\": \"object\",\n      \"properties\": {\n        \"2. Symbol\": { \"type\": \"string\" },\n        \"3. Last Refreshed\": { \"type\": \"string\" }\n      }\n    },\n    \"Time Series (1min)\": {\n      \"type\": \"object\",\n      \"additionalProperties\": {\n        \"type\": \"object\",\n        \"properties\": {\n          \"1. open\": { \"type\": \"string\" },\n          \"2. high\": { \"type\": \"string\" },\n          \"3. low\": { \"type\": \"string\" },\n          \"4. close\": { \"type\": \"string\" },\n          \"5. volume\": { \"type\": \"string\" }\n        }\n      }\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>We want 4 &#8211; the CLOSE value.  We just need to extract it from the JSON &#8211; <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@body('Parse_JSON')?&#91;'Time Series (1min)']?&#91;body('Parse_JSON')?&#91;'Meta Data']?&#91;'3. Last Refreshed']]?&#91;'4. close']<\/code><\/pre>\n\n\n\n<p>Now we need to add a condition &#8211; we don&#8217;t want Tabby to get fat if the stocks aren&#8217;t doing well, right??<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@greater(float(body('Extract_Stock_Price')),float(variables('Previous_Price')))<\/code><\/pre>\n\n\n\n<p>If True &#8211; continue.  If False, well someones going to not be happy.  Let&#8217;s assume that the stocks are going to do well, so let&#8217;s call out to Home Assistant.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Call Home Assistant<\/strong><\/h3>\n\n\n\n<p>In the <strong>True<\/strong> branch, add an <strong>HTTP<\/strong> action to trigger the treat dispenser:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Method<\/strong> &#8211; POST<\/li>\n\n\n\n<li><strong>URI<\/strong> &#8211; https:\/\/your-homeassistant-instance\/api\/webhook\/&lt;&lt;that webhook id I told you to remember>>.  For example &#8211; <a href=\"https:\/\/your-homeassistant-instance\/api\/webhook\/dispense_treats\">https:\/\/your-homeassistant-instance\/api\/webhook\/dispense_treats<\/a><\/li>\n\n\n\n<li><strong>Headers<\/strong> &#8211; Add your Home Assistant authentication token.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Bonus Round: Go Big or Go Home<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Track Multiple Stocks<\/strong> &#8211; Add a loop to monitor multiple ticker symbols and dispense extra treats for each one in the green.<\/li>\n\n\n\n<li><strong>Add Notifications<\/strong> &#8211;  Send a Teams message or email with the stock performance and treat status.<\/li>\n\n\n\n<li><strong>Cat Mood Analytics<\/strong> &#8211; Track treat frequency and correlate it with your cat\u2019s nap times in a Power BI dashboard.<\/li>\n<\/ul>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Alright, buckle up, folks &#8211; we\u2019re 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&#8217;s build a system that dispenses treats based on stock market performance. Because why not tie your feline friend\u2019s snack schedule to &hellip; <a href=\"https:\/\/draith.com\/?p=703\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Why Not?  Logic Apps + Home Assistant to Automate Your Cats Treat Dispenser&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,29,13,24],"class_list":["post-703","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-azure","tag-home-asssistant","tag-logic-apps","tag-why-not"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=703"}],"version-history":[{"count":4,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/703\/revisions"}],"predecessor-version":[{"id":707,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/703\/revisions\/707"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}