Add a new automation type "Condition based Automation"

PROBLEM
Jump directly to “SOLUTION/SUGGESTION” if bored!

Before introduced to Home Assistant half a year ago I used to implement my customized automations via a few SIEMENS Logos. For those who don’t know: This devices are equipped mostly with a handful of digital (and/or analogue) inputs and relay outputs, extendable via modules. The programming inbetween is based mostly on logic modules (such as AND, OR, NOR etc.) as well as time function blocks (time delay, impulse etc) and so on.
After installing Home Assistant I integrated these Logos via Modbus and moved some of the automations to Home Assistant while others are still done by the Logos to preserve a basic independency.

While working with Home Assistant I noticed that sometimes doing automations is a bit complicated because there could be a confusion between triggers and conditions.

I wanna give you an example:
I want to be the climate preset be ‘comfort’ when I am at home between 1 P.M. and 10 P.M., otherwise it should be ‘eco’. I want it also to be ‘eco’, if any window is open.

Here we go:

  • When I am at home at 1 P.M. already, the heat schedule is the trigger;
    my home state and the closed window states are the confirming conditions.
  • When I come home later, my home state is the trigger;
    the heat schedule and the closed window states are the confirming conditions.
  • When I open any window and close it again, the window state change becomes the trigger
    and so on. I think you got the idea.
    And, of course, the opposite state changes (me leaving home, heat schedule runs out or any window state changes to ‘open’) should be the trigger to change the climate preset back to ‘eco’.

Just for comparison: To implement this very automation via Logo would just have been:
Combining my ‘home’ state, the heat schedule and each window’s ‘closed’ state via AND function, the output would be the climate preset (1 = comfort, 0 = eco). Done!

Another example:
I also use the window state to switch on a little signal light next to the front door via automation to remind me to close the window again before leaving the house. So the light has to be on if any window is open.
To automate this in Home Assistant I had to create two automations:
Automation No. 1: Any window state is set as trigger (change to ‘open’) to turn on the light.
Automation No. 2: Any window state is set as trigger (change to ‘closed’) as well as condition (state ‘closed’) to turn off the light. (Without the conditions the light would be turned off by closing any window even if other windows are still open!).

Again for comparison: To implement that very automation via Logo would just have been:
Combining the ‘open’ states of each window via OR function, the output would be the lamp. Done!

SOLUTION / SUGGESTION

So I’d like to suggest to add a new type of Automation, the “Condition based Automation” without triggers and just conditions that are permanently (or optional periodically) checked. You can interlace multiple layers of logic functions to make any constellation desired.

Because of the missing trigger we now need two actions to configure:
The ‘Action when the result of the conditions turn to TRUE’ and
the ‘Action when the result of the conditions turn to FALSE’.
Because of that I’d like to have a little switch to link both so that, if turned on, every action entry I add to one of the action fields will appear reversed on the other field automatically.
So if I add the ‘Turn light x on’ entry to ‘Action when […] the conditions turn to TRUE’,
the ‘Turn light x off’ entry will automatically be added to ‘Action when […] the conditions turn to FALSE’.

In Home Assistant, the main difference between an automation and a script is an automation has triggers but a script does not.

  • A script does nothing until it is executed (for example, manually by the user, via a card, or an automation).

  • An automation has triggers which actively monitor state-changes and/or events. It executes its actions when triggered (assuming its conditions are met).

In Home Assistant, conditions monitor nothing. They simply act as passive filters to determine go/no-go and are evaluated after the automation is triggered.

Only triggers “monitor” whereas conditions “evaluate”.


Much of what you described, involving multiple stimuli and constraints, can already be achieved with a Template Trigger or a set of matching triggers and conditions (you may already be doing this).

FWIW, what you’re asking for is to have Home Assistant behave a very different way (to the extent of redefining the terminology used to define an automation’s parts, namely making conditions behave like triggers).

Post the code for the two automations.

1 Like

I’m reading this as: rather than learning how to write efficient state driven automations you want Home Assistant to change to what you already know. Which is unlikely to happen as polling continuously is far less efficient (though there is actually a way to do this with the available triggers it is not recommended).

So start learning, your example is easily implemented. If you need help with it then open a topic in the Configuration category. There are many experienced users here that can help you.

1 Like

The Node Red addon might be an option more aligned to your Siemens Logos experience.

While doing it this way is great for industry where the rules need to be 100% true, it is definitely not for homes. Because this would mean there’s no longer a way to intervene. Your automation would undo any manual control immediately.

For your example, there will be no way to manually intervene in the mode of the climate mode. For a motion based light, it would mean you cannot turn on the light if there’s no motion (or off even if there is motion). you’d have to code every (and I really mean every) exception in.

The event driven paradigm that Home Assistant is based on is an entirely different concept. It is much more suited to Home Assistant behaving like other humans in the house. Doing what it is needed (only) when it it is needed, while allowing you to control things manually too. Adding a completely different approach on top of it would break what is already there.

