Dynamic Delay Automation for Temperature-Based Control

I recently came across an interesting challenge while setting up my smart home automation system. I wanted to create a delay in an automation that’s based on the current temperature. For example, if the temperature reaches 28 degrees, I want a relay to turn on and then off after 28 seconds. This sounds straightforward, but I ran into some issues while trying to configure it in YAML. Here’s what I did and what I learned along the way.

First, I looked into the existing YAML setup I had for similar automations. The structure seemed correct, but I realized that setting a dynamic delay wasn’t as simple as just plugging in the temperature value. I found that the delay section in the automation doesn’t accept variables directly, which meant I needed to find a workaround.

After some research, I discovered that using a template condition could help. By calculating the delay value based on the temperature sensor’s state, I could dynamically set the delay. Here’s a snippet of what I came up with:

yaml
if:

  • condition: template
    value_template: ‘{{ (states(’‘sensor.average_wind_speed’‘)|float)+ 2 < states(’‘sensor.vejo_greitis’‘)|float }}’
    then:
  • type: turn_on
    device_id: adcb138847cc90c6fbaa3d0a183cbacd
    entity_id: switch.tasmota
    domain: switch
  • delay:
    hours: 0
    minutes: 0
    seconds: 28
    milliseconds: 0
  • type: turn_off
    device_id: adcb138847cc90c6fbaa3d0a183cbacd
    entity_id: switch.tasmota
    domain: switch

The key here was figuring out how to inject the dynamic delay value into the automation. I realized that using a template to calculate the delay based on the temperature sensor’s state was the way to go. This approach allowed me to set the delay dynamically without hardcoding values.

However, I encountered a few hurdles. The main issue was ensuring that the template correctly parsed the temperature value and converted it into an integer for the delay. I also had to make sure that the automation didn’t trigger unintended actions if the temperature sensor provided an unexpected value.

After some trial and error, I got it working! The automation now dynamically adjusts the delay based on the temperature, which is exactly what I wanted. This experience taught me the importance of understanding how templates and conditions work in YAML automations. It also highlighted the value of community resources and forums where I could find examples and solutions to similar problems.

If anyone has tips or alternative approaches for setting dynamic delays, I’d love to hear them! This was a great learning experience, and I’m excited to apply these techniques to other parts of my smart home setup.