Help with Automations - Consolidation

It’s possible, but it doesn’t gain you much:

- alias: Disable Sonos Alarm
  trigger:
  - platform: state
    entity_id: input_boolean.alarm_clock_six
    to: 'off'
    id: six_am
  - platform: state
    entity_id: input_boolean.alarm_clock_eight
    to: 'off'
    id: eight_am
  action:
  - choose:
      # At 6 AM
      - conditions: "{{ trigger.id == 'six_am' }}"
        sequence:
          - service: sonos.update_alarm
            data:
              entity_id: media_player.bedroom_sonos
              alarm_id: 1129
              enabled: false
    # At 8 AM
    default:
      - service: sonos.update_alarm
        data:
          entity_id: media_player.bedroom_sonos
          alarm_id: 1233
          enabled: false

You should also go through your config and do a search and replace for all instances of data_template and replace with just data and for service_template replace with just service.

That is exactly what i was searching for. Big thanks!

Ok this is what im looking for but sadly its not working for me.

i think this has to do with the actions. can you help me with that?

What is not working?

What errors do you get?

How did you test it?

The automation is not working. So the alarm for 6am and 8am are not disabled.

i dont get specific errors but the alarms stay enabled.

i tested this by disabling my previous automations and enabled yours but after changing the state of input_boolean.alarm_clock_eight oder input_boolean.alarm_clock_six nothing happens

Try this instead:

- alias: Disable Sonos Alarm
  trigger:
  - platform: state
    entity_id: input_boolean.alarm_clock_six
    to: 'off'
    id: six_am
  - platform: state
    entity_id: input_boolean.alarm_clock_eight
    to: 'off'
    id: eight_am
  action:
  - service: sonos.update_alarm
    data:
      entity_id: media_player.bedroom_sonos
      alarm_id:  >
        {% if trigger.id == "six_am" %}
          1129
        {% else %}
          1233
        {% endif %}
      enabled: false

Im so sorry. Both versions are working i had a typo in my config. after changing this it works perfect.
Thank you so much for the fast help!

1 Like

Yet one more way to do the same thing:

- alias: Disable Sonos Alarm
  trigger:
  - platform: state
    entity_id:
    - input_boolean.alarm_clock_six
    - input_boolean.alarm_clock_eight
    to: 'off'
  action:
  - service: sonos.update_alarm
    target:
      entity_id: media_player.bedroom_sonos
    data:
      alarm_id: "{{ 1129 if 'six' in trigger.to_state.object_id else 1233 }}"
      enabled: false
1 Like

Ok one more thing im struggeling with. This is a bit different from my first question because conditions are different.
also i think trigger should be swapped the condition (input_boolean). This was one of my first automations so i don’t know what happened there :smiley:

So when i togglethe switch the lights should slowly fade in 15 minutes before the time the alarm should go off. So platform time is 7:45 for the 8:00 alarm.
It always starts the same script but based on a different sonos alarm which starts the lights 15 minutes before the alarm.

- alias: Fade in Lights in Bedroom 8:00
  trigger:
  - platform: time
    at: 07:45:00
  condition:
  - condition: state
    entity_id: input_boolean.alarm_clock_eight
    state: 'on'
  - condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
  action:
    service: script.bedroom_light_fade_in
  id: db3028c01f1a41fa8b8238508649757d

and

- alias: Fade in Lights in Bedroom 6:00
  trigger:
  - platform: time
    at: 05:45:00
  condition:
  - condition: state
    entity_id: input_boolean.alarm_clock_six
    state: 'on'
  - condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
  action:
    service: script.bedroom_light_fade_in
  id: 1cf9c5ed596c4eb18b3fd8edcb624ed7

i tried to do it like this:

  condition:
  - condition: state
    state: 'on'
    entity_id:  >
      {% if trigger.id == "six" %}
        input_boolean.alarm_clock_six
      {% else %}  
        input_boolean.alarm_clock_eight
      {% endif %}  

but i think its not working in conditions.
Any ideas?

Have you defined any trigger ids?

Show the whole automation.

Also the state condition can not use templates.

- id: '1629721388503'
  alias: Fade in Lights Bedroom
  trigger:
  - platform: time
    at: 05:45:00
    id: six
  - platform: time
    at: 14:30:00
    id: test   
  condition:
  - condition: state
    state: 'on'
    entity_id:  >
      {% if trigger.id == "six" %}
        input_boolean.alarm_clock_six
      {% else %}  
        input_boolean.alarm_clock_eight
      {% endif %}  
  - condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri    
  action:
    service: script.bedroom_light_fade_in_zehn
  mode: single

So i should switch the automation to something with as a trigger with input_boolean.alarm_clock_six and set the time as a condition?

