Automation - best approach?

Hello community,

I have two automations as follows. I wanted your opinion on the best way to merge it to one automation only.

This automation turn on my Ambient lights 10 minutes before sunset and 30 minutes before sunrise.

- id: '1603111948383'
  alias: Ambient On
  description: ''
  trigger:
  - platform: sun
    event: sunset
    offset: -00:10
  - platform: sun
    event: sunrise
    offset: -00:30:00
  condition: []
  action:
  - scene: scene.ambients_on
  - type: turn_on
    device_id: f0a2f76a4e5fafeda2fabb7c2e85f8cf
    entity_id: switch.tasmota_2
    domain: switch
  mode: single

This automation turns off the ambient light at 22H and as the sun is above horizon.

- id: '1605699881686'
  alias: Ambient Off
  description: ''
  trigger:
  - platform: time
    at: '22:00'
  - platform: state
    entity_id: sun.sun
    from: below_horizon
    to: above_horizon
  condition: []
  action:
  - scene: scene.ambients_off
  - type: turn_off
    device_id: f0a2f76a4e5fafeda2fabb7c2e85f8cf
    entity_id: switch.tasmota_2
    domain: switch
  mode: single

Honestly, I would not combine them into one automation.

There is no penalty for having two automations rather than one and it is far easier to understand (and debug if something goes awry) the way you have it.

3 Likes

Absolutely. I personally would consider more than two.

How about an input_boolean flag with small automations to turn it on 10 minutes before sunset and off 30 minutes before sunrise? Your ambient on and off automations would be triggered by the flag’s change of state.

Less repetition, easy to understand, debug and update, plus other automations can use the same flag if you want to sync lights etc. coming on at dusk. Also the input_boolean can be used in dashboards for manual operation or testing.

1 Like
- id: 'ambient_controller'
  alias: Ambient Controller
  description: ''
  trigger:
  - platform: sun
    event: sunset
    offset: -00:10
  - platform: sun
    event: sunrise
    offset: -00:30:00
  - platform: time
    at: '22:00'
  - platform: state
    entity_id: sun.sun
    from: below_horizon
    to: above_horizon
  condition: []
  action:
  - variables:
      command: "{{ 'on' if trigger.platform == 'sun' else 'off' }}"
  - scene: "scene.ambients_{{ command }}"
  - service: "switch.turn_{{ command }}"
    target:
      entity_id: switch.tasmota_2
  mode: single

EDIT

The scene portion of this automation is incorrect. See corrected version in another post below.

1 Like

Could you explain that part please? I am not familiar with that. Is it templating?

Yes. Fairly self-explanatory to read, but requires accuracy to write.

Yes, it employs templates.

This part defines a variable named command. It sets the value of command to on if the trigger responsible for triggering the automation was the Sun Trigger otherwise it sets it to off.

 - variables:
      command: "{{ 'on' if trigger.platform == 'sun' else 'off' }}"

All types of triggers produce what is known as a Trigger Variable. The information contained by the trigger variable varies depending on the type of trigger. However, they do have one in common and it’s trigger.platform which tells you the trigger’s type (Sun, Time, State, Numeric State, Event, etc).

This next part sets the scene to either scene.ambients_on or scene.ambients_off depending on the value that was assigned to the variable command. EDIT See corrected version in a subsequent post.
- scene: “scene.ambients_{{ command }}”

The same thing happens in this final part. Depending on the value of command it will be either switch.turn_on or switch.turn_off.

  - service: "switch.turn_{{ command }}"
    target:
      entity_id: switch.tasmota_2
1 Like

That is interesting.

HA giving me the following error though:

Invalid config for [automation]: Entity ID scene.ambients_{{ command }} is an invalid entity ID for dictionary value @ data['action'][1]['scene']. Got None. (See ?, line ?).

In VS it shows this error:

That’s my mistake. I had followed what you originally wrote but it was a Device Action and I didn’t implement it correctly. It should work properly as a Service Call (scene.turn_on).

- id: 'ambient_controller'
  alias: Ambient Controller
  description: ''
  trigger:
  - platform: sun
    event: sunset
    offset: -00:10
  - platform: sun
    event: sunrise
    offset: -00:30:00
  - platform: time
    at: '22:00'
  - platform: state
    entity_id: sun.sun
    from: below_horizon
    to: above_horizon
  condition: []
  action:
  - variables:
      command: "{{ 'on' if trigger.platform == 'sun' else 'off' }}"
  - service: scene.turn_on
    target:
      entity_id: "scene.ambients_{{ command }}"
  - service: "switch.turn_{{ command }}"
    target:
      entity_id: switch.tasmota_2
  mode: single