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