|
| 1 | +# Home Assistant Automations |
| 2 | + |
| 3 | +To automate EMHASS with Home Assistant, we will need to define some shell commands in the Home Assistant `configuration.yaml` file and some basic automations in the `automations.yaml` file. |
| 4 | +In the next few paragraphs, we are going to consider the `dayahead-optim` optimization strategy, which is also the first that was implemented, and we will also cover how to publish the optimization results. |
| 5 | +Additional optimization strategies were developed later, that can be used in combination with/replace the `dayahead-optim` strategy, such as MPC, or to expand the functionalities such as the Machine Learning method to predict your household consumption. Each of them has some specificities and features and will be considered in dedicated sections. |
| 6 | + |
| 7 | +## Dayahead Optimization - Method 1) Add-on and docker standalone |
| 8 | + |
| 9 | +We can use the `shell_command` integration in `configuration.yaml`: |
| 10 | +```yaml |
| 11 | +shell_command: |
| 12 | + dayahead_optim: "curl -i -H \"Content-Type:application/json\" -X POST -d '{}' http://localhost:5000/action/dayahead-optim" |
| 13 | + publish_data: "curl -i -H \"Content-Type:application/json\" -X POST -d '{}' http://localhost:5000/action/publish-data" |
| 14 | +``` |
| 15 | +An alternative that will be useful when passing data at runtime (see dedicated section), we can use the the `rest_command` instead: |
| 16 | +```yaml |
| 17 | +rest_command: |
| 18 | + url: http://127.0.0.1:5000/action/dayahead-optim |
| 19 | + method: POST |
| 20 | + headers: |
| 21 | + content-type: application/json |
| 22 | + payload: >- |
| 23 | + {} |
| 24 | +``` |
| 25 | + |
| 26 | +## Dayahead Optimization - Method 2) Legacy method using a Python virtual environment |
| 27 | + |
| 28 | +In `configuration.yaml`: |
| 29 | +```yaml |
| 30 | +shell_command: |
| 31 | + dayahead_optim: ~/emhass/scripts/dayahead_optim.sh |
| 32 | + publish_data: ~/emhass/scripts/publish_data.sh |
| 33 | +``` |
| 34 | +Create the file `dayahead_optim.sh` with the following content: |
| 35 | +```bash |
| 36 | +#!/bin/bash |
| 37 | +. ~/emhassenv/bin/activate |
| 38 | +emhass --action 'dayahead-optim' --config ~/emhass/config.json |
| 39 | +``` |
| 40 | +And the file `publish_data.sh` with the following content: |
| 41 | +```bash |
| 42 | +#!/bin/bash |
| 43 | +. ~/emhassenv/bin/activate |
| 44 | +emhass --action 'publish-data' --config ~/emhass/config.json |
| 45 | +``` |
| 46 | +Then specify user rights and make the files executables: |
| 47 | +```bash |
| 48 | +sudo chmod -R 755 ~/emhass/scripts/dayahead_optim.sh |
| 49 | +sudo chmod -R 755 ~/emhass/scripts/publish_data.sh |
| 50 | +sudo chmod +x ~/emhass/scripts/dayahead_optim.sh |
| 51 | +sudo chmod +x ~/emhass/scripts/publish_data.sh |
| 52 | +``` |
| 53 | + |
| 54 | +## Common for any installation method |
| 55 | + |
| 56 | +### Options 1, Home Assistant automate publish |
| 57 | + |
| 58 | +In `automations.yaml`: |
| 59 | +```yaml |
| 60 | +- alias: EMHASS day-ahead optimization |
| 61 | + trigger: |
| 62 | + platform: time |
| 63 | + at: '05:30:00' |
| 64 | + action: |
| 65 | + - service: shell_command.dayahead_optim |
| 66 | +- alias: EMHASS publish data |
| 67 | + trigger: |
| 68 | + - minutes: /5 |
| 69 | + platform: time_pattern |
| 70 | + action: |
| 71 | + - service: shell_command.publish_data |
| 72 | +``` |
| 73 | +In these automations the day-ahead optimization is performed once a day, every day at 5:30am, and the data *(output of automation)* is published every 5 minutes. |
| 74 | + |
| 75 | +### Option 2, EMHASS automated publish |
| 76 | + |
| 77 | +In `automations.yaml`: |
| 78 | +```yaml |
| 79 | +- alias: EMHASS day-ahead optimization |
| 80 | + trigger: |
| 81 | + platform: time |
| 82 | + at: '05:30:00' |
| 83 | + action: |
| 84 | + - service: shell_command.dayahead_optim |
| 85 | + - service: shell_command.publish_data |
| 86 | +``` |
| 87 | +in configuration page/`config.json` |
| 88 | +```json |
| 89 | +"method_ts_round": "first" |
| 90 | +"continual_publish": true |
| 91 | +``` |
| 92 | +In this automation, the day-ahead optimization is performed once a day, every day at 5:30am. |
| 93 | +If the `optimization_time_step` parameter is set to `30` *(default)* in the configuration, the results of the day-ahead optimization will generate 48 values *(for each entity)*, a value for every 30 minutes in a day *(i.e. 24 hrs x 2)*. |
| 94 | + |
| 95 | +Setting the parameter `continual_publish` to `true` in the configuration page will allow EMHASS to store the optimization results as entities/sensors into separate json files. `continual_publish` will periodically (every `optimization_time_step` amount of minutes) run a publish, and publish the optimization results of each generated entities/sensors to Home Assistant. The current state of the sensor/entity being updated every time publish runs, selecting one of the 48 stored values, by comparing the stored values' timestamps, the current timestamp and [`'method_ts_round': "first"`](https://emhass.readthedocs.io/en/latest/publish_data.html#the-publish-data-specificities) to select the optimal stored value for the current state. |
| 96 | +
|
| 97 | +option 1 and 2 are very similar, however, option 2 (`continual_publish`) will require a CPU thread to constantly be run inside of EMHASS, lowering efficiency. The reason why you may pick one over the other is explained in more detail below in [continual_publish](https://emhass.readthedocs.io/en/latest/publish_data.html#continual-publish-emhass-automation). |
| 98 | + |
| 99 | +Lastly, we can link an EMHASS published entity/sensor's current state to a Home Assistant entity on/off switch, controlling a desired controllable load. |
| 100 | +For example, imagine that I want to control my water heater. I can use a published `deferrable` EMHASS entity to control my water heater's desired behavior. In this case, we could use an automation like the below, to control the desired water heater on and off: |
| 101 | + |
| 102 | +on: |
| 103 | +```yaml |
| 104 | +automation: |
| 105 | +- alias: Water Heater Optimized ON |
| 106 | + trigger: |
| 107 | + - minutes: /5 |
| 108 | + platform: time_pattern |
| 109 | + condition: |
| 110 | + - condition: numeric_state |
| 111 | + entity_id: sensor.p_deferrable0 |
| 112 | + above: 0.1 |
| 113 | + action: |
| 114 | + - service: homeassistant.turn_on |
| 115 | + entity_id: switch.water_heater_switch |
| 116 | +``` |
| 117 | +off: |
| 118 | +```yaml |
| 119 | +automation: |
| 120 | +- alias: Water Heater Optimized OFF |
| 121 | + trigger: |
| 122 | + - minutes: /5 |
| 123 | + platform: time_pattern |
| 124 | + condition: |
| 125 | + - condition: numeric_state |
| 126 | + entity_id: sensor.p_deferrable0 |
| 127 | + below: 0.1 |
| 128 | + action: |
| 129 | + - service: homeassistant.turn_off |
| 130 | + entity_id: switch.water_heater_switch |
| 131 | +``` |
| 132 | +These automations will turn on and off the Home Assistant entity `switch.water_heater_switch` using the current state from the EMHASS entity `sensor.p_deferrable0`. `sensor.p_deferrable0` being the entity generated from the EMHASS day-ahead optimization and published by examples above. The `sensor.p_deferrable0` entity's current state is updated every 30 minutes (or `optimization_time_step` minutes) via an automated publish option 1 or 2. *(selecting one of the 48 stored data values)* |
0 commit comments