This script allows you to easily toggle sections of your Home Assistant and Linux configuration files by surrounding them with HTML-like tags. It is especially useful for enabling or disabling sections of your configuration based on specific conditions, such as the state of a group or device. Toggle block improves code maintenance by improving visibility and eliminating the need to maintain multiple configuration files in parallel.
-
Download the
toggle_block.sh
script and place it in your Home Assistant configuration directory (e.g.,/config/scripts
). -
Access the terminal and make the script executable:
chmod +x /config/scripts/toggle_block.sh
-
Add the following to your
configuration.yaml
to make the script available for use in Home Assistant automations:shell_command: toggle_block: "/bin/bash /config/scripts/toggle_block.sh '{{ target_block }}' '{{ toggle_state }}' '{{ target_file }}' '{{ backup }}'"
-
Restart Home Assistant to apply the changes.
-
Under Developer tools>ACTIONS you will have a new action:
Shell Command: toggle_block
This example demonstrates how the script can toggle blocks in your frigate.yaml
configuration file. The script allows you to activate or deactivate specific sections based on conditions (e.g., whether you're home or away).
When Home | When Away |
---|---|
objects:
track:
- cat
#<away>
# - person
# - umbrella
#</away>
#<home>
- cell phone
- wine glass
#</home>
filters:
#<away>
# person:
# max_ratio: 1
# min_area: 100000
#</away> |
objects:
track:
- cat
#<away>
- person
- umbrella
#</away>
#<home>
# - cell phone
# - wine glass
#</home>
filters:
#<away>
person:
max_ratio: 1
min_area: 100000
#</away> |
cat
is always tracked, regardless of the state.- The block tagged with
<away>
is active when you're marked as "away" (uncommented) and deactivated when you're "home" (commented). In this case:- When away,
person
andumbrella
are tracked. - Additional filters, such as
max_ratio
andmin_area
, are applied toperson
when away.
- When away,
- The block tagged with
<home>
is the opposite, activated only when you're "home". In this case:- When home,
cell phone
andwine glass
are tracked.
- When home,
This approach allows you to dynamically adjust tracking, filters, zones, camera settings...(anything you can think of), enhancing both functionality and maintainability without having to manually edit the configuration each time or maintain multiple versions of the configuration file.
You can use the script directly from the terminal by passing the parameters in the following order:
/bin/bash /config/scripts/toggle_block.sh <target_block> <toggle_state> <target_file> <backup>
-
To
show
theaway
block(s) infrigate.yaml
:/bin/bash /config/scripts/toggle_block.sh "away" "show" "frigate.yaml"
-
To toggle the
away
block(s)on
infrigate.yaml
with abackup
:/bin/bash /config/scripts/toggle_block.sh "away" "on" "frigate.yaml" "backup"
-
Navigate to Developer Tools:
- In Home Assistant, go to the Developer Tools section in the left-hand sidebar.
-
Select the ACTIONS Tab:
-
Click on the ACTIONS tab.
-
Look for the entry called:
Shell Command: toggle_block
-
-
Switch to YAML Mode:
- Click the GO TO YAML MODE button to switch from the default interface.
-
Fill in the Parameters:
-
Paste the following code into the YAML editor and replace values as needed:
data: target_block: home toggle_state: "off" target_file: frigate.yaml backup: "on"
-
-
Perform the Action:
- Once you’ve filled in the YAML, click the PERFORM ACTION button at the bottom of the page to run the command.
-
View the Response:
-
After executing the action, you will receive a response similar to this:
stdout: |- Backup created: /config/frigate.yaml.bak The block 'home' has been commented. #<home> # - cell phone # - wine glass #</home> stderr: "" returncode: 0
-
You can easily integrate this script into Home Assistant automations. Here's an example that toggles a block in frigate.yaml
based on whether the Residents group is home or away:
alias: Frigate Toggle Block Home Away
description: >-
Toggles the home and away blocks of the frigate.yaml configuration file based
on the state of group.residents
trigger:
- platform: state
entity_id:
- group.residents
to: not_home
id: Residents Away
- platform: state
entity_id:
- group.residents
to: home
id: Residents Home
condition: []
action:
- choose:
- conditions:
- condition: trigger
id:
- Residents Away
sequence:
- action: shell_command.toggle_block
data:
target_block: home
toggle_state: "off"
target_file: frigate.yaml
backup: "on"
- action: shell_command.toggle_block
data:
target_block: away
toggle_state: "on"
target_file: frigate.yaml
backup: "on"
- conditions:
- condition: trigger
id:
- Residents Home
sequence:
- action: shell_command.toggle_block
data:
target_block: home
toggle_state: "on"
target_file: frigate.yaml
backup: "on"
- action: shell_command.toggle_block
data:
target_block: away
toggle_state: "off"
target_file: frigate.yaml
backup: "on"
- action: hassio.addon_start
data:
addon: ccab4aaf_frigate
mode: single
The script accepts four parameters in the following order:
-
target_block (Required): The name of the block you want to toggle. Blocks that need toggling should be surrounded by case-sensitive HTML-like tags.
- Example:
#<away>
and#</away>
.
- Example:
-
toggle_state (Required): The desired state of the block, either
on
,off
orshow
.on
will uncomment the block.off
will comment the block.show
will display the block without making changes.
-
target_file (Required): The YAML file where the block is located. If a file name is provided without a path, the script assumes it is in the
/config
directory.- Example:
frigate.yaml
will be treated as/config/frigate.yaml
.
- Example:
-
backup (Optional): Determines if a backup
.bak
file will be created.on
orbackup
: Creates a.bak
file (default).off
orno_backup
: Skips backup creation.- Note: Backup is skipped automatically for the
show
state.
-
Space after the
#
for toggle blocks:
When toggling comments, the script requires a space after the#
for it to function properly.- Example:
# max_ratio: 1
will work. - However,
#max_ratio: 1
(without the space) will not uncomment the line.
- Example:
-
Spacing in tags:
Spaces are only allowed before the#
in tags.- Incorrect:
# <away>
- Incorrect:
#<away>
— can be difficult to spot. - Incorrect:
#<away toggle>
- Incorrect:
-
No content after the tag:
There should be nothing after the closing tag.- Incorrect:
#<away_toggle> # This is my comment
- Incorrect:
You can add comments within toggle blocks that will be preserved regardless of the toggle state by removing the space after the #
. These are called hard comments and will not be affected by the script:
#<away>
# - person # This will be toggled
## Hard comment # This will be preserved and ignored by the script
#</away>
- Case Sensitivity: The block markers are case-sensitive. For example,
#<away>
is not the same as#<Away>
. - No nesting: Tags within tags are not supported. They will be treated as hard comments and will not be toggled. Create a separate toggle block for each tag.
- Multiplicity: You can use the same tags in multiple places within a file to keep code maintenance simple. You can use many different tags in a file and they can all be toggled independently.
- Testing: It is recommended to copy your target file to a temporary file for testing, 'test.yaml', for example. Always use
show
to verify that your tags and toggle block are being correctly identified before toggling. Use the Home Assistant Developer tools and/or terminal to fully debug before incorporating your toggle block into an automation, where exit errors are often reported as "Action run successfully". - More Testing: Your file still has to be a valid file after all the toggling. If you toggle off a section of the file that is required, and don't toggle on a replacement, you can easily leave the configuration in a non-working state.
- Backups: Remember to switch backup to
off
orno_backup
once you have completed testing and remove any test or .bak files that you no longer need to prevent directory clutter. - Last Resort: This script should be used as a last resort when problems cannot be solved in the "recommended manner". In the case of Home Assistant, be sure you cannot achieve your goal within the GUI before using a heavy handed approach like this. In the specific example of Frigate, many options can be changed from the Home Assistant integration without changing frigate.yaml and restarting Frigate.
This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.