Hello, Home Assistant Community!
I am quite new to the Home Assistant system (it is my first post),
still figuring out all the capabilities of the HASS, but finding it very interesting and customizable to be used for majority of tasks.
I’m trying to use Home Assistant to control zigbee AC plugs for my project,
currently I am using Home Assistant REST API + python script to control the switches based on certain logic.
Ideally, I would like to install Home Assistant OS on a Raspberry Pi 4 B and do all the tasks within the Home Assistant, as it supports installing add-ons to do all the cool stuff in one centralized platform, and not have to manage Home Assistant and python code separately.
However, the logic inside the python code is a little bit complicated, and it requires installing some external packages. Also I need to launch multiple linux CLI ‘screens’ to run several virtual environments simultaneously in the background, so I have to install Home Assistant with Docker and run the code in parallel with HASS.
It would be great if you can just review the perspective of my project and assess if it could be achieved with just Home Assistant features (RESTful sensors, scripts, blueprints etc.), or should I just stick to current method. (If there is a way to achieve this just with HASS, I can continue researching.)
What I need to achieve is:
- I have my website where people can book seats inside a room on a given date and time.
- Every hour (or 30 minutes, depending on a reservation program) I need to check whether a seat is booked or not and turn off the light (zigbee plug) if it is.
- Lights should be turned off for x minutes (ie. 5 minutes) before the next reservation window. (It can be done with automation, I guess, but some times one person can book several hours, then it shouldn’t be turned off, because the booking session is not over.)
The website currently has a backend API to check reservation schedule of seats within a certain facility. (I can modify API if needed.)
The structure of the API call is:
-
header should contain
"accessToken" : {access_token}
-
query parameters:
http://my_example_service.com/api/{facility_guid}/{start_timestamp}/{end_timestamp}
-
When the request is successful, API returns json object in data: for example
"[{"seat_id": "seat1", "reservation_info": [{"reservation_id": "reservationID", "start_time": "2023.12.13 12:00:00", "end_time": "2023.12.13 13:00:00"]}, {"seat_id": "seat2", "reservation_info": [ ]}]"
-
If the
"reservation_info"
has an empty array[ ]
, the seat has no booking at that time.
In order to achieve this, I need to
- Store constant variables (access token, GUID of a facility)
- Get datetime range of the next schedule window.
(Booked seats need to be controlled few minutes before the schedules time. So. at 13:55 → check booking status of seats between 14:00 ~ 15:00. At 15:55 check booking status of seats between 16:00 ~ 17:00 etc. - Convert the datetime to timestamps (2023.12.13 14:00 ~ 2023.12.13 15:00 → 1702357200000 / 1702360800000)
- Make a GET request to the server (with access token and timestamp generated above)
- Parse response json data and get only the value of a key needed to check the booking status. (“reservation_info”)
- Loop for objets inside an array of objects
- Check if the array of schedules has values or empty.
- Turn on or turn off a switch depending on the booking status (each switch entity is named by the index of an array: For example: seat status return value [0, 1, 0, 1, 0, 1], indexes are 0 to 5, plugs are named: ‘no0’ ~ ‘no5’
I cannot control the Home Assistant switches directly from the server, because there are multiple facilities and buildings, numerous plugs to control (it will impact the performance issue, as the system gets larger), and each Home Assistant device would have it’s own IP address to access from external network and API keys to manage.
Written like this, I think it’s quite complicated to be achieved just with the Home Assistant,
but maybe there are some powerful features of the HASS I don’t know yet that can be used to implement the task I need… Or maybe I am overcomplicating something that could be done in a simpler way.
Would be great if you just give me your opinion of whether there are some features that you presume that can be used to deal with such programmatic tasks, are there any othere projects trying to achieve a similar task, or I should just give up and keep using my python code.
Sorry for the long post.
Will be looking forward to hear what you think about the project.
Best regards.