Putting multiple automations in one

Hi there,

I’m wondering if you guys can help me out. Over time I’ve got many automations and I think with some variables I can work them out together.

For example

  • I got four automations for when I am at home. Fase 1 - 2 - 3 and 4 for lightning. Every step the lightning would get a little bit brighter and some extra lights would turn on. I’ve got them running in one script now and one automation.

Automation checks if someone is at home
and if the sun is getting below the horizon: if yes; play the script.

Script.
Play fase one, wait ~ 1 hour,
play fase two, wait ~ 1 hour
etc

The problem is / was that this one would run when I am at home. This would trigger the right moment for the lights to turn on. But one I gét home (for example two hours after sunset) this isn’t working. If the script would trigger at that moment instead of @ six PM, it won’t make sense.

So I also got four automations for getting home @ 6PM, 7PM, etc etc

Is it possible to get this into one script or one automation as well?

These are the automations I’ve got at this moment

- id: '1514285419023'
  alias: Thuiskomen na zonsondergang (fase 1)
  trigger:
  - entity_id: binary_sensor.iemand_in_huis
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - after: sunset
    after_offset: -01:15:00
    condition: sun
  action:
  - data:
      brightness: 47
      entity_id:
      - light.lamp_booglamp
      - light.lamp_tv_meubel
      rgb_color:
      - 255
      - 169
      - 84
    service: homeassistant.turn_on
- id: '1514207028212'
  alias: Thuiskomen na zonsondergang (fase 2)
  trigger:
  - entity_id: binary_sensor.iemand_in_huis
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - after: sunset
    after_offset: -00:45:00
    condition: sun
  action:
  - data:
      brightness: 65
      entity_id:
      - light.lamp_booglamp
      - light.lamp_tv_meubel
      rgb_color:
      - 255
      - 169
      - 84
    service: homeassistant.turn_on
- id: '1543167100630'
  alias: Thuiskomen na zonsondergang (fase 3)
  trigger:
  - entity_id: binary_sensor.iemand_in_huis
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - after: sunset
    after_offset: -00:20:00
    condition: sun
  action:
  - data:
      brightness: 85
      entity_id:
      - light.lamp_booglamp
      - light.lamp_tv_meubel
      - light.lamp_hanglamp
      rgb_color:
      - 255
      - 169
      - 84
    service: homeassistant.turn_on
  - data:
      entity_id: script.keuken_sfeerverlichting
    service: script.turn_on
- id: '1514209171022'
  alias: Thuiskomen na zonsondergang (fase 4)
  trigger:
  - entity_id: binary_sensor.iemand_in_huis
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - after: sunset
    condition: sun
  - before: '23:59:59'
    condition: time
  action:
  - data:
      brightness: 85
      entity_id: light.hanglamp_muur
      rgb_color:
      - 255
      - 169
      - 84
    service: homeassistant.turn_on
  - data:
      brightness: 116
      entity_id:
      - light.lamp_booglamp
      - light.lamp_tv_meubel
      - light.lamp_hanglamp
      rgb_color:
      - 255
      - 169
      - 84
    service: homeassistant.turn_on
  - data:
      entity_id: script.keuken_sfeerverlichting
    service: script.turn_on

I think your logic is alittle off here. All 4 of these automations are identical instead of the condition. And your first automation literally covers all the rest.

This condition:

  - after: sunset
    after_offset: -01:15:00
    condition: sun

Causes the automation to fire any time between sunset-1:15 and midnight. The other scripts have a tighter time span, but do the same thing and your first automation spans all the others. So there is no reason to have the others.

There are a few differences.

## starting with two lights
 - after: sunset
    after_offset: -01:15:00
    condition: sun
  action:
  - data:
      brightness: 47
## more brightness  
- after: sunset
    after_offset: -00:45:00
    condition: sun
  action:
  - data:
      brightness: 65
      entity_id:
      - light.lamp_booglamp
      - light.lamp_tv_meubel
## one light extra and more brightness      
- after: sunset
    after_offset: -00:20:00
    condition: sun
  action:
  - data:
      brightness: 85
      entity_id:
      - light.lamp_booglamp
      - light.lamp_tv_meubel
      - light.lamp_hanglamp

And the last condition is

 - after: sunset
    condition: sun
  - before: '23:59:59'
    condition: time

This one makes sure that (around the house) there won’t be to much lighting to wake people up :slight_smile:

I didn’t notice those minor changes. But my point still stands. Your logic is flawed because you’ll be firing all 4 automations, in a seemly random order.

This means that ALL 4 automations will fire between sunset and midnight.

When you omit an ending time period from a sunset condition, it assumes you want to go until midnight. Documented here.

First of all thanks for your reply / replies =)!

I think we both see or mean something different…It’s true that I’ve got four automations, but they don’t trigger at the same time. If I’m at home - lets say @ 16:20 the first automation will triger. After thet, the script that I’m at home will run and trigger the rest of the automations, turning more lights on and set them brighter.

However what I want and I’m just wondering if it possible (because this works just fine) is if the code can be any better and cleaner

Trigger

  • Is sensor.someone_home from off to on
  • is condition sunset down
    – minus offset A == turn on light A into brighness 40
    – minus offset between A & B == turn on light A into brightness 60
    – minus offset between B & C == turn light into brighness 80 + turn on light B
    – minus offset after C and before midnight == turn light into brightness 100 + turn on light B + C

Can this be done in one automation or do I need the four I’ve got?
If so; how do I do this?

If I understood your requirements correctly, attempting to combine all four automations into one will produce something like this:

- alias: 
  trigger:
  - platform: state
    entity_id: binary_sensor.iemand_in_huis
    from: 'off'
    to: 'on'
  action:
  - service: homeassistant.turn_on
    data_template:
      brightness: " TEMPLATE GOES HERE "
      entity_id: " TEMPLATE GOES HERE "
      rgb_color: [255,169,84]
  - service: homeassistant.turn_on
    data_template:
      brightness: " TEMPLATE GOES HERE "
      entity_id: " TEMPLATE GOES HERE "
      rgb_color: [255,169,84]
  - service: script.turn_on
    data_template:
      entity_id: " TEMPLATE GOES HERE "

The automation requires 5 nearly identical templates. The template has to determine the current time and, depending on the current time’s relation to sunset, produce the desired result (brightness value or list of entities). The last template determines if it should run a script (keuken_sfeerverlichting) or a ‘dummy script’ (that does nothing).

The template must perform time calculations that will be considerably messier than the simple way they’re done in a condition. Then you must use this time-calculating template 5 times in this automation. Is this worth the effort? I’d say no but it’s your decision.

I get that, but your trigger is you arriving home

  - entity_id: binary_sensor.iemand_in_huis
    from: 'off'
    platform: state
    to: 'on'

Unless this binary_sensor is continuously toggling on and off, it won’t work the way you expect.

You keep saying

but then your triggers are all:

  - entity_id: binary_sensor.iemand_in_huis
    from: 'off'
    platform: state
    to: 'on'

So, yes, they do all trigger at the same time… I think you have a fundamental miss understanding on how things work.

So for example: If you arrive between sunset and midnight… ALL 4 will trigger. ALL 4. They will trigger in a random order as well.