Automation runs no matter what

Hey guys. I can’t figure this one out. The condition doesn’t seem to matter. The idea is the action should run if the inside temperature is >80 and outside is <75, but it runs everytime.

- alias: 'Notify - Too Hot, Open Windows and Turn on Fan'
  trigger:
    platform: template
    value_template: "{{ state_attr('climate.home_heater','current_temperature') | float > 80 }}"
  condition:
    - condition: state
      entity_id: binary_sensor.nest_away
      state: 'off'
    - condition: template
      value_template: "{{ states('sensor.dark_sky_apparent_temperature') | float < 75 }}"

I don’t have sensor.dark_sky_apparent_temperature in my system but look how your template behaves when evaluated in the Template Editor:

It returns True. Why? Because if the states() function can’t get a proper value (in my case because the sensor doesn’t exist), it returns 0 by default. That’s less than 75 so the template produces True.

The point here is that the template can work in a way you may not have expected. Ensure the sensor you’re passing to the states() function is valid. In addition, ensure the sensor’s state contains a valid numeric value. Because if it doesn’t, I think you can guess what the result of this will be:

{{ 'junk' | float < 75 }}

If you guessed True, you’re right. If the float filter encounters a value it can’t convert, it defaults to 0.

Thanks for the reply.

I get what you are saying, but I do have the sensor with a value. Also, I thought changing things to float converts things from a string to a numeric value. The template checks below seems to come out properly.

To be thorough, I am calling this automation from automation, but assume that should matter due to conditions.

{{ states('sensor.dark_sky_apparent_temperature')}}    #Result 62.6
{{ states('sensor.dark_sky_apparent_temperature') | float }}    #Result 62.6
{{ states('sensor.dark_sky_apparent_temperature') | float < 75 }}    #Result True

{{ state_attr('climate.home_heater','current_temperature')}}    #Result 78
{{ state_attr('climate.home_heater','current_temperature') | float }}     #Result 78.0
{{ state_attr('climate.home_heater','current_temperature') | float > 80 }}    #Result False

When you say it ‘runs everytime’ are you testing it by triggering the automation manually (i.e. via the Services page)?

Because if you are then you should know that manually triggering an automation like that will skip the trigger and conditions sections and execute the action.

I’ll assume you’re not doing that and saying that the automation ‘runs everytime’ when activated by its trigger and therefore doesn’t respect the condition you’ve defined. Correct?

FWIW, I see nothing fundamentally wrong with your automation. It triggers when home_heater > 80 then checks if apparent_temperature is < 75. If this arrangement ‘runs everytime’ then the main culprit is the condition. If we exclude all the edge-cases I outlined earlier, I see no reason the template would always evaluate to True.

Unfortunately, I already realized that before coming to the forum. When I arrive home, it literally tells me via Alexa the inside and outside temp (logic should fail) and then runs the automation. I’m glad I’m not the only one puzzled by this. Thanks for the attempt. There is only 1 thing left I think I can try. I can upgrade to latest version I suppose. I’m running 0.95.4 so hopefully 0.96.5 helps.

I’m confused. How does it run your automation directly after you arrive home? Is this just by luck? Are you calling automation.trigger service? As it stands your current automation has nothing to do with you arriving home.

You are corrrct. I use automation.trigger.

Can you add multiple conditions to get around it?

{{ states('sensor.dark_sky_apparent_temperature') | float < 75 and states('sensor.dark_sky_apparent_temperature') | float != 0 }}

or something like:

{{ states('sensor.dark_sky_apparent_temperature') | float < 75 and states('sensor.dark_sky_apparent_temperature') != 'unknown' }}

Read @123 's comment.

Take a look at the docs as conditions can also be part of an action which will probably solve your issue.

Your welcome but it wasn’t so puzzling after all because then you said this:

There it is. That’s exactly how you manually trigger an automation from the Service page. If you call the automation.trigger service, it executes the automation’s action without evaluating its condition section. Effectively, you are not waiting for the automation to be activated by its own trigger, and filtered by its own condition, but you are forcing it to execute. And so it does, dutifully running your automation’s action.

Whether this a benefit or a deficiency depends on one’s needs:

  • If I absolutely, positively always want the conditional aspect evaluated then I’d move it into the action section.
  • If I occasionally want to only execute the automation’s action portion (effectively using it like a script), then I’d leave the conditional portion in the condition section.

I guess the question is “Why are you using the automation.trigger service?”

And that’s why your conditions are being skipped.

Aaah… okay… so how would I call another automation from within an automation if I want the conditions in the second automation to be evaluated?

I suppose an action with a template value with the conditions in it?

Some more context info:
I have an automation that runs when I return home and open the door. Part of the actions of that calls on this second automation to perform additional actions based on the conditions… I didn’t think it was relevant to show that.

Fire an event with the first automation and use that event as a trigger for the second.

Or

Add another coming home trigger to the second automation

Or

Move the conditions to the action of the second automation

One automation calling another another automation is a strange arrangement. An automation’s purpose is to execute its action when activated by its own trigger. In other words, wait for something specific to happen and then do the following task.

You can simply move what’s currently in the condition section to the action. Now if you manually trigger the automation, the condition will be evaluated.

Perhaps what you want is better served by a script? For example:

  1. Move the automation’s condition and action to a script.
  2. Change the automation’s action to simply call this script.
  3. Now you can call the same script from other automations.

Thanks everyone! I think I got it now. I think my brain was using Automations like Scripts. I’ve now converted my conditional action as a script and updated my “Welcome Home” automation action with a service call to that script – based on parameters. It’s working now.

Automation

- alias: 'Announce - IOS Welcome Home'
  trigger:
    platform: state
    entity_id: 'binary_sensor.front_door_opened'
    from: 'off'
    to: 'on'
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: device_tracker.iphone_ios
      state: 'home'
  action:
    - delay: '00:00:02'
    - service: media_player.alexa_tts
      data_template:
        entity_id: media_player.living_room_echo
        message: "Welcome back my lord..."
    - delay: '00:00:02'
    - service: script.turn_on
      data_template: 
        entity_id: >-
          {% if (states('sensor.home_thermostat_heater_temperature') | float > 80) and (states('sensor.dark_sky_apparent_temperature') | float < 75) %}
           script.open_blinds_for_windows
          {% else %}
           script.dont_open_blinds_for_windows
          {% endif %}

Script

  dont_open_blinds_for_windows:
    sequence:
      - service: media_player.alexa_tts
        data_template:
          entity_id: media_player.blue_echo
          message: "Hi,, the temperature inside the house is {{ states('sensor.home_thermostat_heater_temperature') }} degrees,,,, and the current outside temperature is {{states('sensor.dark_sky_apparent_temperature')}} degrees.  Not opening blinds."
          
  open_blinds_for_windows:
    sequence:
      - service: media_player.alexa_tts
        data_template:
          entity_id: media_player.blue_echo
          message: "Hi,, the temperature inside the house is {{ states('sensor.home_thermostat_heater_temperature') }} degrees,,,, and the current outside temperature is {{states('sensor.dark_sky_apparent_temperature')}} degrees.  Please use your phone to let me know if I should open the blinds."
      - service: notify.ios_iphone_ios
        data:
          title: "It's HOT inside and cooler outside!"
          message: "Swipe LEFT -> VIEW for ACTIONS! House = {{ states('sensor.home_thermostat_heater_temperature') }} , Outside =  {{states('sensor.dark_sky_apparent_temperature')}}"
          data:
           push:
             category: "blinds"