How do I create a script?

I am a professional programmer. I have programmed in many languages, but I am currently programming in Python at work.

Through my years of experience, I have found that (in general) that visual programming environments make programming much more difficult. Home Assistant doesn’t disappoint in that respect. (The automation has already been running for weeks turning on/off my Christmas lights.)

That is, I am trying to run a simple script to set a Helper (which is a global variable) to a value based on the date. I have the script written in a Template, but I have no idea how to get it to run at noon then set the value of the Helper. I will want another script (this time called an Automation) to read this value and act accordingly. (The lights will not be turned on except between Black Friday and the first Saturday after January 2nd.)

I have tried reading documentation, but as one used to using a text editor to write code, I am finding the visual aspect of Home Assistant to be a maze of twisty passages that all look alike.

1 Like

Hi Val,

I love the colossal cave reference…

I know it’s painful, but build a simple script in the script UI editor and another in the automation UI editor to get your feet wet.

It does sound like you want to split your configuration and use vscode/codeserver to write your own however.

This document is a bit over complicated and trying to be all things to all people, but it is a start: Splitting up the configuration - Home Assistant
I have found this video helpful run by Frenck, the current principal engineer at Nabu Casa: https://youtu.be/FfjSA2o_0KA?si=_v02Kbs0RDsgcd4b
You can look at my configuration and see how I split things up: HA Config

Perhaps one of these will help you bring light into the dark room, or get your treasure back from the troll.

And I appreciate the counter-sign from CC.

I will try the links you gave and let you know if I get it working.

1 Like

You got lots of coding options for getting the job done with HA. If you want to do stuff outside of HA you can write scripts in any language you want and then generate events you send to HA via MQTT. You then have automation that take actions based on these events. HA has an addon called Appdaemon where can write scripts in python to interact with HA. You can also look at ESPhome and have lower power micro controllers that can run code and interact with HA. HA supports a shell command integration where you can use bash to get things done. As you’ve already noted you can use templates and automation. The web interface allows you to change from visual to yaml editing if you prefer to just write your yaml scripts in a general editor. I run the supervised version of HA on debian so I can have some supporting scripts outside in the language of my choice.

Here’s an example of using ruby gems that connect to HA via MQTT.

Here’s a bash script that monitors GPIO pins and talks with HA via MQTT.

Here’s an example using ESP32 to provide local control of a lamp, but also allowing the lamp to be send touch events to HA to control the overhead light in the same room.

Here I use a command script in HA to send a message out via whatsapp.

Here’s an example of using Appdeamon to control the lights when you’re away from home.

This just shows the HA debian setup I use.

A script is an automation without a trigger or conditions. If you want it to run at a specific time, you need an automation.

That’s part of the problem I’m having; the terminology. Scripts are not scripts, but automations and helpers are.

Actually, they are neither. What we call script or automation is a configuration that gets translated into JSON format. I find it easier to write scripts and automations in yaml. Sometimes I will use the UI to start the process, then switch to yaml. (The UI just creates the yaml code).

I have just recently been introduced to PyScript which lets you write scripts and automations in Python. (I think).

I am far, far from an expert. I can’t write a script without cheating a bit. But I have more than 200 automations and more than 100 scripts in my Home Assistant.

If you have a programming background, you might try looking at yaml code instead of the UI.

Sir_Goodenough,

I watched the entire video you linked. I looks similar to the Azure pipeline programming I have done at work.

However, one of the cautions he said was that you want to be very careful about which YAML files you edit manually because if you use Home Assistant’s visual editor later they will be overwritten. It sounds like I need to choose one or the other, not both.

And that link to split up configuration files is for LARGE configuration files. Right now I just have a script (yes that’s what I’m calling the configuration changes I made in three different locations in Home Assistant) to turn on my Christmas lights every evening. I have another (again in three different places) to turn them off every morning.

My goal was to no longer turn them on from January to Black Friday. With the progress I have been able to make over the past week, I will just unplug the lights from the outlet. That will take MUCH less time.

If you put your manually written ones in the include paths / files, and leave the actual automations.yaml and script.yaml for the UI to edit, you will be fine.

If you look at my stuff I have a symlink in there for scripts.yaml so it shows up in the path and all my manuals and the scripts work. There is the mentioned hack to get the include to parse the automations.yaml in it’s home location.

Basically the UI reads the entire automations.yaml or script.yaml, parses it, then rewrites it when it saves. All comments go away, and it leave ‘machiney’ formatting not ‘humany’ formatting.

Yes you can do both. But edit automations.yaml very carefully. One mistake can screw up all of your automations. The way I edit automations in yaml is very safe. You start with the UI, then select “edit in yaml”. You get the best of both and can go back and forth whenever you want.

1 Like

But I still haven’t figured out how to get my script to run. The scripts (I’m still calling them scripts) I made to turn the lights on and off I did by watching a You Tube video; it was a cookbook approach. There is NO WAY I can get something as complicated as Home Assistant to do what I want.

I have written in event-driven languages before, but Home Assistant is the most convoluted “programming” environment I have ever used.

But you are YAML challenged and a self proclaimed ‘not an expert’… :stuck_out_tongue_winking_eye:

Just kidding, that is indeed another way. Might be better for you as well, I just shared the way I know.

I have already discovered how to edit the YAML directly. I have used that editor to make some changes directly in YAML that I couldn’t figure out how to do otherwise.

Creating the script/automation/helper/template/whatever in the first place is what I haven’t been able to do.

I have created the code in Janja (from code in these forums) and modified it to do what I wanted. (I picked up programming in Janja in less than an hour.) And it produces the expected output, in the Template editor. However, after weeks of trying I still cannot get it connected to Home Assistant.

I have already unplugged my lights. I will plug them in again on Black Friday. Home Assistant can continue to turn on and off the plugs in the meantime.

Jinja by itself cannot accomplish anything. Yes it can read values from Home Assistant, but it cannot write any changes. For that you must use HA’s own YAML-based automation language.

Start by using the visual editor to set up your basic triggers and actions, then go into YAML mode and insert your Jinja templates. For someone familiar with just about any programming it should be quite easy. For simple automations I typically build them like this and leave them in automations.yaml.

For anything even slightly more complicated though I write from scratch, using VSCode over SSH with each automation in separate files. This mostly because I despise how anything in automations.yaml is mangled by HA by expanding shorthands, hard breaking templates into short lines and enforcing unnecessary list indentation which somehow makes YAML harder to parse for my brain…

The more I learn, the more I realize that I don’t know.

I think that I need to know how the data flows in Home Assistant. Is there such a page. They maybe I will be able to understand why scripts aren’t scripts, but there are three other things that are scripts.

It seems to me you are much too hung up on what things are called. Yes, naming of many things in HA is a convoluted mess that just sucks. Chief among them IMHO is that there are way too many different things which are all called “templates”. Does not affect their functionality though.