Hey everyone,
I’m trying to turn off a relay with the following conditions (using motion sensor values):
triggers:
- trigger: numeric_state
entity_id:
- sensor.mmwaveofc1_c95e60_existence_energy
above: 20
id: Office1 On Trigger
- trigger: numeric_state
entity_id:
- sensor.mmwaveofc1_c95e60_existence_energy
id: Office1 Off Trigger
for:
hours: 0
minutes: 15
seconds: 0
below: 47
conditions: []
actions:
- if:
- condition: trigger
id:
- Office1 On Trigger
- condition: state
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_1
state: "off"
then:
- action: switch.turn_on
metadata: {}
data: {}
target:
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_1
- if:
- condition: trigger
id:
- Office1 Off Trigger
- condition: numeric_state
entity_id: sensor.mmwaveofc1_c95e60_motion_energy
below: 10
then:
- action: switch.turn_off
metadata: {}
data: {}
target:
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_1
mode: parallel
Basically, I want the relay to turn off when existence energy is less than 47 for 15 mins. (trigger), only if during those 15 mins. motion energy was less than 10 (if condition in action). I’m struggling to have a “for” time in the motion energy being less than 10 for 15 mins. part. I’ve seen threads [old ones mostly] where people recommend using boolean helpers. But I want to avoid this since it’s not just 1 sensor, but many that I’m working with here. Here’s what I mean :
triggers:
- trigger: numeric_state
entity_id:
- sensor.mmwaveofc4_863460_motion_energy
above: 11
id: Office4 On Trigger
- trigger: numeric_state
entity_id:
- sensor.mmwaveofc4_863460_existence_energy
id: Office4 Off Trigger
for:
hours: 0
minutes: 15
seconds: 0
below: 67
- trigger: numeric_state
entity_id:
- sensor.mmwaveofc1_c95e60_existence_energy
above: 20
id: Office1 On Trigger
- trigger: numeric_state
entity_id:
- sensor.mmwaveofc1_c95e60_existence_energy
id: Office1 Off Trigger
for:
hours: 0
minutes: 15
seconds: 0
below: 47
conditions: []
actions:
- if:
- condition: trigger
id:
- Office4 On Trigger
- condition: state
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_4
state: "off"
then:
- action: switch.turn_on
metadata: {}
data: {}
target:
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_4
- if:
- condition: trigger
id:
- Office4 Off Trigger
- condition: numeric_state
entity_id: sensor.mmwaveofc4_863460_motion_energy
below: 10
then:
- action: switch.turn_off
metadata: {}
data: {}
target:
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_4
- if:
- condition: trigger
id:
- Office1 On Trigger
- condition: state
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_1
state: "off"
then:
- action: switch.turn_on
metadata: {}
data: {}
target:
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_1
- if:
- condition: trigger
id:
- Office1 Off Trigger
- condition: numeric_state
entity_id: sensor.mmwaveofc1_c95e60_motion_energy
below: 10
then:
- action: switch.turn_off
metadata: {}
data: {}
target:
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_1
mode: parallel
I’ll probably have 10+ relay-sensor combinations in there eventually and I want to avoid having to create a boolean helper for each. Any suggestions has to how I could template the “for” time in the numeric state condition in actions?
Thanks.
That’s not what your condition is doing… it’s checking whether the current values is below 10. As far as I know there isn’t a way to do what you are describing with just a condition. It might be possible with the new recorder.get_statistics
action… Try the following:
triggers:
- trigger: numeric_state
entity_id:
- sensor.mmwaveofc1_c95e60_existence_energy
above: 20
id: Office1 On Trigger
- trigger: numeric_state
entity_id:
- sensor.mmwaveofc1_c95e60_existence_energy
id: Office1 Off Trigger
for:
hours: 0
minutes: 15
seconds: 0
below: 47
conditions: []
actions:
- choose:
- conditions:
- condition: trigger
id:
- Office1 On Trigger
- condition: state
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_1
state: "off"
sequence:
- action: switch.turn_on
metadata: {}
data: {}
target:
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_1
- conditions:
- condition: trigger
id:
- Office1 Off Trigger
sequence:
- action: recorder.get_statistics
data:
start_time: "{{now()-timedelta(minutes=20)}}"
end_time: "{{now()}}"
statistic_ids:
- sensor.mmwaveofc1_c95e60_motion_energy
period: 5minute
types:
- min
response_variable: history
- condition: template
value_template: "{{ merge_response(history)|map(attribute='min')|list|min <= 10 }}"
- action: switch.turn_off
metadata: {}
data: {}
target:
entity_id: switch.office_makeup_rooms_6ch_relay1_relay_1
mode: parallel
Automations have no ability to access the history of an entity’s state when evaluating conditions.
Up until last release, the only option was to create a new entity which could capture the information you wanted so that it was available for the condition to evaluate.
The method that @Didgeridrew suggests is possible now, however it still doesn’t provide exactly the same functionality. What we need is a recorder.get_history
action, and that doesn’t currently exist. The problem with the 5-minute statistics is that they are (obviously) generated in 5 minute intervals. Which means if your automation triggers at (for example) 11:59:59, then it will return the statistics covering 11:40 thru 11:55. So the time window can be off by up to 5 minutes.
If you want to ensure the correct time window of information is being evaluated, you’ll need a new entity to hold the data you want to evaluate.
There was a similar question with my response here (and explanation here). In my opinion, the easiest solution is to create a trend helper for each level of each sensor.
I’m sure it’s possible to create one very complicated trigger-based template sensor to calculate and hold all the data (instead of individual trend sensors) but that would also make your automations more complex too.
2 Likes
Thanks, I was wondering the same about the 5 min. statistics. Hoping they include a “For” time field soon for the numeric state condition.
I don’t think it’s likely that is ever going to happen. Even if it did, it wouldn’t solve what you are trying to solve… under the current way for
is used elsewhere in HA conditions, it represents whether the current value has met the defined criteria for a minimum duration. It asks the question, “Has this value been below 10 for at least 15 minutes?”
With a trigger that fires the moment when the state value has been less than 47 for 15 minutes, it could only ever pass such a condition if the value changed from above 47 to less than 10 immediately.
I think you’re misunderstanding slightly - the variable that’s supposed to be less than 10 for 15 mins. is not the same as the trigger variable that’s required to be less than 47 for 15 mins. If you re-read the post, the trigger references “existence energy” and the condition “motion energy” - they’re not the same thing.
You’re right I did mistake the two entities. But, the other point still remains… If I understand you correctly, the “question” you want your condition to answer is outside the scope of current condition functions and the addition of a for
attribute would not solve that.
Why do you say that? The trigger allows you to input a “for” value under numeric state - in the above example, automation triggers if existence energy crosses and stays below 47 “for” 15 mins. With the trigger set, I want to now define a condition before executing whatever action the trigger is supposed to set off [which in this involves switching off the relay]. The condition I want to check is this : was motion energy under 10 in the 15 mins. prior to trigger triggering. My numeric state “below” value is 10 for motion energy, similar to how my “below” value = 47 for my existence energy trigger. My “for” value in the trigger is 15 mins. Why can’t I have a similar “for” value field in the motion energy condition? I’m not sure what’s stoppong HA devs from doing this. Such use cases show up plenty so I think it’d be super useful to have some sort of “for” in numeric state conditions. I guess for now I’m going to have to do the arduous task of setting up helper threshold sensors to get through this.
Note that the duration of your trigger is not “was existence energy less than 47 at any point in the last 15 minutes?”; it is “has existence energy been less than 47 continually for 15 minutes?”
Conditions are about current values, and those that include a duration answer the question “Is the value currently x, and has it been that for at least y amount of time?” If a duration were to be added for numeric state conditions, most users would expect it to follow the same framework i.e. “Has the value been below 10 continually for at least 15 minutes?”
There is no current way to get such information from the state object. What you have described would require a new function to query and parse the entity’s history to test if the described condition was true at any point within a window of time… all for a relatively small number of use cases that already have solutions like threshold and history stats sensors.
“Has the value been below 10 continually for at least 15 minutes?”
This is exactly what I was looking for. When I said “was motion energy under 10 in the 15 mins. prior to trigger triggering”, I meant “has it been less than 10 continuously in the previous 15 mins.” and not “was motion energy less than 10 at least once in the previous 15 mins.”.
If a duration were to be added for numeric state conditions, most users would expect it to follow the same framework i.e. “Has the value been below 10 continually for at least 15 minutes?”
I too was hoping it would follow the same framework as in the trigger. The use case that checks whether the numeric state of the entity in the condition dropped below x at least once in the past y mins. is NOT what I’m after.