Security Automation

Hello everyone,

I got excited about the idea of implementing a home security system based on Home Assistant.

Here’s a brief overview of what I want to achieve:

  • Security state (activated/deactivated)
  • Alarm state (activated/deactivated)
  • Reading tags to activate and deactivate security
  • Triggering specific automations in case the alarm is activated

Creating automations with door sensors and motion sensors is straightforward.

However, I’ve faced some inconveniences and certain difficulties with automation using tags.

The first thing I did was store tag IDs in a helper element input_text in the form of an array (['00-00-00-00-00-00-00'...]). I implemented the automation, and everything works as needed. The security state changes when a scanned tag is found in the list.

But I encountered the following inconveniences: the list of valid tag IDs in input_text is anonymous, and I don’t know which tag ID belongs to whom. So, I tried using JSON ([{tag:'00-00-00-00-00-00-00', name:'Alex'}...]). After this, I ran into a problem: input_text is limited to 100 characters, making it inconvenient to store a small number of tags.

I didn’t stop there and looked for new information on where I could store a relatively large number of tags. At the same time, it would be good to move the list of valid device IDs for the tag reader into a list.

I learned that data can be stored in JSON files. I created a sensor in configuration.yaml, made a new automation to check (trigger.event.data.device_id, trigger.event.data.tag_id) with the lists from the JSON file, and everything worked perfectly. I was extremely happy!

After that, I decided to go further and wanted to create automation to add a new tag to the JSON file, and here I faced the most challenging problem for me: writing to the file while maintaining JSON validity.

I tried several options:

  • command_line writes to the file, but my script breaks the JSON in the file, and sometimes I encountered errors with the 100-character limit, though I am not sure if that was the cause or if I made a mistake somewhere.
  • I started looking into python_scripts, but it is very limited.

Maybe someone can help me write automation for adding a new tag ID to the JSON file.
For the new entry, I use 2 input_text (tag_id, name).

The JSON format is {"devices":["0000000000000000000000000000000"],"tags":[{"tag":"00-00-00-00-00-00-00","name":"Alex"}]}.

Thanks

Hi, welcome to the forum!

Have a look at Alarmo which covers most of your questions.
For tags, I use an ESP device with a PN532 tag reader which works like a charm: info can be found here in the forum.

1 Like

I was able to solve all my problems

Maybe it will be useful to someone else for other purposes

I used shell_command, this is also limited to just one command but I managed to make a check in json file and add a new record with preserving validity.

here are some examples

shell_command:
  check_tag_device: jq -e '(.devices[] | select(. == "{{ device_id }}")) and (.tags[] | select(.tag == "{{ tag_id }}"))' /config/tags/tags.json
  check_tag: jq -e '(.tags[] | select(.tag == "{{ tag_id }}"))' /config/tags/tags.json
  check_device: jq -e '(.devices[] | select(. == "{{ device_id }}"))' /config/tags/tags.json
  add_new_tag: sed -i 's/,{"test": "test"}/,{"tag":"{{ tag }}", "name":"{{ name }}"},{"test": "test"}/g' /config/tags/tags.json

use in automations

check tag and device

  - action: shell_command.check_tag_device
    data:
      tag_id: "{{ trigger.event.data.tag_id }}"
      device_id: "{{ trigger.event.data.device_id }}"
    response_variable: result_check_tag_device

add new tag

  - action: shell_command.add_new_tag
    data:
      tag: "{{ tag }}"
      name: "{{ name }}"