Efficiency with automation triggers

Hello!

I’ve just started using Home Assistant and bought 3 fibaro switches to get me started.

Everything works but i have a fealing that my automations could be simpler.

Maybe somebody can check out my automations and help me out?

This is my first automation: Turn on the lights 1 hour before sunset(works).


 - alias: "Lights: Turn on at sun set"

          action: 

            entity_id: group.living_room

            service: switch.turn_on

          trigger: 

            event: sunset

            platform: sun

            offset: "-01:00"

This automation turns off the light (works)


    - alias: "Lights: Turn off at midnight"

      action: 

        entity_id: group.living_room

        service: switch.turn_off

      trigger: 

        - platform: time

          at: '22:30'

This automation should turn on the light when (not really efficient i think, but works):

Cloud coverage is above 80%, devices are home, it’s after 09:00 o’clock and when it’s raining.


    - alias: 'Lights: Dark Day'

      trigger:

        - platform: numeric_state

          entity_id: sensor.dark_sky_cloud_coverage

          above: 80

      condition:

        condition: and

        conditions:

          - condition: state

            entity_id: group.devices_tracker

            state: 'home'

          - condition: time

            after: '09:00'

          - condition: sun

            before: 'sunset'

          - condition: numeric_state

            entity_id: sensor.dark_sky_precip_intensity

            above: '0.5'

      action:

        service: switch.turn_on

        entity_id: group.living_room

This automation should turn off every light when:

Cloud coverage is under 80%, when it’s after 09:00 o’clock and 1 hour before sunset(because i already have a automation for the sunset and don’t want to interfere that ^)


- alias: 'Lights: Dark day is over'

  trigger:

    - platform: numeric_state

      entity_id: sensor.dark_sky_cloud_coverage

      below: 80

  condition:

    condition: and

    conditions:

      - condition: time

        after: '09:00'

      - condition: sun

        before: 'sunset'

        before_offset: "1:01:00"

  action:

    service: switch.turn_off

    entity_id: group.living_room

Love to hear what you guys think or what i have to change to make it more efficient.

I don’t think you have to worry. I’ve seen way more complicated automations in YAML than yours.

Happy automating.

But the automations are correct and not interfering with each other? Like the one that turns off the light after a dark day? Jus started and zero experience;) I know it’s not really complicated but I like clean code:)

In general looks good to me, too. The only issue is the before_offset in the condition of the last automation. I think you want:

before_offset: "-01:00"

The after_offset and before_offset options for the sun condition are a bit confusing. Think if it this way: after_offset just means it goes with the after condition; it doesn’t mean the offset is after the event. Same with before_offset – it just means it goes with the before condition, not that the offset is before the event. Only the sign of the offset determines if it is before or after the event.

So, in your case, you want the condition to be true before one hour before sunset. The first before in that sentence means you use the before and before_offset options (as opposed to after and after_offset), and the second before means you use a negative offset.

Thanks pnbruckner! Just what i thought. Changed it and seems to work.

1 Like