{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from bayes_sensor import *\n", "from requests import get, post\n", "import json\n", "from pprint import PrettyPrinter\n", "from config import VALID_CONFIG\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "from pprint import pprint\n", "DEFAULT_PROBABILITY_THRESHOLD = 0.5\n", "\n", "def print_json(json_data):\n", " PrettyPrinter().pprint(json_data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I wish to describe how the bayesian_sensor operates, lets do an investigation\n", "\n", "### Code references\n", "* https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/binary_sensor/bayesian.py code\n", "* https://home-assistant.io/components/binary_sensor.bayesian/ docs\n", "* https://github.com/jlmcgehee21/smart_hass#binary-bayes-introspection HA sensor author script for working with bayes sensor \n", "* https://github.com/home-assistant/home-assistant/tree/a1f238816b6130aee2ac88fe9da54ba8f65225f3 Very early home-assistant commit to better understand HA architechture\n", "\n", "\n", "### Bayes references\n", "* https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python/blob/master/02-Discrete-Bayes.ipynb Recommended reading\n", "* https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers another good book\n", "* https://en.wikipedia.org/wiki/Bayes%27_theorem wikipedia on Bayes theorem\n", "* https://en.wikipedia.org/wiki/Bayesian_inference wikipedia on Bayesian inference\n", "\n", "### The Bayesian sensor\n", "\n", "So you've heard about the [bayesian sensor](https://home-assistant.io/components/binary_sensor.bayesian/) in home-assistant, and would like to know what it's good for and how to use it. The Bayesian sensor (named for [Thomas Bayes](https://en.wikipedia.org/wiki/Thomas_Bayes)) should be used when you want a sensor which indicates the state of a system that you cannot directly measure. Lets consider a hypothetical scenario where want a sensor that indicates whether or not someone is cooking in the kitchen. We will call the sensor **sensor.cooking** and when someone is cooking the state is ON, otherwise the state is OFF. To be consistent with the wikipedia article on [Bayes' theorem](https://en.wikipedia.org/wiki/Bayes%27_theorem) lets refer to the state ON as 'event $A$' and the state OFF as 'event $\\neg A$' where the $\\neg$ symbol indicates 'not' $A$. I estimate that roughly 10% of the time someone is cooking, so if your were to enter the kitchen at a random time the probability ($P$) that you would find someone cooking (event $A$) is $P(A)$ = 0.1. Clearly I can then state that 90% of the time nobody will be cooking, and $P(\\neg A) = 1 - P(A)$ = 0.9. We can represent this probability distribution graphically:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAELCAYAAAA2mZrgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAHZFJREFUeJzt3XmYXHWd7/H3xyw2JM2WBJE0kDCJ\nkEUSnLDM42W7IRICAgaFRBzERLneS3RUGEBENhcYFQQ1KosMyBpAlsANIDhEwGFJWExIIhICkiYI\nSSOQfeM7f5zTh0qnuqt6OV2dyuf1PP101Tm/OudblZP69Pmd5aeIwMzMDOBDlS7AzMy6DoeCmZll\nHApmZpZxKJiZWcahYGZmGYeCmZllHAqWO0kXSLqxja89RdLjLcy/X9IXi7WVtELSnm1Zbytr3EbS\nvZLelXR7kfnnSLqmzGW12FbSSZJ+X+ay2vy529are6ULsK5J0qvAR4CNwEpgBvC1iFhRybqaiogj\nW5jXu/GxpOuA+og4N4cyPkvyWfWJiA1F6vhhuQsqbCtpAPAK0KNxuRFxE3BTO+ttUfpv/+WIeLjM\n9teR32drncx7CtaST6dfrJ8A9gM2+0+vxNa+He0B/LVYIJhtabb2/8xWhoh4HbgfGA4gaaakH0j6\nE7AK2FPSrpKmS3pb0kJJX2mymBpJ0yQtl/SspBGNMySdLenldN58SZ9p8lpJ+nnaPfMXSaMLZsyU\n9OVidUsKSYMknQqcBJyZdindK+nfJf2uSfufS7q8mWUNSdf1jq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0j1rV+/Xrq6+tZs8ZXyldCTU0NdXV19OjRo3RjM+vSqmKM5vr6empraxkwYACb3kzT8hYR\nNDQ0UF9fz8CBAytdjpm1U1Xc5mLNmjX06dPHgVABkujTp4/30syqRFWEAuBAqCB/9mbVo2pCwczM\n2s+hYGZmmao40Fzom3f+Obdl/3R8m8ZWNzPbYlTlnsKqtRtZunxth/2sWtv6kQKvvPJKdtllF0aO\nHMmee+7JddddB8Dq1as55JBD2Lhx02VOmTKFPfbYA4B169Zx8MEHs2HDhnZ/FgAPPPAAe+21F4MG\nDeKSSy5ptt2kSZPYeeedGT686WiYZra1qMpQWLluA2+tWNthPyvXtf7Lec6cOVxwwQU8//zz3HHH\nHZx++ukAXHvttYwfP55u3bplbV955RVmzpzJunXrWL58OT179mT06NFMmzat3Z/Fxo0bOe2007j/\n/vuZP38+t9xyC/Pnzy/a9pRTTuGBBx5o9zrNbMtVdd1HhYZ9tKwxzFs07422Dcg1d+5cTjjhBADq\n6uqyPYObbrqJm2++eZO2559/Pueeey5XX3018+bN48ADD+S4447j29/+NieddFK76n/66acZNGgQ\ne+6ZjKs+YcIE7rnnHoYOHbpZ24MPPphXX321Xeszsy1bVe4pdAVz585l7733JiL42c9+xtFHH826\ndetYtGgRAwYMyNrNmzePF154gRNPPJEhQ4Ywb948AIYPH86sWbOaXf5BBx3EyJEjN/t5+OGHN2n3\n+uuvs9tuH9x3sK6ujtdff71j36yZVY2q3lOolMWLF7NixQqOOOIIevTowf7778/UqVNZtmwZO+yw\nwyZtv/Od7/C9730PSQwZMoQXXkhGL+3WrRs9e/Zk+fLl1NbWbraOxx57rKxakruJbMrXFZhZcxwK\nOZgzZw6jR4/erH/+/fff3+TK36eeeooHH3yQ559/ntNOO401a9awzz77ZPPXrl1LTU1N0XUcdNBB\nLF++fLPpP/nJTzj88MOz53V1dSxe/MFYRvX19ey6665tfm9mVt2qOhTaejygvebOncuIEZufvrrj\njjuyceNG1qxZQ01NDeeccw733Xcfo0ePBuDNN99k3333Ba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} ], "source": [ "colours = [\"#348ABD\", \"#A60628\"]\n", "_width=0.2\n", "prior = 0.1\n", "x = [0.25, 0.5]\n", "y = [prior, 1 - prior]\n", "\n", "plt.bar(x[0], y[0], alpha=0.7,\n", " width=_width, color=colours[0],\n", " label= \" $P(A)$ = \" + str(prior),\n", " lw=\"3\", edgecolor=colours[0])\n", "\n", "plt.bar(x[1], y[1], alpha=0.7,\n", " width=_width, color=colours[1],\n", " label= r\"$P(\\neg A)$ = \" + str(1 - prior),\n", " lw=\"3\", edgecolor=colours[1])\n", "\n", "plt.ylim(0,1)\n", "plt.xticks(x, [\"ON / $A$\", r\"OFF / $\\neg A$\"])\n", "plt.title(\"Probability of initial states\")\n", "plt.ylabel(\"Probability\")\n", "plt.legend(loc=\"upper left\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets say that I create **sensor.cooking** at a random time with no knowledge about whether someone is cooking at the time. My initial belief about the likelihood that someone is cooking ($P(A)$) is reffered to as the *prior*, as it is my belief prior to any measurement. We have previously established that the prior, $P(A)$ = 0.1. Lets say that I have built a DIY sensor for detecting cooking smells and through experimentation I have concluded that it measures a true-positive ON 70% of the time when someone is cooking. I call the ON state of this DIY sensor 'event $B$' and can now state the likelihood that this sensor is ON *given* that someone is cooking is 70%. In algebraic notation, *given* is represented by the $|$ symbol and I can now state that the likelihood $P(A|B)$ = 0.7. However this DIY sensor is not perfect and measures a false-positive ON 10% of the time. We capture this false-positive likelihood in the expression $P(B| \\neg A)$ = 0.1. \n", "\n", "It turns out that we now have enough information to calculate the probability that we really care about, $P(A|B)$ which is the probability that someone is cooking *given* that we measured an ON with the DIY sensor. This probability is referred to as the *posterior* probability, as is is calculated after (or posterior to) the measurement. The posterior is calculated using the Bayes formula, where in words:\n", "\n", "$$\\mathtt{posterior} = \\frac{\\mathtt{likelihood} \\times \\mathtt{prior}}{\\mathtt{normalisation}}$$ \n", "\n", "The [full algebraic expression](https://en.wikipedia.org/wiki/Bayes%27_theorem) is:\n", "\n", "$$P(A|B) = \\frac{P(B|A)\\,P(A)}{ P(B|A) P(A) + P(B| \\neg A) P(\\neg A)}\\cdot$$\n", "\n", "Bayes formula is implemented in the Home-assistant [bayesian_sensor](https://home-assistant.io/components/binary_sensor.bayesian/) in the function update_probability():" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def update_probability(prior, prob_true, prob_false):\n", " \"\"\"Update probability using Bayes' rule.\"\"\"\n", " numerator = prob_true * prior\n", " denominator = numerator + prob_false * (1 - prior)\n", " probability = numerator / denominator\n", " return probability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For bayesian sensors we typically define a cutoff probability of 50% ($P$ = 0.5), below which we consider the sensor to be OFF, and obove which the sensor is ON. Before any measurements we only have our prior to estimate the state of the sensor, and since our prior is below the cutoff (0.1 < 0.5) the initial state of **sensor.cooking** is OFF. \n", "\n", "\n", "Lets now imagine we've made a single measurement using the DIY sensor and its state is ON. We know that for a single reading the likelihood $P(B|A)$ = 0.7. To use the same variable names as update_probability(), our complete set of inputs are; the prior $P(A)$ = 0.1, the true positive likelihood prob_true = $P(B|A)$ = 0.7, and the false positive likelihood prob_false = $P(B| \\neg A)$ = 0.1. Lets plug these into update_probability() (which is Bayes formula) and calculate the posterior probability ($P(A|B)$):" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.44\n" ] } ], "source": [ "prior = 0.1\n", "prob_true = 0.7\n", "prob_false = 0.1\n", "posterior = update_probability(prior, prob_true, prob_false)\n", "print(round(posterior,2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We find that after a single reading the posterior is below the 50% threshold and **sensor.cooking** remains OFF. \n", "However the posterior probability of 0.44 now becomes the prior for the next measurement. Lets say that we have a second sensor we believe detects cooking but with a much lower true positive rate, that is also reads ON, how does this improve the posterior probability? We calculate:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.54\n" ] } ], "source": [ "prior = 0.44\n", "prob_true = 0.15\n", "prob_false = 0.1\n", "posterior = update_probability(prior, prob_true, prob_false)\n", "print(round(posterior,2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And great news, with only two measurements the posterior probability is above the 50% threshold and **sensor.cooking** is now ON. \n", "\n", "Lets now consider a second hypothetical scenarior, and ask how many of the poor sensors does it require to trigger **sensor.cooking** to ON?" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.18181818181818182\n", "0.3076923076923077\n", "0.47058823529411764\n", "0.64\n" ] } ], "source": [ "prior = 0.1\n", "prob_true = 0.2\n", "prob_false = 0.1\n", "posterior = prior # Our initial conditions\n", "while posterior < 0.5:\n", " posterior = update_probability(posterior, prob_true, prob_false)\n", " print(posterior)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We find that four consecutive ON measurements are required to trigger **sensor.cooking** to ON.\n", "\n", "Now, how do we update our posterior if we receive a negative reading?\n", "\n", "How about combining the readings from several sensors?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bayesian sensor\n", "\n", "The component works by creating a list of all the sensors in the config that are in their ON state, and calculatules the conditional probability based on their prob_true/prob_false values. Lets check that by creating a Change bayesian sensor on a localhost HA instance and query it via the restfulAPI. I will start by creating the bayesian sensor example from the [docs](https://home-assistant.io/components/binary_sensor.bayesian/), with a couple of dummy switches as inputs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "switch:\n", " platform: command_line\n", " switches:\n", " kitchen_lights:\n", " command_on: \"\"\n", " command_off: \"\"\n", " bedroom_lights:\n", " command_on: \"\"\n", " command_off: \"\"\n", "\n", "binary_sensor:\n", " - platform: bayesian\n", " prior: 0.1\n", " observations:\n", " - entity_id: 'switch.kitchen_lights'\n", " prob_given_true: 0.6\n", " prob_given_false: 0.2\n", " platform: 'state'\n", " to_state: 'on'\n", " - entity_id: 'switch.bedroom_lights'\n", " prob_given_true: 0.25\n", " prob_given_false: 0.1\n", " platform: 'state'\n", " to_state: 'on'\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initially, the switches are off" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'off'\n" ] } ], "source": [ "url = 'http://localhost:8123/api/states/switch.kitchen_lights'\n", "headers = {'content-type': 'application/json'}\n", "\n", "response = get(url, headers=headers).json()\n", "print_json(response['state'])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'off'\n" ] } ], "source": [ "url = 'http://localhost:8123/api/states/switch.bedroom_lights'\n", "headers = {'content-type': 'application/json'}\n", "\n", "response = get(url, headers=headers).json()\n", "print_json(response['state'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets switch on switch.kitchen_lights and see how the binary_sensor behaves" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'attributes': {'friendly_name': 'BayesianBinary',\n", " 'observations': [{'prob_false': 0.2, 'prob_true': 0.6}],\n", " 'probability': 0.25,\n", " 'probability_threshold': 0.5},\n", " 'entity_id': 'binary_sensor.bayesianbinary',\n", " 'last_changed': '2017-12-15T06:48:29.173741+00:00',\n", " 'last_updated': '2017-12-15T06:48:39.547470+00:00',\n", " 'state': 'off'},\n", " {'attributes': {'assumed_state': True, 'friendly_name': 'kitchen_lights'},\n", " 'entity_id': 'switch.kitchen_lights',\n", " 'last_changed': '2017-12-15T06:48:39.546260+00:00',\n", " 'last_updated': '2017-12-15T06:48:39.546260+00:00',\n", " 'state': 'on'},\n", " {'attributes': {'assumed_state': True,\n", " 'auto': True,\n", " 'entity_id': ['switch.bedroom_lights',\n", " 'switch.kitchen_lights'],\n", " 'friendly_name': 'all switches',\n", " 'hidden': True,\n", " 'order': 0},\n", " 'entity_id': 'group.all_switches',\n", " 'last_changed': '2017-12-15T06:48:39.548952+00:00',\n", " 'last_updated': '2017-12-15T06:48:39.548952+00:00',\n", " 'state': 'on'}]\n" ] } ], "source": [ "url = 'http://localhost:8123/api/services/switch/turn_on'\n", "payload = {'entity_id': 'switch.kitchen_lights'}\n", "headers = {'content-type': 'application/json'}\n", "\n", "response = post(url, data=json.dumps(payload), headers=headers).json()\n", "print_json(response)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.25" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response[0]['attributes']['probability']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see that the bayesian sensor has been updated to include the observations associated with switch.kitchen_lights and the probability has been updated to 0.25. Lets just check that this agrees with what we expect." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.25\n" ] } ], "source": [ "prior = 0.1\n", "prob_true = 0.6\n", "prob_false = 0.2\n", "posterior = update_probability(prior, prob_true, prob_false)\n", "print(round(posterior,2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now lets turn on the second switch." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'attributes': {'friendly_name': 'BayesianBinary',\n", " 'observations': [{'prob_false': 0.2, 'prob_true': 0.6},\n", " {'prob_false': 0.1, 'prob_true': 0.25}],\n", " 'probability': 0.45,\n", " 'probability_threshold': 0.5},\n", " 'entity_id': 'binary_sensor.bayesianbinary',\n", " 'last_changed': '2017-12-15T06:48:29.173741+00:00',\n", " 'last_updated': '2017-12-15T06:48:39.737947+00:00',\n", " 'state': 'off'},\n", " {'attributes': {'assumed_state': True, 'friendly_name': 'bedroom_lights'},\n", " 'entity_id': 'switch.bedroom_lights',\n", " 'last_changed': '2017-12-15T06:48:39.736376+00:00',\n", " 'last_updated': '2017-12-15T06:48:39.736376+00:00',\n", " 'state': 'on'}]\n" ] } ], "source": [ "url = 'http://localhost:8123/api/services/switch/turn_on'\n", "payload = {'entity_id': 'switch.bedroom_lights'}\n", "headers = {'content-type': 'application/json'}\n", "\n", "response = post(url, data=json.dumps(payload), headers=headers).json()\n", "print_json(response)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.45" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response[0]['attributes']['probability']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see that the bayesian sensor has again been updated to include the observations associated with switch.bedroom_lights and the probability has been updated to 0.45. Lets just check that this agrees with what we expect." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.45\n" ] } ], "source": [ "prior = 0.25\n", "prob_true = 0.25\n", "prob_false = 0.1\n", "posterior = update_probability(prior, prob_true, prob_false)\n", "print(round(posterior,2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fantastic, it agrees, however note that the probability of 0.45 is below the standard threshold of 0.5. Lets switch off lights." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'attributes': {'friendly_name': 'BayesianBinary',\n", " 'observations': [],\n", " 'probability': 0.1,\n", " 'probability_threshold': 0.5},\n", " 'entity_id': 'binary_sensor.bayesianbinary',\n", " 'last_changed': '2017-12-15T06:48:29.173741+00:00',\n", " 'last_updated': '2017-12-15T06:48:39.918076+00:00',\n", " 'state': 'off'},\n", " {'attributes': {'assumed_state': True, 'friendly_name': 'bedroom_lights'},\n", " 'entity_id': 'switch.bedroom_lights',\n", " 'last_changed': '2017-12-15T06:48:39.907083+00:00',\n", " 'last_updated': '2017-12-15T06:48:39.907083+00:00',\n", " 'state': 'off'},\n", " {'attributes': {'assumed_state': True, 'friendly_name': 'kitchen_lights'},\n", " 'entity_id': 'switch.kitchen_lights',\n", " 'last_changed': '2017-12-15T06:48:39.916704+00:00',\n", " 'last_updated': '2017-12-15T06:48:39.916704+00:00',\n", " 'state': 'off'},\n", " {'attributes': {'assumed_state': True,\n", " 'auto': True,\n", " 'entity_id': ['switch.bedroom_lights',\n", " 'switch.kitchen_lights'],\n", " 'friendly_name': 'all switches',\n", " 'hidden': True,\n", " 'order': 0},\n", " 'entity_id': 'group.all_switches',\n", " 'last_changed': '2017-12-15T06:48:39.918405+00:00',\n", " 'last_updated': '2017-12-15T06:48:39.918405+00:00',\n", " 'state': 'off'}]\n" ] } ], "source": [ "url = 'http://localhost:8123/api/services/switch/turn_off'\n", "\n", "response = post(url).json()\n", "print_json(response)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.1" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response[0]['attributes']['probability']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And we are back to our original prior.\n", "\n", "What is clear from this exercise is that if you don't choose your input sensors/switches, weights and thresholds correctly, the bayesian sensor is pretty useless as it is never on. In practice the best approach is to set your inputs and their weights with your best guess, then monitor the probability of the bayesian_sensor for a few days and note its value at the times you care about (e.g. whilst cooking), then set your default threshold at this level. For example, in our scenario we observed that the probaility 0.45 < 0.5, so we would then set our default threshold to 0.44.\n", "\n", "Another thing that is pretty clear from this exercise if that if you only have a couple of inputs (say less than three or four), then there isn't much point in using a bayesian_sensor over a regular group. The bayesian_sensor only shows its value when you have many inputs, combining both weakly and strongly sensitivie/specific sensors. In such a case you could write many rules to determine when to trigger a group sensor, but the bayes_sensor takes care of the calculation for you. \n", "\n", "If you find that you have relatively few inputs corresponding to real sensors/switches, one strategy to implement a bayesian_sensor is to create software sensors which trigger after given time delay after a state change. For example, in the cooking scenario, I might consider creating a sensor which triggers ON only when there has been motion activity for more than 5 minutes in the kitchen. You could use the [timer](https://home-assistant.io/components/timer/) component and automations to create your software sensor." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }