Multiple Actions, but skip some based on conditions. Possible?

I have a goodbye automation that does several things, normal stuff, lights off, doors locked, garage closed, etc. One issue I ran into a while ago, was if I left at night, it would also turn off the outdoor lights. Makes sense, since I used a action to just turn off all lights. My solution ended up being creating two groups for my lights, one called Daytime lights, and one called nighttime lights.

It works fine, but I’ve been trying to streamline and reduce my automations when possible.

So, now I have two 99% identical lengthy automations with the only two differences. A condition for sunset, and a home.assistant.Turn.off for the appropiate group.

Lots of details to ask: Can I have a action that only runs after sunset, and another action that only runs before sunset, along with all my other actions, in the same automation?

You can do conditions inside your action list. That’s how I typically solve that kind of stuff:

- alias: foo
  trigger:
    - platform: state
      entity_id: sensor.door
      to: 'open'
  action:
    - service: notify.notify
      data:
        message: "the door is open"
      # when this condition is not met, nothing after it will execute
    - condition:
        condition: or  # 'when dark' condition: either after sunset or before sunrise
        conditions:
          - condition: sun
            after: sunset
          - condition: sun
            before: sunrise
    - service: light.turn_on
      entity_id: light.door_light
3 Likes

does the condition sunset/rise work well? see State sun.sun vs event sunrise/sunset please where I try to figure out why that doesnt work for me, and the sun.sun.state has to be used…
wonder why?

So with this

  - service: notify.pushbullet_notifications
    data_template:
      message: "All lights have been turned off, except for night time lighting.  Garage door has closed and locks have been locked"
      target:
      - !secret pauls_secret_email
      title: "Goodbye Routine has ran"
  - condition: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    above: 2.0  
  - service: homeassistant.turn_off
    entity_id:
      - group.outdoor 

the outdoor lights group will only be turned off when the sun is above 2.0 degrees above horizon, right? But everything above will run as normal. Thanks for this info. Easy to understand!

1 Like

Yes, that is correct. Any condition can be used in the action section.

1 Like
action:
    - condition: template
      value_template: "{{ states('cover.sunblind_front_97') == 'closed' and states('cover.sunblind_back_188') == 'closed'}}"
    - service: cover.open_cover
      entity_id: cover.sunblind_front_97,cover.sunblind_back_188
    - service: notify.oneplus
      data_template: 
        message: "Sunblinds Rolled Up."
        
    - condition: template
      value_template: "{{ states('cover.sunblind_back_188') == 'closed'}}"
    - service: cover.open_cover
      entity_id: cover.sunblind_back_188
    - service: notify.oneplus
      data_template: 
        message: "Sunblind BACK Rolled Up."
  
    - condition: template
      value_template: "{{ states('cover.sunblind_back_188') == 'closed'}}"
    - service: cover.open_cover
      entity_id: cover.sunblind_back_188
    - service: notify.oneplus
      data_template: 
        message: "Sunblind BACK Rolled Up."

Each individual action works on its own. However, putting them in sequence delivers no result. I assume that ones the first condition is not being met, the process halts. Hence, the thought to make the conditions OR, i.e. with embedded

    - condition:
        condition: or 
        conditions:
           - condition: template
             value_template: "{{ states('cover.sunblind_front_97') == 'closed' and 
etc.

However, this fails thus far. Any suggestion?

syntax is wrong, drop the initial condition.

- condition: or
  conditions:
  - condition: template
    value_template: {{ }}
  - condition: etc...

also if you’re using a template, just use or inside the template

Thanks for your quick response. Based on your suggestion, I ended up with:

  action:
    - condition: or
      conditions:
      - condition: template
        value_template: "{{ states('cover.sunblind_front_97') == 'closed' and states('cover.sunblind_back_188') == 'closed'}}"
      - service: cover.open_cover
        entity_id: cover.sunblind_front_97,cover.sunblind_back_188
      - service: notify.oneplus
        data_template: 
          message: "Sunblinds Rolled Up."
        
      - condition: template
        value_template: "{{ states('cover.sunblind_back_188') == 'closed'}}"
      - service: cover.open_cover
        entity_id: cover.sunblind_back_188
      - service: notify.oneplus
        data_template: 
          message: "Sunblind BACK Rolled Up."
  
      - condition: template
        value_template: "{{ states('cover.sunblind_back_188') == 'closed'}}"
      - service: cover.open_cover
        entity_id: cover.sunblind_back_188
      - service: notify.oneplus
        data_template: 
          message: "Sunblind BACK Rolled Up."

This however, gives the following error message:
Invalid config for [automation]: Unexpected value for condition: 'None'. Expected numeric_state, state, sun, template, time, zone, and, or, not, device @ data['action'][0]['conditions'][1]. Got None Unexpected value for condition: 'None'. Expected numeric_state, state, sun, template, time, zone, and, or, not, device @ data['action'][0]['conditions'][2]. Got None

Is this because I have embedded the - service: ?

I don’t believe you can add your services to the conditions as you’ve done. The action should be after the condition. Also unless I’m missing something, aren’t your last two conditions exactly the same?

What is the ultimate goal? I believe your goal is to get two notifications. One to let you know that any blind was rolled up, and then a second notification to let you know which one. Front or back. In your automation is the blind(s) also the trigger?

Maybe this will work for you

- alias: 'Sunblinds Rolled Up Notification'
  trigger:
  - platform: state
    entity_id:
      - cover.sunblind_front_97
      - cover.sunblind_back_188
    to: 'closed'
  action:
  - service: notify.oneplus
    data_template:
      message: "The {{ trigger.to_state.attributes.friendly_name }} Rolled Up"

You are correct, the last condition should reflect the front blind instead. Your proposal I had working before. I just started out with a more fancy messaging scenario in which case the message would be subject to whether both or an individual blind had to be rolled up. It was also aimed at giving me a bit more experience as to what was possible under ACTION. Unfortunately, not that much I’m afraid. For this purpose I used an almost 1-1 translation of my VERA - LUA code currently supporting the blind control. At least, thanks for thinking along with me.

If you wanted to get a message when both rolled up at the same time, you’d have to get a bit creative (I think so anyway). Since from an HA perspective, they will both never trigger at the exact same time. Couple of ideas. You could create a binary sensor that would only be On (or off) when both covers are closed. You could then add a delay to the other sensors as well. Maybe the condition could be that the binary sensor is on(off).

I tried to be a bit more creative. I still think this wont quite work, probably will go off twice if both blinds close together, but it’s closer

- alias: 'Sunblinds Rolled Up Notification'
  trigger:
  - platform: state
    entity_id:
      - cover.sunblind_front_97
      - cover.sunblind_back_188
    to: 'closed'
  action:
  - delay:
      seconds: 5  #This give the time for the blinds to finish closing
  - service: notify.oneplus
    data_template:
      message:
        {% if states('cover.sunblind_front_97') == 'closed' and states('cover.sunblind_back_188') != 'closed'  %}
          Front Blinds Closed
        {% elif states('cover.sunblind_front_97') != 'closed' and states('cover.sunblind_back_188') == 'closed'  %}
          Back Blinds Closed
        {% states('cover.sunblind_front_97') == 'closed' and states('cover.sunblind_back_188') == 'closed'  %}
          Both Blinds Closed
        {% else %}
          hmm, somthing is wrong here
        {% endif %}

Thanks for thinking along with me. However, with the necessary clean-up, the above script does not deliver the aimed end result. I’m currently moving away from this concept and will investigate whether the use of a variable “message” passed on to a script will do the trick. Again, thanks for yr thoughts thus far.