# Multiple trigger to achieve a calculation

Hi,
looking for a way for doing the following automation. I have some hints but very complicated. Hope you help me to get a more comfortable solution:

I have 2 power sensors… each with a different update time. Sensor 1 with fast update, and Sensor 2 slower.
Need to fire an event when Sensor1 - Sensor 2 above some value… but the question is that i need to detect the event but then wait for the Sensor 2 to update to verify that the event is real and if then…fire the event.

In other words… I measure Power with two sensors. (Power1 is total power, Power2 is subsystem power). The difference is rest of power. When the difference is more than a threshold, i need to fire an event. But the problem is that Power2 could update 2 or 3 seconds later so that Power1- Power2 is NOT over a threshold.
In numbers: (Threshold 1000W)
Time 1 -------- Sensor 1: 2000W Sensor 2: 1700W
Time 2 -------- Sensor 1: 2800W Sensor 2: 1700W (above threshold but have to wait if its real…)
Time 3 -------- Sensor 1: 2800W Sensor 2: 2500W (NOT an event)

Time 4 -------- Sensor 1: 2000W Sensor 2: 1700W
Time 5 -------- Sensor 1: 2800W Sensor 2 : 1700W (above threshold but have to wait to update)
Time 6 ------- Sensor 1: 2800W Sensor 2: 1750W (2800-1750—above threshold FIRE event)

Hope you understand the question.

If Sensor2 always updates after Sensor1, why not just trigger on Sensor2’s changes?

``````alias: example
trigger:
- platform: state
entity_id: sensor.sensor_2
condition: "{{ states('sensor.sensor_1') | float(0) - trigger.to_state.state | float(0) > 1000 }}"
action:
... etc ...
``````

That way if Sensor2 changes every 30s all the time I trigger the automation every 30s although i dont meet conditions. Maybe its an overload for home assistant.
Due to the fact that Sensor1-Sensor2 is above threshold one or two times in a day…im looking to trigger by difference.

Also could happen that Sensor2 doesnt change (maybe 0W) and Sensor1 steps more than 1000W and the automation is not triggered when it should be.
It’s not as easy as it seems to be, so further ideas are welcome. Thanks.

How about this:

``````trigger:
- platform: template
value_template: "{{ states('sensor.sensor_1') | float(0) - trigger.to_state.state | float(0) > 1000 }}"
action:
- choose:
- conditions:
- condition: template
value_template: "{{ states.sensor.sensor_1.last_changed > states.sensor.sensor_2.last_changed }}"
sequence:
- wait_for_trigger:
platform: state
entity_id: sensor.sensor_2
timeout:
seconds: 32 # make this slightly larger than sensor_2's update period.
continue_on_timeout: true
- condition: template
value_template:  "{{ states('sensor.sensor_1') | float(0) - trigger.to_state.state | float(0) > 1000 }}"
- service: do your actions here
``````

This triggers when Sensor_1 - Sensor_2 is above some value (1000) it then checks to see which sensor was updated last, if it was sensor_1 wait until sensor_2 changes and check the calculation again. Only do the actions if Sensor_1 - Sensor_2 is above some value.

Hi, a similar way was the one I had thought. I think this could work. But instead of Sensor2, i have Sensor2, Sensor3 and Sensor4 to substract from Sensor1, each with 30s update period, so how could I adapt it. Should I add this three sensors prior to do this?

Another way i thought of was shift Sensor1 30 seconds (30s delayed Sensor1 signal) and use this “normally”.