I think this is backwards logic in HA.
The home state and closed window should be the trigger, the schedule the condition :thinking:

3 Likes

Node red will be the same as home assistant. It’s event driven. What the op is asking for is automations that are evaluated like a plc where the conditions are continuously evaluated.

All the automations asked for can be done in home assistant, it just requires thinking about the events that will fire them.

Here we go: Automation No. 1:
alias: Blinken wenn Fenster auf
description: “”
triggers:

  • trigger: state
    entity_id:
    • binary_sensor.affenstall_giebelfenster
    • binary_sensor.affenstall_linkes_erkerfenster
    • binary_sensor.pumakafig_giebelfenster
    • binary_sensor.tiefseeaquarium_giebelfenster
      to: “on”
      conditions:
      actions:
  • action: switch.turn_on
    metadata: {}
    data: {}
    target:
    entity_id: switch.notification_flash_signal_5_5
    mode: single

Automation No. 2:
alias: Blinken stopp wenn Fenster zu
description: “”
triggers:

  • trigger: state
    entity_id:
    • binary_sensor.affenstall_giebelfenster
    • binary_sensor.affenstall_linkes_erkerfenster
    • binary_sensor.pumakafig_giebelfenster
    • binary_sensor.tiefseeaquarium_giebelfenster
      to: “off”
      conditions:
  • condition: state
    entity_id: binary_sensor.affenstall_giebelfenster
    state: “off”
  • condition: state
    entity_id: binary_sensor.affenstall_linkes_erkerfenster
    state: “off”
  • condition: state
    entity_id: binary_sensor.pumakafig_giebelfenster
    state: “off”
  • condition: state
    entity_id: binary_sensor.tiefseeaquarium_giebelfenster
    state: “off”
    actions:
  • action: switch.turn_off
    metadata: {}
    data: {}
    target:
    entity_id: switch.notification_flash_signal_5_5
    mode: single

As you can see, I had to put every window contact three times.
I mean, yes it’s possible but a little to complicated.

No it wouldn’t because the automation would only execute an action when the conditions are changing. For example:
I am at home, the windows are closed and the schedule’s state is on, in this case the climate preset is ‘comfort’, right? So imagine the schedule runs out so the conditions are changing and the automation executes the action to switch the climate preset to ‘eco’.
After that I will be able to manually switch the preset back to ‘comfort’ if needed. The automation will not prevent that or overwrite the new state until next time the conditions are changing to FALSE again.

That’s the great difference between Home Assistant and LOGO because in case of the LOGO your statement is correct: There’s a permanent link between the conditions and the output without any chance to intervene. That’s not the case in Home Assistant.

That would lead to the following problem:
On days when I stay at home (and as a result my state will not change to ‘home’ at any time), the trigger is missing. So I have to set the schedule also as a trigger.

Here’s how I would do it using a single automation.

Create a Binary Sensor Group helper containing all four binary_sensors.

Assuming you name the Group helper binary_sensor.fenster, the automation is simply this:

alias: Blinken Fenster
triggers:
  - trigger: state
    entity_id: binary_sensor.fenster
    to:
      - 'on'
      - 'off'
conditions: []
actions:
  - action: switch.turn_{{ trigger.to_state.state }}
    target:
      entity_id: switch.notification_flash_signal_5_5
mode: single

The reason the State Trigger has explicit values for to is to prevent the slight chance of triggering to the unavailable state.


NOTE

If you want to incorporate more things in it, like schedules, etc then post the way you currently do it and I will review it and let you know if there’s a more optimal way.

1 Like

Didn’t know there’s a helper “Binary Sensor Group” that works like an ‘AND’ or ‘OR’ function. Unfortunately, it only works as long as all input types are binary sensors.
So this won’t work for me because as soon as I changed the window sensors to Senoro (I attempt to order as soon as they are available again) they are no binary sensors anymore because they will delivery not two but three states (open, close and tilted).

The example I posted above, employing a Binary Sensor Group helper, works exactly the way your two automations work.

Reference

Group integration

That wasn’t mentioned earlier and represents a different scenario.

How are they represented in Home Assistant? As sensor entities?

Yes, they will be represented as sensor entities.

I didn’t mention it because using Senoro window sensors is a project of mine for the future. They are very new and the first batch was sold out very fast after simon42 tested and recommended them in a YouTube video two weeks ago. Right now me (and many others) are waiting for them to be available again.

However I’d love to be independence by the entity type (sensor, binary sensor, boolean input etc.) when combining them like they are in the binary sensor group.

So how would that change it?
Do you need tilted or open as open, or do you need separate tilted, open and closed some how?

Then the simplest solution is to use time trigger, set it to execute automation every, lets say, 1 minute. Then it will trigger independently of actual triggers, you want to avoid, check conditions, which you want to be checked and based on their state execute actions… or not. No need to change anything in automation logic in HA.