WTH why can't automations be functional

No I don’t mean that they work but are functional rather than imperative.

It’s no secret that automations get pretty complex or numerous and grow combinatorially. Triggered by conditions can help reduce the number but that simply moves the complexity into fewer automations.

What if there were a better way? What if you could define your outputs in terms of a function of the inputs and not worry about triggers?

As an example, say I have something (e.g. A/C) that I want on during certain times but only if the windows are shut. This would be trivial with analogue circuitry (timer and switch) but in HA, even in the simplest case, you have to handle the window opening and closing, and the time starting and stopping.

What if you could simply define it logically as: ON = (time window) AND (window shut)
HA could take care of how that relates to triggers etc.

This is related but different.

I think you’ve got the wrong product. Still time to change before you spend too much money. :laughing:

I’ve noticed this is becoming a pattern, @RedKing. Someone points out a rough edge in a WTH thread and the reply is “switch product”, “Vote for your own WTH”, or “What difference would it make?” :laughing:

With respect, that’s not really what WTH is for. The whole month exists so people can say what bugs them and maybe get it fixed. If every suggestion is met with “use something else” we’d have a very short month and a very quiet forum.

So maybe it’s not the product that needs changing but the attitude. :wink: If you think the idea is bad, tell us why or suggest a better way to solve it. That would actually help. Pointing people to the exit doesn’t.

It is indeed a pattern! Well spotted. :laughing:

It’s a pattern because the pattern of this year’s WTH seems to be drifting - away from people saying what bugs them towards people wishing for things that would make HA entirely different. An automation without a trigger would not be an automation.

By all means design an app that will allow you to edit automations in a way that seems more intuitive to you (like the Node-RED one) but don’t expect the automations themselves to change. Too late for that - it is what it is.

Edit: “Vote for your own WTH” is a real thing - it’s not automatic.

I had started a replying explaining why what you ask for does not make sense, because you do not want to be monitoring and reacting on states all the time, it’s very inefficient. And then I realized that you asked for something different.
Basically, you want an automation that triggers when a specific logical combination of states switches from true to false, or from false true. And then you want different actions for the different triggers. But you do not want to make that automation with two inverted template triggers and a choose block with trigger ID conditions. You want a UI editor that lets you select the states and the logic and the actions, and then makes the automation with the triggers in the background for you. Did I paraphrase that correctly?
If so, that does sound like the direction HA is heading in, though maybe a bit too forward-thinking for immediate implementation.

That actually warrants its own WTH. But it’s probably a Discourse limitation so out of scope.

Glad we agree on the pattern. :laughing:

But “an automation without a trigger would not be an automation” answers a proposal nobody made. Read the first post again: the triggers stay, HA just works them out instead of the user writing them by hand. allard77 put it very well above.

And “too late for that, it is what it is”? HA already does exactly this. A template binary sensor without an explicit trigger figures out which entities it depends on and re-evaluates when any of them change. Nobody writes those triggers by hand. The proposal is simply to let automations benefit from something HA already knows how to do.

It’s also not the first time “it is what it is” turned out to be wrong. Services became actions, the automation syntax was reworked to triggers, conditions and actions, and the editor has been rebuilt more than once.

So WTH isn’t drifting. People are pointing at things that could be better, which is rather the point. Telling them to build an app or switch product is just “it is what it is” with extra steps. :wink:

That indeed would be a completely different way of defining automations compared to the current state driven automations. Would it be better?

IDK. I find it trivial to implement things like your example with the current system (in YAML).

It’s probably heading the way you want with all the recent development effort being put into the new purpose-specific triggers.

HA was designed by ‘software people’ and the way automations are defined comes from that background - code is executed sequentially. Industrial automation that’s designed by ‘electrical people’ is generally defined more like @jsingleton suggests, based on how discrete electrical devices are wired together. The Ladder Logic diagramming that’s used for most industrial PLCs can be drawn either way around. It ought to be possible to create a visual editor that can be switched between ‘trigger based’ and ‘outcome based’ design modes, with both generating the same ‘trigger based’ YAML output.

OK, well back to the WTH. You could install OpenCode and then tell that what you want the automation to look like, much like OP describes. So, maybe integrating that into core is an option?

That would be a script.

I do not see an exclusive situation. A user describes something like:
Light on from 22:00 to 8:00, else of
Can be translated to a set of triggers. Besides the triggers at 22:00 and 8:00 you need to add triggers on restart etc. The presentation to the user and the internal representation can different. You need helpful transinformation.

Home Assistant’s current automation model is imperative and event-driven—you define the exact triggers, conditions, and step-by-step actions to run. That works well and can handle very complex workflows, especially for one-shot routines and notifications.

​What this WTH is getting at isn’t a replacement for that, but an optional, declarative alternative (similar to functional or logic-based models). Instead of writing out the step-by-step process to reach an outcome—and chasing every edge case or restart—you describe the shape of the solution (for example, “the office light should be on whenever the room is occupied and dark”) and let the engine keep things in sync.

​It wouldn’t take anything away from the existing approach. It would just add a complementary option for continuous, state-based rules where managing individual triggers gets brittle. I think it would be a great addition to HA.

What you describe is called a template
Also see Templating - Home Assistant

Doing things functionally is, as said above, a fundamentally different approach that is not compatible with event driven systems. That is why the remark: “you chose the wrong system” is not so strange. It would require a complere rewrite of the whole automation engine and break everything you have now.

