Repetitive configurations, templates, or...?

As I dive into Home Assistant I keep hitting the “buy” button on Amazon for sensors and such, and one thing I am finding is much of my config file is littered with repetitive configurations.

As an example, the zooz sensors provide temperature, humidity, motion and battery (among others).

Temperature (for reasons a bit unclear but history graph does not work reliably otherwise) require a define a replacement sensor with a value template. Plus one day I may want to include a calibration calculation there.

Battery requires a template to pull the battery level out via attributes.

Motion requires a value template to compare against a specific return value (8).

None of this is complex, all of it though produces a lot of typing. More to the point, if I decide to change something, a lot of changes to keep consistent.

Are there any sorts of macro/pragma type functions available, some way to create a section (or a file) that you can call over and over with different parameters?

Linwood

What you may be looking for are YAML’s anchors and aliases.

https://confluence.atlassian.com/bitbucket/yaml-anchors-960154027.html

Ah… I was looking for a Home Assistant fix, it did not occur to me to look more thoroughly at YAML. Thank you. Now to try it and rewrite my config file.

You may also want to look into AppDaemon. When I found myself repeating lots of motion sensor/light switch/alarm zone automations, I wrote an AppDaemon Python script once and just instantiate it many times with different switches/lights/zones passed in. I can do a heck of a lot of work with a small amount of simple Python code.

Anchors are really helpful in lovelace when you use styles and the same card type but only change the sensor. When I found them, it cut my lovelace from 4k down to 1k lines.

So can I get a sanity check?

It appears that replacements (i.e. the Less-colon-colon) have to be whole lines, there is no token based version. So if your sensor has a long formula on a value template, and one name part is different, you still have to type the whole line in each instance. Right?

Also, you cannot do replacements that span … is the name process? So if a given device sensor needs several sensors and a binary sensor, you can invoke a definition for these, but it is several invocations one for each derived sensor, and a separate one in a separate place for the binary sensor.

I also could really use an example for lovelace, because this line-oriented aspect is confusing me. If I had wanted to report temperature, motion, luminance and humidity on a separate card for each room it seems ideal, except each entity definition line has the name of the sensor which has to be replaced. Now it would be real handy I think for skinning cards with other settings you want to change en mass, but the core part has to be re-typed (as opposed to just replacing something like “sensor-id” with “kitchen” and “laundry_room” and “master_bedroom”.

Am I missing anything?

Using anchors is only useful when replacing 2 or more line templates, it also doesn’t work with indentations. At least I couldn’t get it to work. If you have 10 lines that need 10 different results for every instance, anchors are pointless.