Change state in template after a specific time

Hi, is it possible to expand this template code so that the status change is executed only if the value is below or above the defined voltage for a specific time (f.e. 30 secondes)?

  - platform: template
    sensors:
      rain_sensor:
        friendly_name: "Niederschlag"
        value_template: >-
          {% if states('sensor.esp_regensensor_analog')|float < 1.7 %}
            sehr stark
          {% elif states('sensor.esp_regensensor_analog')|float < 2.2 %}
            stark
          {% elif states('sensor.esp_regensensor_analog')|float < 2.6 %}
            mässig
          {% elif states('sensor.esp_regensensor_analog')|float < 2.9 %}
            leicht
          {% elif states('sensor.esp_regensensor_analog')|float < 3.1 %}
            tröpfeln
          {% else %}
            trocken
          {% endif %}
        icon_template: >-
          {% if states('sensor.esp_regensensor_analog')|float < 1.7 %}
            mdi:water-alert
          {% elif states('sensor.esp_regensensor_analog')|float < 2.2 %}
            mdi:water-plus
          {% elif states('sensor.esp_regensensor_analog')|float < 2.6 %}
            mdi:water
          {% elif states('sensor.esp_regensensor_analog')|float < 2.9 %}
            mdi:water-minus
          {% elif states('sensor.esp_regensensor_analog')|float < 3.1 %}
            mdi:water-remove
          {% else %}
            mdi:water-off
          {% endif %}

same functionallity as possible in automations (for:)

No because the Template Sensor’s template is updated only at the moment when sensor.esp_regensensor_analog changes state.

1 Like

ok.
But I could make a sceond sensor and update that one with an automation, where I use a condition that the state is only changed if the changed state of the first template sensor is for a specific time :face_with_monocle: :nerd_face:

What kind of service call will the automation use to update the sensor?

sorry, it’s not an sensor, it must be an input_select with the same “states” :wink:

You’re going to use an input_select to store voltage values (numbers)?

Maybe an input_number would be better for storing a voltage value?

I don’t want to store voltage value, I wan’t a “state-machine” who changes the state depending on the voltage value, but only if the voltage value is over the level for a specific time.

the states are:
very heavy (rain)
heavy
moderate
light
drip/dropping
dry

Ah, I see.

To meet your requirement of maintaining a voltage value for 30 seconds before triggering, you could try this (untested) automation:

- alias: test example
  trigger:
  - platform: state
    entity_id: sensor.esp_regensensor_analog
    for : '00:00:30'
  action:
  - service: input_select.select_option
    data:
      entity: input_select.rain_sensor
      option: >
        {% set v = trigger.to_state.state | float %}
        {% if v < 1.7 %} sehr stark
        {% elif v < 2.2 %} stark
        {% elif v < 2.6 %} mässig
        {% elif v < 2.9 %} leicht
        {% elif v < 3.1 %} tröpfeln
        {% else %} trocken
        {% endif %}

FWIW, it isn’t entirely necessary to use an input_select and an input_text would work as well.

1 Like

thank you very much, I will try this :heart_eyes: