How do i compare

when creating an automation, is it possible to compare an entity to an input number instead of a fixed value?
what i want to do is keep track of the closest lightning strikes from blitzortung to set an alert level, but how do i compare the current distance to the previous minimum?

yes, i know i have to reset after some time, but i think i can get that part sorted.

That would be via template, some examples in the doc/link and many MANY more in the forum

Template - Home Assistant (home-assistant.io)

Using statistics you should be able to work out something like “show me the max value over last hour”.

Statistics - Home Assistant (home-assistant.io)

And then maybe you can use the combination of template and statistics to achieve what you want to do.

You could use a helper and an automation.

input number:

minimum_lightning_distance:
  name: "Minimum Lightning Distance"
  icon: "mdi:flash-outline"
  initial: 100
  min: 0
  max: 100 # adjust as necessary
  step: 0.01

automation:

trigger:
  - platform: numeric_state
    entity_id: sensor.blitzortung_lightning_distance
    below: input_number.minimum_lightning_distance
action:
  - service: input_number.set_value # set the new minimum
    target:
      entity_id: input_number.minimum_lightning_distance
    data:
      value: "{{ trigger.to_state.state |float(100) }}"
  - # your alert or whatever here

To reset it you have a number of options, probably the best would be triggering on the input_number not changing for an hour or more.

trigger:
  platform: state  # triggers on changing to any state for 1 hour
  entity_id: input_number.minimum_lightning_distance
  to:
  for:
    hours: 1
action:
  - service: input_number.set_value
    target:
      entity_id: input_number.minimum_lightning_distance
    data:
      value: 100

The trouble with using a statistics sensor is that there is no way to set a default if there are no lightning strikes for your sample time, and no way to reset it back to a large value.

i’ve added this to my configuration.yaml:

  - platform: statistics
    name: "Lightning distance (15 min)"
    entity_id: sensor.blitzortung_lightning_distance
    state_characteristic: value_min
    max_age:
      minutes: 15

now i gotta wait for the next thunderstorm to see if (and how well) it works…

Or you update your sensor manually

it doesn’t work all that well. the minimun distance is set but when lightning further away is detected, the value i get back is reset to a higher number, sometimes after a few seconds instead of 15 min.

Which is why I gave you an alternate solution above.

unfortunately that doesn’t work either. the automation doesn’t get triggered, and i can’t find out why. and that crappy new UI in 2022.9 isn’t exactly making it easier :frowning:

Thinking aloud… if you can turn the template into a device-tracker, you could use the proximity option and even see if it is getting closer, or travels away

And proximity’s state is the distance from the zone, which you can monitor in the history graph.


This is not the lighting, but a history of my proximity(which has 2 devices and some ignored zones), but i hope tou get the idea?
:thinking: