Automation mimic numeric_state trigger, but with other entity's values for above/below

Tags: #<Tag:0x00007f32703c8e68>

Dear all,

as there is still no support to reference input_number states as above: or below thresholds in Numeric State triggers, I am looking for an elegant way to achieve that scenario using template triggers.

Use case:

  • i have a series of motion sensors outside, which report the illuminance in lux. I use a Min/Max sensor to get the current mean value of all sensors. sensor.illuminance_mean
  • i have two Input Number helpers which receive the illuminance at sunset and sunrise, input_number.illuminance_sunrise and input_number.illuminance_sunset (using automations)
  • i have two Statistics sensors, which sample the last 7 value changes of those input number helpers to get the average illuminance outside at sunset and sunrise: sensor.illuminance_sunrise and sensor.illuminance_sunset.

The overall goal is to use the values of sensor.illuminance_sunrise and sensor.illuminance_sunset as thresholds in an automation, which turn on/off outdoor lights if current illuminance falls below sensor.illuminance_sunset or exceed sensor.illuminance_sunrise

This would be my automation, if using other entities in above/below:

trigger:
  platform: numeric_state
  entity_id: sensor.illuminance_mean
  above: sensor.illuminance_sunrise
  below: sensor.illuminance_sunset
  for: '00:05:00'
action:
  service: light.turn_{{ states('sensor.illuminance_mean') | float < states('sensor.illuminance_sunset') | float }}
  entity_id: light.exterior_lights

But using other entity’s values in above/below is not supported. So I go for Template triggers like


trigger:
  - platform: template
    value_template: "{{ states('sensor.illuminance_mean') | float < states('sensor.illuminance_sunset') | float }}"
    for: "00:05:00"
  - platform: template
    value_template: "{{ states('sensor.illuminance_mean') | float > states('sensor.illuminance_sunrise') | float }}"
    for: "00:05:00"

But does template triggers continuously trigger whenever the current illuminance is above or below threshold for more than 5 mins. How can I achieve that they only trigger once, if the monitored entity falls below or goes above the threshold initially?