Trigger condition problem

Hello all,
I have an automation with two triggers, and as a condition, I’d like the current temperature, the one that triggered the trigger, to be at least 0.5 degrees Celsius lower than the previous one. The problem arises when I go to test the condition, because I don’t know which of the two triggers the value of “trigger.from_state.state” refers to, and it could clearly skew the result of the condition.

triggers:

  - trigger: state
    entity_id: 'sensor.valvola1_current_temp'
    to: ~
    id: 'valvola1'

  - trigger: state
    entity_id: 'sensor.valvola2_current_temp'
    to: ~
    id: 'valvola2

conditions:
      - condition: template
        value_template: |
          {{ trigger.to_state.state | float(0) <= trigger.from_state.state | float(0) - 0.5 }}
          

For example, if valvola1 detected a difference between 18.5 and 18.0 and subsequently valvola2 detected a difference between 19.5 and 19.0, the condition could have the previous value at 18.0 and the current one at 19.0, thus skewing the test.

Since I’ve set an ID for each trigger, is there a way in the condition to precisely identify which of the two triggers it refers to? Or what other solution do you suggest? Thanks in advance.

Well, you can test the trigger.id and/or the trigger.entity_id. But I’m not clear on what you home to accomplish. You are just giving us a solution that does not solve something.

First of all you cannot test this by hand. the trigger variable is set for the trigger and entity that triggers the condition. If you run this by hand, none have changed. But I don’t think this template will solve any real world problem well.

The problem with this template is that it depends on the frequency of updates of the sensor, which is unpredictable. Ten changes of 0.25 within a millsecond after another won’t trigger, but a .5 change since an hour ago will? And then to return false if the next step is .01 soon after. How is that useful?

I think you are trying to implement a derivative sensor the bad way. Create one for each temperature sensor and react on those. You probably want the maximum of both to be above or below some value. It can be used if you want to know if any of both temperatures changes at some predefined rate (for examp,e 0.5 degrees per whatever timeframe you intended).

Hello @Edwin_D ,
sorry, but I forgot to mention that, crucially for everything to work, the valves move in 0.5-degree steps. Sorry again.

But what do you want to see: Any temperature change? Then you don’t need this template at all. Or a rate of change? If you do not know since when and how many changes in a period, you know nothing about the rate of change from the size of the step.

For example, if you see a 1 degree change, and after that nothing changes for 5 hours, the template will remain true all that time. So what does the template tell you? In my book the temperature is stable, not changing. But you do not seem to want the sensor to say true when the temperature does not change, or do you?

Both questions answered: you can use trigger.id or trigger.entity_id to identify which one triggered. But I wouldn’t bother. My solution would be to create two derivative sensors, one for each temperature sensor. If those derivatives are negative the temperature drops, if they are positive the temperature rises, if they are 0 the temperature is stable. The bigger the number is, the faster the change is. React on that.

My goal is, when I detect a drop in temperature in one of the valves, to bring the temperature of the room where it is located back to a certain temperature using heat pumps. I’ve left out all the rest of the automation because it’s not necessary to solve my problem. My requirement is simply the one I expressed in my first post: to detect the drop in temperature of each valve. Obviously, if I create two automations, one for each valve, the problem is solved. However, I wanted to try to unify everything into a single automation.
I’ve looked at the Derivatives, but I don’t understand how to set them up to be useful in my code.

A derivative is a rate of change. You need it to account for the amount of time between changes. In this case the ratte of change is degrees per hour (or some other timeframe). Your automation would look at deriviatives below a certain value to detect a significant drop. I still don’t understand your use case though. Why worry about a temperature drop if it already too hot?

In the beginning of the graph above you see only small (irrelevant?) changes. Low derivative values. Later on temperature changes more dramatically. The derivative values increase. At the end, when there are no more changes (no trigger for you in your current setup!!) the derivative returns to 0.

The graph is clear, thanks. But it seems too sophisticated for my purposes; it would overcomplicate something that should be simpler and more straightforward to manage. If the thermometer drops below a certain degree, the heat pump turns on; with the derivative, the temperature drop could even be between 30 and 20 degrees, but it wouldn’t be of any use to me.

Exactly why I asked what you need this for. You do not need temperature changes, you need temperature difference from a setpoint. What you need is a generic thermostat. If it is too cold, turn it on, if the temperature is fine, turn it off. Your original template is in no way suited to the problem you want to solve.

If you want the simple version, use:

If you want a more sophisticated one, because you want to prevent things like overshoot:

If your heat pump has granular control (not just on/off) use something like:

1 Like

How can you say the rest of your automation is Not Relevant, ?

You are using sensor.temp , i doubt these are template sensors
Your Preferred bedroom Temp, is not (always hardly) the same as your sensor.temp