This is not valid. You cant use templates there.

  condition:
  - condition: state
    state: 'on'
    entity_id:  >
      {% if trigger.id == "six" %}
        input_boolean.alarm_clock_six
      {% else %}  
        input_boolean.alarm_clock_eight
      {% endif %}  

You can only use templates under data:, service:, or target: and in some rare configuration options specifically defined in the documentation.

You can do it like this:

  condition:
    - condition: or
      conditions:
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarm_clock_six
              state: 'on'
            - condition: trigger
              id: "six"
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarm_clock_eight
              state: 'on'
            - condition: trigger
              id: "test"

Or considerably more compact:

  condition:
    - condition: or
      conditions:
        - "{{ states('input_boolean.alarm_clock_six', 'on') and trigger.id == 'six' }}"
        - "{{ states('input_boolean.alarm_clock_eight', 'on') and trigger.id == 'test' }}"

This uses a specific shorthand for template conditions introduced a few updates ago. Using traditional longhand template conditions it would look like this:

  condition:
    - condition: or
      conditions:
        - condition: template
          value_template: "{{ states('input_boolean.alarm_clock_six', 'on') and trigger.id == 'six' }}"
        - condition: template
          value_template:  "{{ states('input_boolean.alarm_clock_eight', 'on') and trigger.id == 'test' }}"

See: https://www.home-assistant.io/docs/scripts/conditions/#template-condition

Wow. Thats some nice stuff.

How do implement the weekdays condition?

On your first example im pretty sure its just another condition under it.
But how in the compact way?

I will try out the compact style and will tell if it works.

Like so:

  condition:
    - condition: or
      conditions:
        - "{{ states('input_boolean.alarm_clock_six', 'on') and trigger.id == 'six' }}"
        - "{{ states('input_boolean.alarm_clock_eight', 'on') and trigger.id == 'test' }}"
    - condition: time
      weekday:
      - mon
      - tue
      - wed
      - thu
      - fri  

Conditions are AND logic by default, so we have: either of the templates must be true and the weekday condition must be true.

It could also be done like this (using shorthand templates again):

  condition:
    - condition: or
      conditions:
        - "{{ states('input_boolean.alarm_clock_six', 'on') and trigger.id == 'six' }}"
        - "{{ states('input_boolean.alarm_clock_eight', 'on') and trigger.id == 'test' }}"
    - "{{  0 <= now().weekday() <= 4 }}"

So i have this code:

- id: Fade in Lights Bedroom Test
  trigger:
  - platform: time
    at: 05:45:00
    id: six
  - platform: time
    at: 14:30:00
    id: test     
  condition:
    - condition: or
      conditions:
        - "{{ states('input_boolean.alarm_clock_six', 'on') and trigger.id == 'six' }}"
        - "{{ states('input_boolean.alarm_clock_eight', 'on') and trigger.id == 'test' }}"
    - condition: time
      weekday:
      - mon
      - tue
      - wed
      - thu
      - fri                    
  action:
    - service: script.bedroom_light_fade_in
  mode: single

and it gives me this error:

Invalid config for [automation]: Expected HH:MM, HH:MM:SS or Entity ID with domain ‘input_datetime’ or ‘sensor’ @ data[‘at’][0]. Got None. (See /config/configuration.yaml, line 12).

In the first two templates, replace states with is_state.

The third template can be reduced to this because isoweekday reports Monday as 1 and Sunday as 7.

    - "{{ now().isoweekday() <= 5 }}"
1 Like

Geez I’m making a mess tonight. Might be time for bed. Fixed:

  condition:
    - condition: or
      conditions:
        - "{{ is_state('input_boolean.alarm_clock_six', 'on') and trigger.id == 'six' }}"
        - "{{ is_state('input_boolean.alarm_clock_eight', 'on') and trigger.id == 'test' }}"
    - "{{ now().isoweekday() <= 5 }}"
1 Like
- id: Fade in Lights Bedroom Test
  trigger:
  - platform: time
    at: 05:45:00
    id: six
  - platform: time
    at: 14:30:00
    id: test     
  condition:
    - condition: or
      conditions:
        - "{{ is_state('input_boolean.alarm_clock_six', 'on') and trigger.id == 'six' }}"
        - "{{ is_state('input_boolean.alarm_clock_eight', 'on') and trigger.id == 'test' }}"
    - "{{ now().isoweekday() <= 5 }}"                 
  action:
    - service: script.bedroom_light_fade_in
  mode: single

gives me the same error: Invalid config for [automation]: Expected HH:MM, HH:MM:SS or Entity ID with domain ‘input_datetime’ or ‘sensor’ @ data[‘at’][0]. Got None.

Try this

- id: Fade in Lights Bedroom Test
  trigger:
  - platform: time
    at: '05:45:00'
    id: six
  - platform: time
    at: '14:30:00'
    id: test  

This works. But why do i need to out the time in ’ '?
I never did this for a time for any automation. Is this because of multiple triggers?