Automation constantly running

I have the below code set to turn on a light when the weather condition changes. It functions, though it is constantly firing. I only know this as I have it trigger IFTTT and my IFTTT activity for this Automation shows it constantly activating.

How may I had a timer of sorts to this? Allow it to check only every 30 mins. Possible? Thanks.

- id: '1570136486395'
  alias: Weather-Not Sunny
  trigger:
  - platform: template
    value_template: "{% if states('weather.home') != sunny %}\n  true\n{% endif %}\n"
  condition:
  - condition: state
    entity_id: sun.sun
    state: above_horizon
  action:
  - data:
      message: Weather-Not Sunny
    service: notify.notify
  - alias: ''
    data:
      entity_id: light.living_room_shelf_lamp
    service: light.turn_on

you could do a time pattern for the trigger and move your existing trigger into the condition.

And why are you putting in the newline characters? those aren’t necessary.

- id: '1570136486395'
  alias: Weather-Not Sunny
  trigger:
  - platform: time_pattern
    minutes: "/30"
  condition:
  - condition: template
    value_template: "{{ states('weather.home') != sunny }}"
  - condition: state
    entity_id: sun.sun
    state: above_horizon
  action:
  - data:
      message: Weather-Not Sunny
    service: notify.notify
  - data:
      entity_id: light.living_room_shelf_lamp
    service: light.turn_on

That’s because of the way your value_template is constructed. It is performing a string comparison except the word sunny isn’t presented as a string. Therefore the test never matches the word sunny and always results in true.

Here’s an example of what I mean. The state of my security system is disarmed. I use two templates to compare its state to disarmed (not a string) and 'disarmed' (a string). Compare the results.

The first template will never match the word disarmed, because it’s not presented as a string, and so will always result in true.

All you need to do is change your value_template to this:

    value_template: "{{ states('weather.home') != 'sunny' }}"

Crap! I didn’t catch that.

Thanks Taras

1 Like

Thanks finity for the input…good to know going forward.

I’ve had this coded for several days and it does not seem to be firing at all. No entries for it in the log and the light does not change. Any suggestions?

- id: '1570136486395'
  alias: Weather-Not Sunny
  trigger:
  - platform: template
    value_template: "{{ states('weather.home') != 'sunny' }}"
  condition:
  - condition: state
    entity_id: sun.sun
    state: above_horizon
  action:
  - data:
      message: Weather-Not Sunny
    service: notify.notify
  - alias: ''
    data:
      entity_id: light.living_room_shelf_lamp
    service: light.turn_on

During daylight hours when it’s not sunny, paste the template into the Template Editor and see what it reports:

{{ states('weather.home') != 'sunny' }}

It should report True.

Now here is what I think may be happening:

The Template Trigger will be evaluated only when the state of weather.home changes. So if, for example, it changes from sunny to ‘cloudy’, the template will be evaluated. If it then remains ‘cloudy’ for the next three days (during daylight hours) that’s not a state-change and the Template Trigger is not evaluated.

So how’s the weather in your part of the world? :slight_smile:

1 Like

Yep, it reports True.

Excellent explanation. Now, each morning, I want HA to determine what the weather is and appropriate turn on/off the light, since it was in a different state overnight. Do I need to write new code for this?

Weather for me is cold…waking up to 40’s F (it’s all relative) in Central Texas, USA.

There are several ways to improve this and it all depends on how you want it to behave. Let’s say the only extra thing you want is to force this to be evaluated at 8:00 AM.

Add a Time Trigger to run at 8:00 AM
Add a Template Condition to confirm that {{ states('weather.home') != 'sunny' }}

I realize that the condition seems redundant (given that there’s a Template Trigger using the same template) but now it’s needed because when the automation triggers at 8:00 AM it should only proceed to execute the action if it’s not a sunny morning.

How does this look? I set it to fire at sunrise instead of a specific time.

- id: '1570136486395'
  alias: Weather-Not Sunny
  trigger:
  - platform: template
    value_template: "{{ states('weather.home') != 'sunny' }}"
  - event: sunrise
    offset: '+00:25'
    platform: sun
  condition:
  - condition: state
    entity_id: sun.sun
    state: above_horizon
  - condition: template
    value_template: "{{ states('weather.home') != 'sunny' }}"
  action:
  - data:
      message: Weather-Not Sunny
    service: notify.notify
  - alias: ''
    data:
      entity_id: light.living_room_shelf_lamp
    service: light.turn_on

Will '+25' be understood to mean 25 minutes after sunrise? I would’ve thought it should be something like '00:25:00' or '00:25'. Well, we’ll know tomorrow morning. :slight_smile:

Ah, yes, I’ll make that change…thanks for the help.

This worked for me, although I broke it out into it’s own Automation.

FYI

The custom in this forum is to tag the post that solves the original problem as being the ‘Solution’. By tagging you own post as the ‘Solution’ it suggests nothing finity or I contributed helped you solve the original problem.

I had not meant to tag myself, I’ll make that change. Do you guys earn points or something for solutions. What was not mentioned is I said thanks in a reply, not to mention I actually came back here to mark a solution. Whatever.

Points? Of course. I’m very close to collecting enough for a one-week trip to Tahiti. :palm_tree: :wink:

Nope, no points. By marking a post as ‘Solution’ (only you, the author of the topic, can mark a post) a checkmark will appear next to your topic’s title, signaling to others than this topic/problem has an accepted solution. In addition, a link will appear beneath the first post that leads to the Solution. All of this is to help other users, who are facing a similar problem (or are simply interested in learning more), to find answers.

Marking the post containing the problem-solving idea is simply a fair practice; it acknowledges the source of the solution. Otherwise you get the following situation (and I’ve seen it happen):

  1. A user asks for help to solve a problem.
  2. Several people provide assistance.
  3. At least one person’s idea is the key ‘problem solver’.
  4. The user takes that idea and, with few or no significant changes to it, re-posts it as the Solution.

I don’t think that’s how the Solution tag was designed to be used. If everybody did that then this forum would look like everyone ultimately solves their own problems. :man_shrugging:

1 Like