Automation for Fountain

Hi, can anyone help me write a simple automation in visual editor format, to switch a garden fountain on/off based on time of day and instantaneous weather? I would like to have the fountain on during daylight whenever the weather is good (low cloud cover and low wind).

I’m using a Shelly switch which is working fine, I’m just struggling to construct an automation using Sun and Home Forecast. Many thanks.

Below my existing automation, which checks the sun position and cloud cover every hour, then turns on the fountain only while it’s daytime and the cloud cover is below 50%. At least that’s the theory - I haven’t tested it yet.

alias: Fountain Control
description: ""
triggers:
  - trigger: time_pattern
    minutes: "0"
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: sun.sun
            state: above_horizon
          - condition: numeric_state
            entity_id: weather.forecast_home
            attribute: cloud_coverage
            below: 50
        sequence:
          - type: turn_on
            device_id: 5994272570289321466829f9f3063bbb
            entity_id: a6b8948af4599cb06a3e849ad6a58c75
            domain: switch
      - conditions:
          - condition: state
            entity_id: sun.sun
            state: below_horizon
        sequence:
          - type: turn_off
            device_id: 5994272570289321466829f9f3063bbb
            entity_id: a6b8948af4599cb06a3e849ad6a58c75
            domain: switch
      - conditions:
          - condition: numeric_state
            entity_id: weather.forecast_home
            attribute: cloud_coverage
            above: 50
        sequence:
          - type: turn_off
            device_id: 5994272570289321466829f9f3063bbb
            entity_id: a6b8948af4599cb06a3e849ad6a58c75
            domain: switch
mode: single

Here’s how I would do it. The automation is completely event-based; it triggers only when specific events occur such as sunrise/sunset and when cloud coverage is above/below 50. It does not use a Time Pattern Trigger to repeatedly (and often needlessly) test if the conditions meet the desired requirements.

alias: Fountain Control
description: ""
triggers:
  - trigger: event
    event: sunrise
    variables:
      mode: |
        {{ 'on' if state_attr('weather.forecast_home', 'cloud_coverage') | int(0) < 50 else 'off' }}
  - trigger: event
    event: sunset
    variables:
      mode: 'off'
  - trigger: numeric_state
    entity_id: weather.forecast_home
    attribute: cloud_coverage
    above: 50
    variables:
      mode: 'off'
  - trigger: numeric_state
    entity_id: weather.forecast_home
    attribute: cloud_coverage
    below: 50
    variables:
      mode: |
        {{ 'on' if is_state('sun.sun', 'above_horizon') else 'off' }}
conditions: []
actions:
  - action: switch.turn_{{ mode }}
    target:
      entity_id: switch.your_fountain
mode: single

How it works

  • It triggers at sunrise and sets the value of mode to on if cloud coverage is below 50, otherwise it’s set to off.

  • It triggers at sunset and sets the value of mode to off.

  • It triggers when cloud coverage increases above 50 and sets mode to off.

  • It triggers when cloud coverage decreases below 50 and sets mode to on if it is daytime, otherwise it’s set to off.

  • The action is either switch.turn_on or switch.turn_off depending on the value of mode.

If you use it, be sure to replace switch.your_fountain with the entity_id of your switch.

1 Like

Thank you very much for the very detailed answer. Makes sense.

My only concern is that adding additional weather constraints will make the code rather complex. I’ve tried to simplify my version by using a default action to turn the fountain off, but also added a test of the wind speed. Still untested though.

alias: Fountain Control
description: ""
triggers:
  - trigger: time_pattern
    minutes: "0"
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: sun.sun
            state: above_horizon
          - condition: numeric_state
            entity_id: weather.forecast_home
            attribute: cloud_coverage
            below: 50
          - condition: numeric_state
            entity_id: weather.forecast_home
            attribute: wind_speed
            below: 10
        sequence:
          - type: turn_on
            device_id: 5994272570289321466829f9f3063bbb
            entity_id: a6b8948af4599cb06a3e849ad6a58c75
            domain: switch
    default:
      - type: turn_off
        device_id: 5994272570289321466829f9f3063bbb
        entity_id: a6b8948af4599cb06a3e849ad6a58c75
        domain: switch
mode: single

If you don’t mind having your switch either turned on or turned off 1440 24 times a day, then use your approach.

Another approach is to create a Template Trigger that incorporates all conditions required to turn on the switch. However it involves a template and I suspect you aren’t comfortable with it just yet.

It would involve adding another trigger which isn’t more complicated than having to add another condition (as you did in your latest example). Same effort but a more efficient result.

I would post an amended example, demonstrating how it easy it is, but I get the impression you’re already sold on using polling as opposed to event-driven.

Thanks again. The trigger is the 0th minute of every hour, not the 0th second of every minute. As I understand it, the weather integration refresh interval is anyway only 30 minutes.

Right, certainly not comfortable with a Template Trigger. But something to investigate though - thanks very much for the suggestion.

1 Like