Files
AI-on-the-edge-device-docs/Integration-Home-Assistant/index.html
2023-02-03 23:19:57 +00:00

426 lines
22 KiB
HTML

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><link rel="canonical" href="https://jomjol.github.io/AI-on-the-edge-device-docs/Integration-Home-Assistant/" />
<link rel="shortcut icon" href="../img/favicon.ico" />
<title>Integration into Home Assistant - AI on the Edge Device</title>
<link rel="stylesheet" href="../css/theme.css" />
<link rel="stylesheet" href="../css/theme_extra.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/github.min.css" />
<script>
// Current page data
var mkdocs_page_name = "Integration into Home Assistant";
var mkdocs_page_input_path = "Integration-Home-Assistant.md";
var mkdocs_page_url = "/AI-on-the-edge-device-docs/Integration-Home-Assistant/";
</script>
<script src="../js/jquery-3.6.0.min.js" defer></script>
<!--[if lt IE 9]>
<script src="../js/html5shiv.min.js"></script>
<![endif]-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<img src=https://jomjol.github.io/AI-on-the-edge-device-docs//img/logo.png>
<a href=".." class="icon icon-home"> AI on the Edge Device
</a><div role="search">
<form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" title="Type search term here" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption"><span class="caption-text">Getting Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="..">Welcome</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Installation/">Installation</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../initial-setup/">Initial Setup</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Hardware-Compatibility/">Hardware Compatibility</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Best-Practice/">Best Practice</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../FAQs/">Frequently Asked Questions</a>
</li>
</ul>
<p class="caption"><span class="caption-text">External Links</span></p>
<ul>
<li class="toctree-l1"><a class="" href="https://github.com/jomjol/AI-on-the-edge-device/releases">Releases</a>
</li>
<li class="toctree-l1"><a class="" href="https://jomjol.github.io/AI-on-the-edge-device/index.html">Web Installer/Console</a>
</li>
<li class="toctree-l1"><a class="" href="https://github.com/jomjol/AI-on-the-edge-device/issues">Place an Issue</a>
</li>
<li class="toctree-l1"><a class="" href="https://github.com/jomjol/AI-on-the-edge-device/discussions">Join a Discussion</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Configuration</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../ROI-Configuration/">ROI (Region of Interest)</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Choosing-the-Model/">Model Selection</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Neural-Network-Types/">Neural Network Types</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../ota/">Over-The-Air (OTA) Update</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Parameters/">Parameters</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Advanced</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal current" href="./">Integration into Home Assistant</a>
<ul class="current">
<li class="toctree-l2"><a class="reference internal" href="#using-mqtt-automatically-setup-entities-using-home-assistant-mqtt-discovery">Using MQTT (Automatically Setup Entities using Home Assistant MQTT Discovery)</a>
<ul>
<li class="toctree-l3"><a class="reference internal" href="#using-mqtt-manually-setup-entities">Using MQTT (Manually Setup Entities)</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#examples">Examples</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#statistics-graph">Statistics Graph</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#influxdb-graphs">InfluxDb Graphs</a>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#using-rest">Using REST</a>
<ul>
<li class="toctree-l3"><a class="reference internal" href="#photo">Photo</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../External-LED/">External LED</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../rolling-installation/">Living on the Edge</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Configuration-Parameter-Details/">Configuration Parameter Details</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Configuration/">Configuration</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Watermeter-specific-analog---digital-transition/">Analog/Digital Transition on Water Meters</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../collect-new-images/">Collect images to improve the models</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Learn-models-with-your-own-images/">Learn a model with your own images</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Correction%20Algorithm/">Correction Algorithm</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Additional-Information/">Additional Information</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../New-Releases-Notification/">Notification about new Releases</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Troubleshooting</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../Error-Codes/">Error Codes</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Error-Debugging/">Error Debugging</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Frequent-Reboots/">Frequent Reboots</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Demo-Mode/">Demo Mode</a>
</li>
</ul>
<p class="caption"><span class="caption-text">API's</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../REST-API/">REST API</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../MQTT-API/">MQTT API</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Influx-DB/">Influx DB</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Development</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../Build-Instructions/">Build the Project</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Upload-files-by-script/">Scripted File Upload</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Testing/">Testing</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Release-creation/">Preparing for Release</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Old Pages (no longer maintained)</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../outdated--Integrated-Functions/">Integrated Functions</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../outdated--Gasmeter-Log-Downloader/">Gas Meter Log Downloader</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../outdated--Migrate-Old-Config-To-New-Config/">Migration from water-meter „old“ to water-meter “AI-on-the-edge-device”</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Assorted Pages</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../Graphical-Configuration/">Graphical Configuration</a>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="..">AI on the Edge Device</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href=".." class="icon icon-home" alt="Docs"></a> &raquo;</li>
<li>Advanced &raquo;</li>
<li>Integration into Home Assistant</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/jomjol/AI-on-the-edge-device-docs/edit/main/docs/Integration-Home-Assistant.md" class="icon icon-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div class="section" itemprop="articleBody">
<h1 id="integration-into-home-assistant">Integration into Home Assistant</h1>
<p>There are 3 ways to get the data into your Home Assistant:</p>
<ol>
<li>Using MQTT (Automatically Setup Entities using Home Assistant MQTT Discovery)</li>
<li>Using MQTT (Manually Setup Entities)</li>
<li>Using REST calls</li>
</ol>
<p>The first one is the easier way if you already have MQTT in use.</p>
<h2 id="using-mqtt-automatically-setup-entities-using-home-assistant-mqtt-discovery">Using MQTT (Automatically Setup Entities using Home Assistant MQTT Discovery)</h2>
<p>‼️ This feature will be available with the next release!</p>
<p>Starting with Version <code>&gt;12.0.1</code>, AI-on-the-edge-devices support Home Assistant Discovery.</p>
<ol>
<li>Check <a href="https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery">here</a> to learn more about it and how to enable it in Homeassistant.</li>
<li>
<p>You also have to enable it in the MQTT settings of your device:</p>
<p><img alt="grafik" src="https://user-images.githubusercontent.com/1783586/199350781-e2a59eeb-b5bb-407b-9c0d-2aafab50daab.png" /></p>
<p>Make sure to select the right Meter Type to get the right units!</p>
</li>
</ol>
<p>On the next start of the device, it will send discovery topics and Home Assistant should pick them up and show them under <code>Settings &gt; Integrations &gt; MQTT</code>:</p>
<p><img alt="grafik" src="https://user-images.githubusercontent.com/1783586/199352538-ddcc3484-39ef-44f4-a853-53286807d30b.png" />
<img alt="grafik" src="https://user-images.githubusercontent.com/1783586/199352565-9b0ade28-cb43-47b4-821f-7909cad41a73.png" />
<img alt="grafik" src="https://user-images.githubusercontent.com/1783586/199352619-217df627-4b87-4fa0-86a2-f5347c452fdb.png" /></p>
<h3 id="using-mqtt-manually-setup-entities">Using MQTT (Manually Setup Entities)</h3>
<p>First make sure with an MQTT client (for example <a href="http://mqtt-explorer.com/">MQTT Explorer</a>) that MQTT works as expected and to get a list of the available topics!</p>
<p>Then add a sensor for each property:</p>
<pre><code class="language-yaml">mqtt:
sensor:
- state_topic: &quot;wasserzaehler/main/value&quot;
name: &quot;Watermeter Value&quot;
unique_id: watermeter_value
unit_of_measurement: 'm³'
state_class: total_increasing
device_class: water # Needs Home Assistant 2022.11!
icon: 'mdi:water-pump'
availability_topic: wasserzaehler/connection
payload_available: connected
payload_not_available: connection lost
- state_topic: &quot;wasserzaehler/main/rate&quot;
name: &quot;Watermeter Rate&quot;
unique_id: watermeter_rate
unit_of_measurement: 'm³/min'
state_class: measurement
device_class: water # Needs Home Assistant 2022.11!
icon: 'mdi:water-pump'
availability_topic: wasserzaehler/connection
payload_available: connected
payload_not_available: connection lost
- state_topic: &quot;wasserzaehler/main/error&quot;
name: &quot;Watermeter Error&quot;
unique_id: watermeter_error
icon: &quot;mdi:water-alert&quot;
availability_topic: wasserzaehler/connection
payload_available: connected
payload_not_available: connection lost
- state_topic: &quot;wasserzaehler/uptime&quot;
name: &quot;Watermeter Uptime&quot;
unique_id: watermeter_uptime
unit_of_measurement: 's'
state_class: measurement
device_class: duration
entity_category: diagnostic
icon: &quot;mdi:timer-outline&quot;
availability_topic: wasserzaehler/connection
payload_available: connected
payload_not_available: connection lost
</code></pre>
<p>If you run the discovery once, you can also extract the information from there (MQTT Info, untested):</p>
<pre><code class="language-yaml">mqtt: # Extracted form the Discovery but untested!
sensor:
- name: Value
unique_id: wasserzaehler-main_value
icon: mdi:gauge
state_topic: wasserzaehler/main/value
unit_of_measurement: m³
device_class: water
state_class: total_increasing
availability_topic: wasserzaehler/connection
payload_available: connected
payload_not_available: connection lost
</code></pre>
<p>If you want to convert the <code></code> to <code>l</code>, use a template sensor:</p>
<pre><code class="language-yaml">template:
- sensor:
- name: &quot;Watermeter in l&quot;
unique_id: watermeter_in_l
icon: &quot;mdi:gauge&quot;
state: &quot;{{ states('sensor.watermeter_value')|float(default=0) * 1000 }}&quot; # Convert 1 m3 =&gt; 1000 l
unit_of_measurement: l
availability: &quot;{{ states('sensor.watermeter_value') not in ['unknown', 'unavailable', 'none'] }}&quot;
</code></pre>
<p>If you you want to have the consumption per day, you can use an <a href="https://www.home-assistant.io/integrations/utility_meter/">Utility Meter</a>.
it is a helper and can be used to reset the total increasing values once a day</p>
<pre><code class="language-yaml">utility_meter:
utility_meter_gas_per_day:
source: sensor.gasmeter_value
cycle: daily
utility_meter_water_per_day:
source: sensor.watermeter_value
cycle: daily
</code></pre>
<p>Note that you also can add it using the UI.</p>
<h3 id="examples">Examples</h3>
<p><img alt="grafik" src="https://user-images.githubusercontent.com/1783586/193472069-4135736e-e63a-4afb-8009-5b97aa5c9ac5.png" /></p>
<p><img alt="grafik" src="https://user-images.githubusercontent.com/1783586/193472091-1484aac4-ddc2-48ba-896c-28370963fc2d.png" /></p>
<h3 id="statistics-graph">Statistics Graph</h3>
<p>Creating Statistics Graphs (e.g. usage per day) is easy using the <a href="https://www.home-assistant.io/home-energy-management/">Energy Dashboard</a>:
<img alt="grafik" src="https://user-images.githubusercontent.com/1783586/193471893-d8ab8f5f-0906-4076-8926-8b5a69a24bce.png" /></p>
<p>Note that there seems to be a bug in the graph, see <a href="https://github.com/home-assistant/frontend/issues/13995">https://github.com/home-assistant/frontend/issues/13995</a>!</p>
<h3 id="influxdb-graphs">InfluxDb Graphs</h3>
<p>See also <a href="../Influx-DB">Influx-DB</a>.</p>
<p>If you have setup InfluxDB already, it is also possible to fetch statistics from there, e.g. daily usage:</p>
<pre><code>from(bucket: &quot;HomeAssistant&quot;)
|&gt; range(start: v.timeRangeStart, stop: v.timeRangeStop)
|&gt; filter(fn: (r) =&gt; r[&quot;entity_id&quot;] == &quot;wasserverbrauch_tag&quot;)
|&gt; filter(fn: (r) =&gt; r[&quot;_field&quot;] == &quot;value&quot;)
|&gt; timeShift(duration: -1d)
|&gt; aggregateWindow(every: 1d, fn: max, createEmpty: false)
|&gt; yield(name: &quot;mean&quot;)
</code></pre>
<p><img alt="grafik" src="https://user-images.githubusercontent.com/1783586/193473347-c81fc301-c52f-4af0-9fcb-56fab12cacac.png" /></p>
<h2 id="using-rest">Using REST</h2>
<p>When using REST, Home Assistant has to periodically call an URL on the ESP32 which in return provides the requested data.</p>
<p>See <a href="../REST-API">REST API</a> for a list of available URLs.</p>
<p>The most practical one is the <code>json</code> entrypoint which provides the most relevant data JSON formatted:
<code>http://&lt;IP&gt;/json</code>
This would return:</p>
<pre><code class="language-JSON">{
&quot;main&quot;:
{
&quot;value&quot;: &quot;512.3020&quot;,
&quot;raw&quot;: &quot;0512.3020&quot;,
&quot;error&quot;: &quot;no error&quot;,
&quot;rate&quot;: 0.000000,
&quot;timestamp&quot;: &quot;2022-10-02T20:32:06&quot;
[..]
}
}
</code></pre>
<p>To do such a REST call, you need to create a REST sensor:</p>
<pre><code class="language-yaml">sensor:
- platform: rest
name: &quot;Gasmeter JSON&quot;
resource: http://&lt;IP&gt;/json
json_attributes:
- main
value_template: '{{ value_json.value }}'
headers:
Content-Type: application/json
scan_interval: 60
template:
sensor:
- name: &quot;Gasmeter Value from JSON&quot;
unique_id: gas_meter_value_from_json
state: &quot;{{ state_attr('sensor.gasmeter_json','main')['value'] }}&quot;
unit_of_measurement: 'm³'
- name: &quot;Watermeter Value from JSON&quot;
unique_id: water_meter_value_from_json
state: &gt;-
{{ state_attr('sensor.watermeter_json','main')['value'] | float }}
unit_of_measurement: 'm³'
device_class: water
state_class: total_increasing
icon: mdi:gauge
</code></pre>
<p>See also https://community.home-assistant.io/t/rest-sensor-nested-json/243420/9</p>
<h4 id="photo">Photo</h4>
<p>REST can also be used to show the photo of the last round:</p>
<p><img alt="grafik" src="https://user-images.githubusercontent.com/1783586/193546075-b247942f-9106-47a4-a64b-42ff96dd9078.png" /></p>
<p>To access it, use <code>http://&lt;IP&gt;/img_tmp/alg_roi.jpg</code> resp <code>http://&lt;IP&gt;/img_tmp/raw.jpg</code>.</p>
</div>
</div><footer>
<div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation">
<a href="../Parameters/" class="btn btn-neutral float-left" title="Parameters"><span class="icon icon-circle-arrow-left"></span> Previous</a>
<a href="../External-LED/" class="btn btn-neutral float-right" title="External LED">Next <span class="icon icon-circle-arrow-right"></span></a>
</div>
<hr/>
<div role="contentinfo">
<!-- Copyright etc -->
</div>
Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<div class="rst-versions" role="note" aria-label="Versions">
<span class="rst-current-version" data-toggle="rst-current-version">
<span>
<a href="https://github.com/jomjol/AI-on-the-edge-device-docs" class="fa fa-github" style="color: #fcfcfc"> GitHub</a>
</span>
<span><a href="../Parameters/" style="color: #fcfcfc">&laquo; Previous</a></span>
<span><a href="../External-LED/" style="color: #fcfcfc">Next &raquo;</a></span>
</span>
</div>
<script>var base_url = '..';</script>
<script src="../js/theme_extra.js" defer></script>
<script src="../js/theme.js" defer></script>
<script src="../search/main.js" defer></script>
<script defer>
window.onload = function () {
SphinxRtdTheme.Navigation.enable(true);
};
</script>
</body>
</html>