Hi, I have specific dates for garbage collection and I want to create a card that tells me the date of the next garbage collection and in how many days it is. I was thinking of writing a python script and having a card read the output of the python script.
Is this a sensible approach? And is this possible?
If so, could someone point me to the steps required to do so?
That seems to be the hard way. The easy way is to create calendar entries and sync to the HA calendar and trigger from it. I used to use the great Garbage Collection extension but he discontinued it when calendaring was able to be externally synchronized with HA, so I just created recurring schedules on an Apple calendar with a few hand-moved ones for holidays or whatever and it works very well - you just use automation to do your notifications or update a helper with the date.
If you search the community forum, you’ll see that most people use Home Assistant to automate their homes without using python scripts. There are simple ways of achieving your goal without creating a python script (and CO_4X4 has explained how).
However, if you prefer to interact with Home Assistant via a python script, here’s the documentation for Home Assistant’s WebSocket API:
thanks for your answer @123 !
Since I’m not very familiar with Home Assistant yet I don’t really know where to start.
What would be the steps involved?
I guess I would have to create a custom sensor with data fields: “next_date”, and “days_remaining”.
I would then have my python script that is run regularly that can update the sensor data fields. Maybe a from python script from this integration could do that?
I could then add a lovelace card that displays both data fields.
Is that possible? How can I create this custom sensor?
Then it would be best if you follow the advice offered to you by users with extensive experience who are telling you not to do it with python scripts.
You should know that there’s a difference between ‘python script’ and the python_script integration. The former uses the python interpreter installed on your machine, without constraints, to execute your python code. The latter runs your python code in a sandbox that disallows importing python libraries. If none of what I wrote is familiar to you, then you’re facing a steep learning curve before you are able to implement what you want.
Whatever you choose to do next, I wish you good luck with your project.
I am well aware the python_script will be run in a sandbox and as far as I know the datetime util is supported. I don’t think I will need much more for this simple task.
The warning is well appreciated, but I would be much more comfortable being able to write my own integrations for such simple tasks and being in full control than using calendars (which if you think about it are very complex in comparison).
I am just not very familiar with the Home Assistant infrastructure so a little kickstart would be appreciated
I assume you are using the term “integration” in a broad sense because in Home Assistant, an “integration” cannot be composed using python_script. It’s python code that conforms to specific requirements.
Actually it’s easy. Given that you believe it’s not implies you’re not entirely familiar with it.
Going from zero to integration development is truly going from dry land to jumping into the Mariana trench and trying to free dive it.
I find the best way to start learning how to do advanced stuff in HA starts with looking at the gobs of sample Jinja code people post and understanding what is happening and why, then look at the source code of other integrations that may be similar to what you want to create and then understanding what happens there, but with the developer docs open as a reference so you understand why things are named how they are or why they might be included in the script.
It’s not rocket science but all good language learning starts with a simple Hello World (which the dev docs provide) application and going from there. There’s a reason why "Hello World’ is a universal teaching tool for every language, it lets you dip your toe in the water and ease into it.
You have two distinctly different aspects to learn (three if you include Jinja): Python and how HA uses Python and its libraries to do the job. Learn Python first.
@CO_4X4 my question was how I can use a python script in home assistant, what makes you think that I don’t know python?
I would be cautious with too much warnings, it might kill the enthusiasm to learn.
What I want is very basic and is supposed to be my “hello world” for Home Assistant.
I managed to achieve that by creating a template sensor:
I get the date by scraping a website of my local community and update the sensor values once a day in a python script with the REST API.
To show the data I opted for a Markdown lovelace card:
type: markdown
content: |
# Cardboard
* Next date: `{{states.sensor.next_cardboard_collection_date.state}}`
* In `{{ states.sensor.next_cardboard_collection_days_diff.state }}` days
# Paper
* Next date: `{{ states.sensor.next_paper_collection_date.state }}`
* In `{{states.sensor.next_paper_collection_days_diff.state}}` days
title: Collection Dates
Let me know if there is a much better way to achieve this or if there is anything fun to add