Severals automations in one blueprints

Sometimes, you need create severals automations for one think, so, could be very useful add suport for this in blueprint.

Example:


blueprint:
  name: Example
  description: Example
  domain: automation
  input:
    motion1:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    motion2:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion


automations:
 -  trigger:
     platform: state
     entity_id: !input motion1
        from: "off"
        to: "on"
    action:
      - service: tts.google_say
        data:
        entity_id: media_player.home_mini
        message: 'Motion 1 detected'
   mode: single
   max_exceeded: silent

 -  trigger:
     platform: state
     entity_id: !input motion2
        from: "off"
        to: "on"
    action:
      - service: tts.google_say
        data:
        entity_id: media_player.home_mini
        message: 'Motion 2 detected'
   mode: single
   max_exceeded: silent

Maybe, you can create a one complex automation instead two automations, buts… Could be useful.

That’s exactly what you should be doing, put everything that belongs together in the same automation.

1 Like

I will say this again as I’ve said in other threads that this has been discussed…

I think you are completely missing the point of what a blueprint does.

A blueprint gives you a reusable framework so that you can create an automation based on that framework by substituting the desired entities for the variables (or “placeholders”) created in the blueprint.

the example that you gave makes no sense in that respect.

Both of those automations are exactly the same except for the placeholders.

THe way this is supposed to work is that you create the blueprint (from your example):

blueprint:
  name: Example
  description: Example
  domain: automation
  input:
    motion:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion

automation:
  trigger:
   platform: state
   entity_id: !input motion
      from: "off"
      to: "on"
  action:
    - service: tts.google_say
      data:
      entity_id: media_player.home_mini
      message: 'Motion 1 detected'
 mode: single
 max_exceeded: silent

then YOU create two automations from the same blueprint (example taken from the docs):

automation example 1:
  use_blueprint:
    path: example.yaml
    input:
      motion: binary_sensor.kitchen

automation example 2:
  use_blueprint:
    path: example.yaml
    input:
      motion: binary_sensor.den

I am assuming this is the syntax for creating multiple automations from the same blueprint since the docs for actually using blueprints thru yaml are woefully lacking. But at least it gives you the idea.

THere is no reason to combine the two automations in your example into one blueprint since the two automations (or 100, or 1000 other identical automations to the two I posted) can be created from the same blueprint base code just by changing the “motion” placeholder.

5 Likes

Cannot like the post above this one enough. Really worries me about the state of some people’s configuration that this even needs to be explained once, let alone the several times this week in various guises.

2 Likes

I have just posted an alternative view of this. I agree this is a poor example, but there is a need.
Compex Automations rarely operate on their own. Ther are usually other supporting entities: Helpers, Scripts and other Automations. A Blueprint needs to consider the entirety of what is being acheived.

4 Likes

I do think having multiple automations could have an added value. Example sun up/down as light trigger. Having it in one automation makes it more complex and for other examples could be much more difficult.

blueprint:
  name: wake light off sunup
  description: Light off when sun down
  domain: automation  
  input:
    light_entity:
      name: Light 
      selector:
        entity:
          domain: light

automations:
 - trigger:
     platform: state
     entity_id: sun.sun
     from: 'below_horizon' 
     to: 'above_horizon'

   variables:
      light_entity: !input light_entity

   action:
     service: light.turn_off
     data:
       entity_id: light_entity

 - trigger:
     platform: state
     entity_id: sun.sun
     from: 'above_horizon'
     to: 'below_horizon' 

   variables:
      light_entity: !input light_entity

   action:
     service: light.turn_on
     data:
       entity_id: light_entity

This should be a single automation either using templating in the service section, or using choose.

Really?? This is absolutely the most basic example of a single automation that should never have been split into two in the first place :sob:

1 Like

You mean a package ?

How I can create one automation to turn on a light using time helper and turn off at by other time helper?

If you could share your example in what is considered the prefered way for this example and why this one is no good

i currently use a package for it but it has more functionality currently no sure how to implement it via blueprints. (the light only turns off when it was turned on by the sundown event.)

As we can do everything using templates in trigger/conditions/service section why keeping the other other platforms. For my current try’s with blueprints i’m using the choose option.

I hope you didn’t mean the example you posted is “complex”, because it’s not.

In fact, it’s so simple that I can’t understand why anyone would want to split it into two automations.

This automation i’m currently using, it has more lines of code , and needs to execute more stuff during up/down events = my interpretation is more complex
But maybe it can be shorter ?

blueprint:
  name: wake light 
  description: Set light when sun down/sun up
  domain: automation  
  input:

    light_entity:
      name: light 
      selector:
        entity:
          domain: light

trigger:

  - platform: state
    entity_id: sun.sun
    from: 'above_horizon'
    to: 'below_horizon' 

  - platform: state
    entity_id: sun.sun
    from: 'below_horizon' 
    to: 'above_horizon'

variables:

  light_entity: !input light_entity

action:

  - choose:
    - conditions:
        - condition: state
          entity_id: sun.sun
          state: 'above_horizon'
      sequence:
        - service: light.turn_off
          data:
            entity_id: light_entity
    - conditions:
        - condition: state
          entity_id: sun.sun
          state: 'below_horizon'
      sequence:
        - service: light.turn_on
          data:
            entity_id: light_entity

Sure, but it’s getting a bit ridiculous having to keep ‘fixing’ these just to illustrate a point…

blueprint:
  name: wake light off sunup
  description: Light off when sun down
  domain: automation  
  input:
    light_entity:
      name: Light 
      selector:
        entity:
          domain: light

trigger:
  platform: state
  entity_id: sun.sun
action:
  service: "light.turn_{{ 'on' if trigger.to_state.state 'below_horizon' else 'off' }}" 
  entity_id: !input light_entity

Where to start?

  • yours uses variables, but there is absolutely no reason and therefore is added complication with zero benefit

  • yours is 39 lines of code, mine is 16.

  • Aside from the obvious efficiency savings, there are fewer moving parts, therefore fewer things to go wrong

  • In the unlikely event that there is a mistake, 16 lines is far easier to go through than 39.

  • mine is neater, and therefore far easier to discern what it does.

  • splitting this into two serves no purpose other than creating more code and more room for error.

  • [insert dozens more reasons here]

2 Likes

^^^^
This.

Many blueprint automations that I have seen share the same deficiency: bloat. They contain so many unnecessary lines of code it’s like they were being paid by the line.

2 Likes

thanks for your code and explaination and time spend on it.
I get your point and i agree with you in most cases shorter is better.

As for the splitting part, not everyone understand templates

Not everyone should be trying to write blueprints :wink:

1 Like

my mother told me never to give up :smiley: