Automation - When - If - Then

Hi All

i have a simple automation that doesn’t work
I need to turn on a plug when a temperature sensor is over 28°c and the time is 2pm.
I did that but the plug never start
If i’m removing the “if” all works

what is the issue?

thanks

would you mind to share the yaml configuration? thank you

This is not what your trigger does. It will turn on the plug if the temperature goes from 28 to 29, but only if it is after 2pm. If the temperature is already over 28 at 2pm nothing will happen.

Change your numeric state trigger to a template trigger.
Numeric state requires you to cross the threshold for it to trigger. Template trigger simply has to evaluate if the condition is true.

I do something similar to monitor the temperature of some external hard drives:-

{{ states('sensor.main_hdd_temperature')|int(0) > 44 }}

Assuming you mean “at 2pm or later” rather than “at 2pm exactly

To run the action when two things are true at once, you need two triggers:

  • temperature going over 28 (as you now have it)
  • time at 14:00 (new trigger)

and two conditions:

  • time after 14:00 (as you now have it; maybe “13:59” for safety)
  • temperature is above 28 (new condition)

Possible sequences of events:

(cold)

  1. 14:00 trigger fires
  2. temp is only 25, so temp condition blocks action
  3. temp goes above 28 later, temp trigger fires
  4. temp and time conditions pass, action run

(hot)

  1. temp goes above 28 at 13:00, temp trigger fires
  2. time condition blocks action
  3. 14:00 trigger fires
  4. temp and time conditions pass, action run

Your current automation will not run the action if the temperature is already above 28 at 14:00.

If you want the automation to only run at 2pm if the temperature is high, and you don’t care what the temperature is earlier or later, then switch your trigger and condition around: trigger at 14:00, check the temperature.

2 Likes

That’s not the problem…

Both numeric state trigger and template triggers (and every other trigger) will always only fire if the trigger changes from false to true.

if the trigger (either numeric or template) is already true at 2pm then the automation will never run the actions regardless of what type of trigger you use.

You’re right. My logic was not sound.
I was thinking about a generic entity state trigger that would fire whenever the state of the entity changed (regardless of its value), then use a template in the condition and misremembered another automation I thought did the same thing.

1 Like

yes if you use a stateless state trigger (by leaving the “to:” blank or omitting it altogether) the trigger will turn true on every state change. so you are correct in that. but since they only care about a specific state change (from 28 to 29+) then just use that instead of trying to trigger on every state change.

alias: Accendere - Ventilatore Dreo 28°c
description: “”
triggers:

  • trigger: numeric_state
    entity_id:
    • sensor.sensore_camera_temperature
      above: 28
      conditions:
  • condition: time
    after: “14:00:00”
    actions:
  • type: turn_on
    device_id: ff1fc6d6318a606e476f0a43bcb3aca9
    entity_id: 7c0f1df2cf9578c353c545c335278860
    domain: fan
    mode: single

hi
thanks

i did it

alias: Accendere - Ventilatore Dreo 28°c
description: “”
triggers:

  • trigger: numeric_state
    entity_id:
    • sensor.sensore_camera_temperature
      above: 28
  • trigger: time
    at: “14:00:00”
    conditions:
  • condition: time
    after: “14:00:00”
  • condition: numeric_state
    entity_id: sensor.sensore_camera_temperature
    above: 28
    actions:
  • type: turn_on
    device_id: ff1fc6d6318a606e476f0a43bcb3aca9
    entity_id: 7c0f1df2cf9578c353c545c335278860
    domain: fan
    mode: single