"AND" trigger possible?

Is there an “easy” way to create an automation trigger that requires a combine ms condition to trigger?

Specific;
Trigger:
There is still solar energy over
AND
The car is already charging
Action;
Increase charging power.

Or generic
Trigger
A AND B
Action
C

I can of course create an automation with both conditions as triggers and then add both as a condition.
Generic
Triggers
A (implit OR)
B
Condition
A AND B
Action
C

The problem with the latter approach is that it is less readable and in the tracing the automation gets fired a lot which is not needed.

So is there a more ‘easy’ approach?
Note I found an old thread that concluded on the approach to put logic in the conditions (like I described); but i am looking for a more easy approach.
A helper is an approach but I dont think there a GUI supported way to create a conditional helper.

To AND triggers add them as conditions as well.

What’s wrong with…

Trigger A
Condition B is true
Action C

I’m not sure that “The car is already charging” is actually a trigger.

This is the correct way. Triggers are instantaneous events: there’s no such thing as an AND trigger.

1 Like

That’s a bit confusing.

The state trigger from “on” to “off” can stay in the triggered state “off” indefinitely, where a condition can check it. And conditions are AND logic by default. So…

I tried to explain something about when the trigger will occur, but I could not formulate the text good enough.
I deleted the posts, so the confusion is less. :slight_smile:

2 Likes

or use a template trigger. eg:

  • platform: template
    value_template: “{{ states(‘sensor.abc’) == ‘xxx’ and states(‘sensor.efg’) == ‘yyy’ }}”
1 Like

because once A is true and B not, the action is not fired when B becomes true.

Which is why we have repeatedly been saying:

Trigger A
Trigger B
Condition A is true
Condition B is true
Action C

1 Like

In my experience, it will trigger, as the template is evaluated every time either sensor changes state.

2 Likes

that is not true:
In this case I am checking if a power consupmtion is above a certain threshold: Once it reaches the threashold, the trigger is not fired as long as the power is not going below the threshold again.
Or differently: the trigger is not checking when a variable in the condition is changing.
Note: at least this is how I understand triggers and conditions ;-). If I am wrong, can you point to where this is in the documentation?

Agreed: your application of:

needs careful thinking to decide exactly what the triggers are. You’ll need to rethink your logic. This might be a rare occasion where a “check every x minutes” is appropriate. Something like:

trigger:
  - platform: time_pattern
    minutes: "/5"
  - platform: state
    entity_id: binary_sensor.car_charging
    to: 'on'
  - platform: numeric_state
    entity_id: sensor.excess_solar
    above: 1000
condition:
  - condition: state
    entity_id: binary_sensor.car_charging
    state: 'on'
  - condition: numeric_state
    entity_id: sensor.excess_solar
    above: 1000
action:
[script to increase charging power]
1 Like

I do not think so. There are two ways the situation can occur: the solar production increases while the car is charging, or the car starts charging while the solar production is already high enough to cover it. Maybe you could throw in HA restarts, that’s it.

Template triggers work by evaluating a template when any of the recognized entities change state.

Like I said, in my use cases it is working for me, maybe yours is different

Rather than a fixed interval just trigger when the excess sensor updates:

trigger:
  - platform: state
    entity_id: binary_sensor.car_charging
    to: 'on'
  - platform: state
    entity_id: sensor.excess_solar
    not_to:
     - unknown
     - unavailable
condition:
  - condition: state
    entity_id: binary_sensor.car_charging
    state: 'on'
  - condition: numeric_state
    entity_id: sensor.excess_solar
    above: 1000
action:
[script to increase charging power]
3 Likes

As said: this is harder to read -especially when you list of condition is bigger), and the automation gets triggered a lot (making checking what happened more difficult as it gives a lot of ‘traces’ when A or B happen a lot).

And also: when in the time between the triggered is fired, the conditions change (for example triggered from A and A becomes untrue when checking the condition -is this possible? in the framework is the thread of an automation first reading all the variable before applying the logic or is it only reading the variable when needed?), I might miss a ‘trigger’.
I can agree this is for the logic I want to implement not relevant (as I don’t care about short lived events here). But I believe formally
Trigger A
Trigger B
Condition A is true
Condition B is true
Action C
is not the same as
Trigger A AND B
Action C
(since as said A and B can change when we go to checking the condition).

Yeah That’s why I changed the excess solar trigger in my example above to be any change.

1 Like

It is rather the other way; of solar excess > 0W for 1 minute or so… then increase charging power.
Some of my power consuming machines have a very heratic behaviour that making it important that the excess has been ‘stable’ for some time.

I require anyway a logic because part of the logic of increasing the power (and the decrease) requires access to an integration that only allows a limited number of calls per day.

Walter

the actual logic is 'House Power < 0): it is not binary ;-).

Walter
ps: Is this forum having a flag to say my comment here was unimportant?

The HA automation setup might not be perfect, but changes have huge effects, so what you might see as a logic improvement will be step back for many others.

HA is open, so there are other options.
I think you can use something like pyscript for a writing in Python, or you could use NodeRed with a catch all event node connected to a function node where you are using JavaScript to write the code.