Trigger and or Conditions Time

Hello,
I’m new to Home Assitant, I started to use HA 1 month ago. I reach good result with standard components but I found difficulty on creating some automation code starting from webgui.
What I want to do is the activation of climate entity in 2 different time ranges.
The action doesn’t start if trigger and conditions are valid.
If I cancel the conditions it works .

Following the code:

- id: '1560921940108'
  alias: Attivazione condizionatore studio
  trigger:
  - platform: numeric_state
    entity_id: sensor.inside_temperature_studio_inside_temperature
    above: '26'
    for: 00:02:00
  condition: 
    condition: or
    conditions:
    - condition: time
      after: '05:45:00'
      before: '10:00:00'
    - condition: time 
      after: '18:30:00'
      before: '23:30:00'
  action:
  - data:
      entity_id: climate.STUDIO
    service: climate.turn_on

Thanks

Pask

Put your alias in quotes to be 100% correct but that is not going to fix this.

You said it works if you cancel the conditions. did you comment them out and restart or just use the dev tools to trigger the automation?

The reason I ask is because it’s the trigger that looks like it could possibly be incorrect.

Try:

  trigger:
  - platform: numeric_state
    entity_id: sensor.inside_temperature_studio_inside_temperature
    above: 26 # no quotes this is a number not a string
    for: '00:02:00' #added quotes

…You said it works if you cancel the conditions. did you comment them out and restart or just use the dev tools to trigger the automation? ..”
I commented the conditions and restarted the HA. After that the trigger + action works.

I followed your suggestions (quote on duration time and no quote on value) and now it works :slight_smile:
I noticed different behaviour if you use WebGui or manual coding for creating automation routines.
The trigger with quotes has been created by WebGui .
Is it not clear when is mandatory the use of quote and when not.

Thanks Tom!!

Pask

Generally, quotes for everything other than a number. Though there are exceptions.

FYI, in this particular case, I’m pretty sure the problem was missing the quotes around the time in the for: option in the trigger. The quotes around the number in the above: option should not make a difference, although for aesthetics reasons I’d not use the quotes there.

Also, FWIW, you could do this, too:

    for:
      minutes: 2

I changed the code following your suggestions… But it doesn’t work. :frowning_face:
Now the temperature is 30 time is 7:50 but the climate is off

See the updated code below :

- id: '1560921940108'
  alias: 'Attivazione condizionatore studio'
  trigger:
  - platform: numeric_state
    entity_id: sensor.inside_temperature_studio_inside_temperature
    above: 26
    for: 
     minutes: 2
  condition: 
    condition: or
    conditions:
    - condition: time
      after: '05:45:00'
      before: '10:00:00'
    - condition: time 
      after: '18:30:00'
      before: '23:30:00'
  action:
  - data:
      entity_id: climate.STUDIO
    service: climate.turn_on

Thanks

Pask

Is your Home Assistant time correct?

Yes, I printed in lovelace UI … Now time and date is: 2019-06-28, 08:08
When I restarted Hassio the time in the log was 2 hours less : i.e. 5.50 instead or 7.50 but I think is normal at boot time (that is greenwich time)… The Italian time was updated after boot +2

Thanks

Now at 8:14 the automation started and the temp is 1 degree up… 31 instead of 30 … But I put a threshold of 26… :frowning:

I

That’s funny.im also trying to use such a kind of automation for my climate component and like you it doesn’t work by listening to temperature sensors maybe I’ll try I time trigger every 15 minutes and out the temperature in condition. I’ll post my example later .

Your spacing here is incorrect, it should be…

for: 
  minutes: 2

Also, add this to the automation to enable it on HA startup. Place this line directly under the alias line.

alias: 'Attivazione condizionatore studio'
initial_state: true

Ok. I see what is wrong.

Consider this:

Early in the morning (before your time windows) the temperature goes above 26 and stays above 26. The automation triggers after two minutes but the time conditions don’t let the actions occur.

Later on, the only way the automation will fire again is if the temperature goes below 26 then rises above 26 for two minutes during your time window.

A better way to do your automation would be to trigger on time and use the temperature in the condition.

Better is an interval of 15 minutes to check the temperature…if above x then turn on the climate

No, that’s not a problem. It’s not aesthetically pleasing, but it’s not wrong.

This might not be the source of this problem, but I think this is wrong and you should fix it. It might lead to problems later. The times shown in home-assistant.log should be local time. Probably you have the time zone set in the OS (or at least the environment in which HA runs) set to UTC. It should be set to the same time zone that HA is set to. If you don’t know how to do that, search the forum. This has been discussed A LOT.

If you’re tempted to write a trigger that way, then you should stop and think about it some more. It’s almost never the optimal way to write triggers in automations. Sure, there are a few use cases where that’s really the only way to do it, but more often than not there’s a much better, and more efficient, way if you just think about it some more.

@tom_l is right. I can think of two ways to solve your problem.

The first is to add time triggers to your automation for '05:45:00' and '18:30:00', and a numeric_state condition that the temperature is above 26. This is generally what I’ve done in my automations.

EDIT: Oh, and if you want it to do the right thing if HA is stopped and later restarted when the climate is off, but the temperature is above 26 and in the specified time ranges, then add one more trigger using the homeassistant start trigger.

The second is to remove the conditions, then add another automation that turns the first automation on and off at the appropriate times.

EDIT: Oh, and just like the first solution, if you want things to work correctly after a HA restart, you probably need to have a homeassistant start trigger that turns the first automation on or off depending on what time it is.

If you’d like a concrete example of how to do either of these, just let me know.

1 Like

Yes please … I appreciate your help. I am a beginner on HA and a concrete example is better for understanding your suggestions.
Thanks

No problem. Here is how I would do it:

- id: '1560921940108'
  alias: 'Attivazione condizionatore studio'
  trigger:
  - platform: numeric_state
    entity_id: sensor.inside_temperature_studio_inside_temperature
    above: 26
    for:
      minutes: 2
  - platform: time
    at: '05:45:00'
  - platform: time
    at: '18:30:00'
  - platform: homeassistant
    event: start
  condition:
  - condition: numeric_state
    entity_id: sensor.inside_temperature_studio_inside_temperature
    above: 26
  - condition: or
    conditions:
    - condition: time
      after: '05:45:00'
      before: '10:00:00'
    - condition: time
      after: '18:30:00'
      before: '23:30:00'
  action:
  - service: climate.turn_on
    entity_id: climate.studio

This will turn on the climate system when the temperature rises above 26, or at 5:45, or at 18:30, or when Home Assistant starts, only if at that time the temperature is above 26, and the time is between 5:45 and 10:00 or between 18:30 and 23:30.

1 Like

Nice shot I’ll adapt my automation and report if it works well

I also would add condition if you are at home …if you have a device tracker and maybe something that turns off the AC when a window is open I use heaty for this . In heaty the schedule is set to off and it kicks in after 15 minutes …so the AC doesn’t run all the time

So YAML doesn’t require 2 spaces on indentations any longer? My config won’t load if the spacing is incorrect.

It’s never required exactly 2 spaces to my knowledge. What it does require, however, is for indentation under a given key to be consistent. So these are ok:

a:
  b: c
  d: e
f:
     g: h
     i: j

but these are not:

a:
  b: c
   d: e
f:
   g: h
 i: j
1 Like