There are merits to the idea, but I believe there are very few things in the home that would benefit from it. In the industry functional behavior would be perfect, but a home is different.

The biggest downside is that all behavior must be in the functional description, including manual override. There is no way to divert from what is in the functional description. It makes your home inoperable by hand unless you program that in, which makes that the functional descriptions become very big and very complex very fast. When the system grows, the functional description grows even faster.

Every exception to the rule needs to be programmed in. Even adding a simple “press the button to toggle the state” or adding a vacation mode suddenly becomes quite hard to do right. In industry there are always very hard rules on what a machine should do, but a home needs to respond to different people with different needs.

As an exercise go sit with your family members and decide on the exact rules on what the setpoint temperature in a room should be for every minute of every year. You can’t. If only because Home Assistant cannot know every variable that is involved, even if you have sensors everywhere.

In your own AC example. Imagine your AC runs into an error and shuts itself off. Your functional behavior would turn it back on immediately, the AC will shut down again, rinse and repeat as fast as the AC will allow it. This will destroy the AC even faster.

The reason pretty much every home automation system uses an even driven approach over hard rules is that it behaves more like humans would, makes it easy to make separate automations that act on the same entities, and intervene manually when needed.

If ever a functional type of automation would be added, it would clash with the event driven ones and you would need to be very, very selective where to use them. You cannot have it both ways.

I wrote an AppDaemon script to do this. It’s been quietly running in the background for three years now, working perfectly. The main use case for me is lights that can be turned on by multiple different sensors (motion detectors, door sensors, physical smart switch, etc.) and need to stay on as long as the conditions are met.

For example, my hallway light comes on in the following conditions:

  • If the motion sensor is detecting motion AND at least the big blind is closed AND its dark outside OR both blinds are closed AND the house is not in sleep mode
  • If the front door was opened from the outside (I use a template sensor with a trigger to combine the states of the door sensor and both indoor and outdoor motion detectors to determine this,) AND its dim outside
  • If the physical switch is on

The rule this looks like this:

light.hallway_ceiling:
  - binary_sensor.motion_hallway_occupancy & (binary_sensor.dark_outside | cover.hallway_blinds=closed) & cover.hallway_blind2=closed & !binary_sensor.bedtime
  - binary_sensor.front_door_opened & sensor.frontyard_motion_illuminance<250
  - binary_sensor.hallway_ceiling_switch

The way the script works is that it listens for changes to all the named entities and whenever they change, the relevant rules are re-evaluated and the output entity’s state is set. The script remembers the last state and only resets the entity when it thinks it has changed. This lets me turn a light temporarily on or off without the script fighting me.

It would be great to have something like this integrated into HA as an alternative automation method with a nice GUI rule builder. This style is not suitable for everything, but doing the above example with normal automation rules would be ridiculously verbose and difficult to update. (The way to do it manually would be to add triggers for all the input entities, then an IF block with a template to evaluate the rules and turn on/off actions for th branches.)

Digressing here.

Ladder logic in industrial controls cycles on read-all-inputs, compute the outcomes, push-all-outputs - just as you would when everything was analog circuitry 40+ years ago.

This burns all the processing power for barely any movement on each individual cycle. The one benefit is your reaction times are very very deterministic if your setup is good, down to less than 100 µs if you push hard on fast hardware.

Now, we don’t need that in home automation. An input state change triggering a specific, isolated little computation (pulling in some few related input states as conditions) followed by a change of equally few outputs, that’s what we have in HA.

And it is good like that. You don’t want to poll battery powered inputs all the time. You don’t want to clog your airwaves updating all the device states that haven’t actually changed (and you don’t want to memorize them off-device either for consistency).

tl;dr … state change triggers automation, and let’s keep it at that.

That is exactly how I build most of my complex automations:

  • trigger on all entities state needed
  • define smart conditions
  • define actions based on IF blocks

If you want non verbose automations you do 2 of them, one for ON, one for OFF

  • trigger on all entities state needed
  • define smart conditions (replaces IF blocs)
  • define actions

This does not make thinks particularly difficult to maintain, and you can use notes, aliases and summary to keep trace of the structure.

You can also even use template triggers to have more fine triggers or conditions (like if (no motion detected AND lights on) for 5 min then shut down) .

I personally do not see really what the actual method makes impossible to do.
But sometimes, you have to scratch your head to make it simple, sometimes the right way is to cut it in two (or more) simpler automations.

And people are allowed to reply to a WTH. Replies aren’t constrained to agreement with the suggestion.

That’s why you have detected a “pattern” because it has been an accepted practice since the very first WTH event was held several years ago.

FWIW, the majority of WTH suggestions are not implemented and there’s no official explanation for the rejection. Therefore it can be helpful when knowledgeable users interject and explain why the suggestion is unlikely to be accepted or how the odds of acceptance improve if the suggestion is modified or how it can already be achieved.

I wasn’t suggesting that the automation should actually work like Ladder Logic, just that an alternative UI could allow you to build automations without starting from the trigger. The automation itself would still be trigger-based, with multiple automations being generated from a single diagram where necessary (which would probably be most of the time).
I’m not sure it’s something that should be part of the core of HA (because it would never be able to work as well for complex stuff), but I think could be done as a custom integration for people who want a simple UI with an ‘outcome-based’ approach.