Automation with Condition but always Triggering

Hi Everyone, been trying to make a “simple” automation with a “simple” condition to work yet still failing.

I simply want to turn on the AC at sunrise (before I leave for work) only when I’m home. What happens is that it always trigger even if I’m not at home.

Here is my code in automations.yaml

- id: '1559728948711'
  alias: Sunrise
  trigger:
  - event: sunrise
    platform: sun
  condition:
  - condition: state
    entity_id: person.ian
    state: home
  action:
  - entity_id: switch.ac
    service: homeassistant.turn_on

A little help. Thanks in advanced

1 Like

I think you dont have a platform specified in the condition.

The docs mention enclosing your entity id and state in parentheses. But it seems like a correct automation to me. Did you check the state of person.ian and is it indeed home at sunrise?

Yes, person.ian entity is indeed ‘not_home’.

I am at a loss just like you. The automation seems fine. Is something else triggering your AC? Try the parentheses just in case.

I added ’ ’ on both entity_id and state. No luck, still the same.

I tried it to other entities both on the condition and action statements, still no luck

How do you check the automation? Since there is only 1 sunrise each day :slight_smile:

Manually triggering it.

Does manually triggering automations ignore conditions?

Yes :slight_smile:

Oh, i see. That was embarassing. Thank you so much! :slight_smile:

You’re not the first person to learn that manually trigger an automation (from the Services page) will skip the trigger and the condition. In addition, if your action refers to the trigger object, there will be no trigger object available.

For example, the following action will fail if the automation is manually triggered because the trigger object will be undefined.

  action:
    - service: system_log.write
      data_template:
        message: >-
          {{trigger.to_state.name}} door {{ 'opened' if trigger.to_state.state == 'on' else 'closed' }}

I have a question for you, if this information were available in the documentation, where do you feel it should be entered? In the Automation page or somewhere else? Where do you think most people would find it and read it?

You didn’t ask for my opinion but that’s never stopped me before…:wink:

Since that information could be important to the conditions and the actions then if it were me I would put the same note in all three sections - “automations basics”, “conditions” and “actions”.

2 Likes

I think putting it here, here, and here. When I was searching the docs, It pointed me to the last two links, Automations and Conditions. ’ Automating Home Assistant’ page didnt came to me from the search results.

I just started using Home Assistant and this tripped me up for a while when I was reading about conditions and trying to test them by manually triggering the automation. I was calling the service through the service page and from Alexa and couldn’t figure out why my condition was bypassed. My closet light should turn on at the end of the Alexa “Good Morning” routine, but only on weekdays. I’m exposing the automation to Alexa.

I think it would be useful to include something like the following text in the Conditions section or the Automation page. This could serve as a second example on the automations page.

Note: Manually triggering an automation (from the service page or an external call) bypasses conditions in the main body of the automation and does not allow access to the trigger object. To evaluate conditions during a manual trigger, include them in the action.

Example of condition in action:

  action:
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - device_id: 3d7669
    domain: switch
    entity_id: switch.closet_light
    type: turn_on

One thing I’m trying to understand still is why you’d ever have the conditions outside the action section. Any pointers?

Good question! I think it is just the standard design pattern: trigger-condition-action. A difference is, when you manually execute an automation, it skips right to the action part. For testing purposes, this is nice. Obviously when you put your conditions inside the action, they won’t be skipped. So it just depends on what you want.

Also, did you know you can easily edit the documentation pages yourself? Just need a Github account. Click Edit this page on Github, click the pencil icon, edit and don’t forget the last step: click create a pull request. Don’t let your input go to waste, that is a great addition.

Thanks @Emphyrio! I made the change and submitted the PR. Along the way I realized that for my purposes I wanted a script so I could trigger it from an Alexa routine, and not an automation.

At least I learned something about automations and hopefully it’s helpful to others.

If it’s outside action, action won’t be executed. If it’s inside, the action will be executed (i.e automation triggered). Doesn’t it make sense?