jeroenterheerdt / HAsmartirrigation

Smart Irrigation custom component for Home Assistant
https://jeroenterheerdt.github.io/HAsmartirrigation/
MIT License
359 stars 49 forks source link

[REQ] blueprints for automations #170

Closed jeroenterheerdt closed 9 months ago

jeroenterheerdt commented 1 year ago

ship blueprints in-box:

gatonero commented 1 year ago

@jeroenterheerdt I'm making some progress with the blueprints for esphome. I think I'll be done with it in a day or two.

gatonero commented 1 year ago

Here is the blueprint for esphome. There is minimum one bug at the end, where the number of days since last irrigation is reset to zero. In a script it works without any flaw but not in the automation. I don't have any clue why?

blueprint:
  name: ESPHome Irrigation
  description: This automation allows you to control the irrigation of different zones with one ESP. The number and order of valves and zones must correspond.
  domain: automation
  input:
    esphome_valve:
      name: ESPHome valve
      description: ESPHome valve to open and close
      selector:
        entity:
          multiple: true
          filter:
            - domain: switch
              integration: esphome

    irrigation_zone:
      name: Irrigation Zone
      description: Smart Irrigation zone to irrigate
      selector:
        entity:
          multiple: true
          filter:
            - device_class: duration
              integration: smart_irrigation

alias: ESPHome Irrigation
description: "ESPHome Irrigation"

variables:
  esphome_valve: !input esphome_valve
  irrigation_zone: !input irrigation_zone
  esphome_count: "{{ expand(esphome_valve) | list | count }}"
  irrigation_count: "{{ expand(irrigation_zone) | list | count }}"

trigger:
  platform: event
  event_type: smart_irrigation_start_irrigation_all_zones

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{esphome_count != irrigation_count}}"
        sequence:
          - service: persistent_notification.create
            data:
              message: >-
                Error: You must choose the same number of valves and zones!
      - conditions:
          - condition: state
            entity_id: input_boolean.irrigation_enabled
            state: "off"
        sequence:
          - service: persistent_notification.create
            data:
              message: >-
                Irrigation is not enabled
      - conditions:
          condition: template
          value_template: >-
            {{states('input_number.number_of_days_since_last_irrigation') < states('input_number.irrigate_every')}}
        sequence:
          - service: input_number.increment
            data: {}
            target:
              entity_id: input_number.number_of_days_since_last_irrigation
    default:
      - repeat:
          for_each: !input irrigation_zone
          sequence:
            - if:
                - condition: template
                  value_template: >-
                    {{states(repeat.item)=='disabled' or
                    states(repeat.item)|int()==0}}
              then:
                - service: switch.turn_off
                  target:
                    entity_id: >-
                      {{esphome_valve[repeat.index - 1]}}
                  data: {}
              else:
                - service: switch.turn_on
                  target:
                    entity_id: >-
                      {{esphome_valve[repeat.index - 1]}}
                  data: {}
                - service: esphome.sprinklercontroller_set_valve_run_duration
                  data:
                    duration: "{{states(repeat.item)}}"
                    valve: "{{repeat.item|int(0)}}"

            # - service: persistent_notification.create
            #   data:
            #     message: >-
            #       {{states(repeat.item)}}

            #       {{repeat.index - 1}}

            #       {{esphome_valve[repeat.index - 1]}}

      - service: esphome.sprinklercontroller_start_full_cycle
        data: {}

      - service: smart_irrigation.reset_all_buckets
        data: {}

      - service: input_number.set_value
        data:
          value: 0
          target:
            entity_id: input_number.number_of_days_since_last_irrigation

mode: single
jeroenterheerdt commented 12 months ago

hey @gatonero thanks for the blueprint - I discovered another logical error in my automation. Here's hopefully my final automation. Can you update the blueprint?

alias: Smart Irrigation
trigger:
  - platform: event
    event_type: smart_irrigation_start_irrigation_all_zones
  - platform: sun
    event: sunrise
    offset: "01:00:00"
  - platform: time
    at: "00:00:00"
    id: midnight
