Sensor for storm detection

With all the new helpers I am looking for a good way to implement this into a sensor:

wind is over 25 km/h
>> storm is on
if wind is below 10 km/h for 20 minutes 
>> storm is off

I can work with time helpers, but I wouldn’t be surprised if there is an easier way.

Any ideas?

I would make it an automation to turn on/off a boolean.

numeric trigger above 25.
numeric trigger below 10, for 20 minutes.

1 Like
template:
  - trigger:
      - platform: numeric_state
        entity_id: sensor.wind
        above: 25
        id: "true"
      - platform: numeric_state
        entity_id: sensor.wind
        below: 10
        for:
          minutes: 20
        id: "false"
    binary_sensor:
      - name: "Storm"
        icon: "{{ 'mdi:weather-windy' if trigger.id == "true" else 'mdi:weather-sunny' }}"
        state: "{{ triggger.id }}"
1 Like

Sorry for a potentially stupid question, but I’m still confused with YAML syntax. Your example is valid if included in the configuration.yaml file, isn’t it?
If I want it to be in a separate template.yaml, what should it look like?

configuration.yaml

templates: !include templates.yaml

templates.yaml

- trigger:
    - platform: numeric_state
      entity_id: sensor.wind
      above: 25
      id: "true"
    - platform: numeric_state
      entity_id: sensor.wind
      below: 10
      for:
        minutes: 20
      id: "false"
  binary_sensor:
    - name: "Storm"
      icon: "{{ 'mdi:weather-windy' if trigger.id == "true" else 'mdi:weather-sunny' }}"
      state: "{{ triggger.id }}"

Many thanks!

Thanks, that looks good. What happens if I restart Home Assistant? Will it still reset?

What about this more complex scenario:

sensor_1 is on or (sensor_2  is on and sensor_3 is above 25)
>> on
sensor_1 and (sensor_2 is off or sensor_3 is below 25  (both since 20 minutes))
>> off

I’m not sure. I don’t have any triggered templates. But all my other template sensors restore.

See if you can work it out using the documents and the example I showed above:

and and or are just that (the words) in a template.

Use the developer tools template editor to test your templates, post here if you get stuck.

1 Like

Thanks, I guess I could skip the trigger part and just check for a time helper. This way it will work after a restart.

The same could be done for the more complex scenario. I just need some additional automations that set the time helpers.

You could use a time pattern trigger but it would be more efficient and respond faster if you use a home assistant start event trigger, you would also have to adjust your state template.

I usually do a trigger on event and trigger on time pattern to be more robust.

But I thought that templates refresh, when a change ist detected on one of the entities. Isn’t that so?

Yes, which is why you dont need the time pattern trigger.

That does not make anything more robust.
You are only using resources for nothing. If you trigger on the event that will happen as the event happens and the time pattern will always trigger too late however shot the time pattern is.

Yes, but if the event does not trigger it becomes important.

For example after a restart it goes from unknown to on and won’t trigger off to on. That can happen if it changes during reboot.

Correct me if I am wrong though.

That is why there is a home assistant start event.
If you have this as a trigger and the state changes then it should work

I will consider this, thanks.