Automation with condition

Hi
I’m trying to make an automation that sets the temperature to 21 degrees and turns on the lights.
BUT I wanted to add the condition ‘if sun has set’.
if I comment the ‘condition’ part out, it works. with, it doesn’t. (I would have supposed the ‘conditional’ part needs to be ‘indented’ but as it seems, if the condition fails the execution just stops?
What am I doing wrong?

  - alias: Patrick gets home
    trigger:
      platform: state
      entity_id: device_tracker.74d9cfd344074c4895ec693991a27d21
      from: 'not_home'
      to: 'home'
    action:
      - service: climate.set_temperature
        data:
          entity_id: climate.thermostat
          temperature: 21
      - condition:
          condition: sun
          after: sunset
      - service: homeassistant.turn_on
        entity_id:
          - light.wohnzimmer

Try:

- alias: Patrick gets home
  trigger:
    platform: state
    entity_id: device_tracker.74d9cfd344074c4895ec693991a27d21
    from: 'not_home'
    to: 'home'
  condition:   
    condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
    - service: climate.set_temperature
      data:
        entity_id: climate.thermostat
        temperature: 21
    - service: homeassistant.turn_on
      entity_id:
        - light.wohnzimmer
1 Like

Hi Mike

Thank you for your suggestion. But, if I’m not totally wrong, that does NOT do what I wanted.
I want to turn up the heating, regardless of the state of the sun, BUT only switch on the lights when after sunset. Would it be better to just make two independant automations? Is that possible without problems?

Please take a look at examples page - you don’t need condition condition. So just go like this

  temperature: 21
  - condition: sun
    after: sunset
  - service: homeassistant.turn_on
    entity_id:
      - light.wohnzimmer

Hm, I copied that from an examples page :slight_smile: (see: Conditions - Home Assistant ‘sun condition’) But I was pretty sure I even tried it without condition: condition because I found it redundant, too. But as it seems I made another mistake then.
Thank you!

Ahh yes I see now. Still learning all of this myself but trying to give back to the community, sometimes it backfires. :slight_smile:

Well that’s for condition section. For actions section should be this url https://home-assistant.io/getting-started/automation-action/

But the example there is over complicated

Oh, I’ve looked there, as well. Did look quite similar to the condition: condition: sun example, doesn’t it?
To be honest I don’t like the syntax of the config file of homeassisant at all. It’s not very intuitive - and Indention is fine, but it shouldn’t be part of the logic. But well, another problem solved, thank’s again.

Nevermind, I appreciate the good intention :wink:

action:
  - service: climate.set_temperature
    data:
      entity_id: climate.thermostat
      temperature: 21
  - condition: sun
    after: sunset
  - service: light.turn_on
    entity_id:
      - light.wohnzimmer

That should work the way you want it, because a condition not met stops the script execution in the action part.

yes, that’s how I tried it yesterday. no syntax error, BUT the condition was false, even in the middle of the night!

No I changed it to (as also mentioned in this thread)

    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'  
    - service: homeassistant.turn_on
      entity_id:
        light.wohnzimmer

and now it seems to work correctly.

I think the hair that’s being split is that after:sunset is an event (not a state). Something that works as a trigger (with event, not after) and only tests true for that moment. If you go to your states dev tool, you’ll see that sun.sun has states of “below_horizon” and “above_horizon”.

That must be it. But I was supposing ‘at sunset’ would be an event, while ‘after sunset’ is a state :wink:

“After” is the way that time-based triggers are expressed. I’m not sure why “after” was chosen over “at”, but that’s consistent with other time-based triggers.