condition:
  - condition: state
    entity_id: input_boolean.irrigation_enabled
    state: "on"
action:
  - if:
      - condition: trigger
        id:
          - midnight
    then:
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.irrigation_automation_ran_today
    else:
      - if:
          - condition: state
            entity_id: input_boolean.irrigation_automation_ran_today
            state: "off"
        then:
          - if:
              - condition: template
                value_template: >-
                  {{states('input_number.number_of_days_since_last_irrigation')<states('input_number.irrigate_every')}}
            then:
              - service: input_number.increment
                data: {}
                target:
                  entity_id: input_number.number_of_days_since_last_irrigation
            else:
              - service: input_number.set_value
                target:
                  entity_id: input_number.smart_irrigation_number_of_zones_enabled
                data:
                  value: 0
              - repeat:
                  for_each:
                    - sensor: sensor.smart_irrigation_lawn_main_deck_owm
                      vid: 0
                      zid: 1
                    - sensor: sensor.smart_irrigation_lawn_tree
                      vid: 1
                      zid: 2
                    - sensor: sensor.smart_irrigation_lawn_office
                      vid: 2
                      zid: 3
                    - sensor: sensor.smart_irrigation_herbs
                      vid: 3
                      zid: 4
                    - sensor: sensor.smart_irrigation_greenhouse
                      vid: 4
                      zid: 5
                    - sensor: sensor.smart_irrigation_side_house
                      vid: 5
                      zid: 6
                  sequence:
                    - if:
                        - condition: template
                          value_template: >-
                            {{state_attr(repeat.item.sensor,'State')=='disabled'
                            or states(repeat.item.sensor)|int(default=0)==0}}
                      then:
                        - service: switch.turn_off
                          target:
                            entity_id: >-
                              {{'switch.sprinklercontroller_enable_zone_'+repeat.item.zid|string()}}
                          data: {}
                      else:
                        - service: switch.turn_on
                          target:
                            entity_id: >-
                              {{'switch.sprinklercontroller_enable_zone_'+repeat.item.zid|string()}}
                          data: {}
                        - service: input_number.increment
                          target:
                            entity_id: >-
                              input_number.smart_irrigation_number_of_zones_enabled
                          data: {}
                        - service: esphome.sprinklercontroller_set_valve_run_duration
                          data:
                            duration: "{{states(repeat.item.sensor)|int(default=0)}}"
                            valve: "{{repeat.item.vid}}"
              - if:
                  - condition: template
                    value_template: >-
                      {{states('input_number.smart_irrigation_number_of_zones_enabled')|int(default=0)>0}}
                then:
                  - service: esphome.sprinklercontroller_start_full_cycle
                    data: {}
                    enabled: true
                  - service: smart_irrigation.reset_all_buckets
                    data: {}
                  - service: input_number.set_value
                    data:
                      value: 0
                    target:
                      entity_id: input_number.number_of_days_since_last_irrigation
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.irrigation_automation_ran_today
mode: single
gatonero commented 12 months ago

I will have a look tomorrow.

gatonero commented 12 months ago

@jeroenterheerdt could you please tell me where and what you changed. I changed a lot to get a template from your automation. Because it has been some days ago I made the template it's difficult for me to locate the changes.

jeroenterheerdt commented 12 months ago

@gatonero here you go: https://www.diffchecker.com/ennJee7h/.

In summary:

  1. Another trigger at midnight
  2. An extra input_boolean (irrigation_automation_ran_today) to make sure we don't run it more than once
  3. I moved the increment of number_of_days_since_last_irrigation out of the irrigation logic so it also is increased when we don't actually irrigate, but ran the automation.
  4. Count number of enabled zones and only trigger actual irrigation when that is >0, because otherwise ESPHOME runs all zones on a default duration.
github-actions[bot] commented 10 months ago

Stale issue message