New HA user: need some help with automation

Hi:
I’m trying to migrate my automation from OpenHAB to HA and are now look for options for a few complex ones. I hope to get some help here, even just some keywords that I can search myself. Below is the automation(s) that I want to implement, it doesn’t need to be exactly the same so other ideas are welcomed!

  1. When my car is connected to the charger
  2. Calculate best/cheapest hours to charge between now and when the car should be ready (which is either tomorrow morning or today at 16.00 depending on when the car is connected)
  3. Present options by sending push notification and if there are more options I can choose which period I want
  4. Schedule the charging based on default or user selected time in step 3
  5. When the time is the scheduled charging hours, start to charge

I’m used to program in OH with DSL rules so it’s fairly simple to implement the above, however I find it difficult (or maybe because my knowledge of HA is too limited) to implement the same in HA. Appreciate if anyone can give me a hand on where to look

well, I have recently been getting the hang of all of this and can say I think for such an automation you will certainly need some templating & jinja particularly for step 2. The other pieces I think you could build in the ui.
this should be a good start to get an idea:

and direct to jinja
https://jinja.palletsprojects.com/en/stable/templates/

if it gets more complex that the sensor needs more info you may want to look at a trigger sensor that can only be done in yaml/jinja.

To test / prototype code there is the template editor under developer tools and that is good place to try things and get them working … eg taking in the price info (presumably you have this already available?) and converting it to a list that you can then presumably (somehow?) make into a dynamic selection but then serve this to the app where it will trigger steps 4/5 which with some trial and error you will likely solve as well.

for creation of code I am using a combination of first building/prototyping in the UI automations, helpers, etc and then putting into configs - here is some integration docs, here:

I am now trying to put them instead into packages (config.yaml) to keep things tidy - in automations you can edit in yaml directly and just copy past and re-indent. Helpers are a bit more difficult, the code is obfuscated a bit as they are only visible in the backend in a hidden file (tip: use vscode integration)

Also being more mindful to name things around functions and adding labels helps me. If I don’t do this I am otherwise finding it easy to get in the weeds with entities/helpers/automations that I can’t remember always if they are testing or not.

hope this helps.

1 Like

There is a template macro for that.

It may or may not work with your provider/integration.

1 Like

Thank you will read through and see if it works for my price integration (energi data service)

Thank you! Just read upon on Jinja and tbh my first feeling is like some afterthoughts for yaml.
Is there a way to write automation in python that can read/write any state/metadata in HA? As a developer I find this might provide the easiest path for me to convert my DSL rules to something that I can version control.

Check out pyscript in HACS

You can create an application (in python) using one of the following interfaces:

  1. Websocket API.

  2. python_script integration (it’s sandboxed and doesn’t allow importing any python libraries).

  3. PyScript integration.

  4. Appdaemon add-on.

This topic might interest you: