Multiples template, sensors and triggers

I’m working on my automated irrigation system. I’m trying to calculate 2 sensors :

  • rain probability (based on forecast)
  • irrigate (yes / no) based on a simple calculation (could be more complex in the future).

I’m quite new with HA, YAML and all this environment, so, I’m sure my mistakes are quite dumb.

My problem all of my 2 sensors are unavailable.

When it works, I would in an ideal world, calculate the first sensor (probability) on specific time of the day. And the second one (irrigate) on update on the first sensor.

I’m struggling as well to test it and to be sure all my changes are correctly taken in account. Thanks for your help. My code below (package yaml).

Thanks for your help!

# Irrigation Automation (dedicated file : packages_irrigation.yaml)

- template:
    - trigger:
        - platform: time_pattern
          minutes: "/1"
      action:
        - service: weather.get_forecasts
          data:
            type: daily
          target:
            entity_id: weather.ferme_myhome
          response_variable: daily
      sensor:
        name: CUST-SE-irrigation-dailyRainForecast
        unique_id: CUST-SE-irrigation-dailyRainForecast
        state: "{{ daily['weather.ferme_myhome'].forecast[0].precipitation }}"
        unit_of_measurement: "mm"

    - trigger:
        - platform: time_pattern
          minutes: "/1"
      binary_sensor:
        name: CUST-SE-irrigation-zone1-irrigate
        unique_id: CUST-SE-irrigation-zone1-irrigate
        device_class: problem
        value_template: >
          {% set forecast = states('sensor.CUST-SE-irrigation-dailyRainForecast') | float %}
          {% set threshold = states('input_number.HL-irrigation-zone1-threshold') | float %}
          {{ forecast < threshold }}

Is it possible you’ve exceeded the API limit for your weather service by trying to pull the forecast every minute? Seems extreme.

That delay was just for testing purpose, but you right there is absolutely no value to check that every minutes. I want for the productive version to change that to a time trigger which fires at 00:01, 18h01 only. I will irrigate during evening or night, so, I have accurate prevision value.

This post provides some helpful details and a method to debug it in developer tools.

I don’t solve it yet the issue, but I think I pointed the problem and need your help.
The code above is in a dedicated file, a package. I include this file in the initial configuration.yaml. It was working with only one template but not 2.

# Include custom packages
homeassistant:
  packages: !include_dir_named packages
    

I’m still struggling on how I need to split things. I liked the idea to have all my template / sensor in a dedicated file.

isn’t the problem with this bit:

 - template:

Because it should just be:

template:
1 Like

Thanks, and which kind of include dir should I use in the main configuration? I have the feeling only my first template is taken into account. :frowning:

These are mine:

# Core components to make them available quickly
mqtt:
  sensor: !include_dir_merge_list cfg/_component/mqtt/sensor/
  binary_sensor: !include_dir_merge_list cfg/_component/mqtt/binary_sensor/
  switch: !include_dir_merge_list cfg/_component/mqtt/switch/
sensor: !include_dir_merge_list cfg/_component/sensor/
binary_sensor: !include_dir_merge_list cfg/_component/binary_sensor/
climate: !include_dir_merge_list cfg/_component/climate/
switch: !include_dir_merge_list cfg/_component/switch/
camera: !include_dir_merge_list cfg/_component/camera/
scene: !include scenes.yaml
script: !include scripts.yaml
automation: !include automations.yaml
group: !include_dir_merge_named cfg/_component/group/
light: !include_dir_merge_list cfg/_component/light/
template: !include_dir_merge_list cfg/_component/template/
notify: !include notifiers.yaml
# cover: !include_dir_merge_list cfg/_component/cover/

media_player: !include_dir_merge_list cfg/_component/media_player/
timer: !include_dir_merge_named cfg/_component/timer/

Taking template as an example:

the files contain content like this:

/homeassistant/cfg/_component/template/solar_production_template.yaml

  - trigger:
      - platform: state
        entity_id:
          - sensor.mll8d7s00v_output_power
          - sun.sun
    binary_sensor:
      - name: Solar Generation 
        unique_id: pv-gen-c1e055c9-af11-4131-8202-928685d70c2a
        state: >-
          {% 
            if (states('sensor.mll8d7s00v_pv_status')|int(0) == 0) 
            or (state_attr('sun.sun','elevation')|float(-99) < 1) 
            or (states.sensor.mll8d7s00v_last_data_update.last_updated | as_local) > (now() - timedelta(minutes=15)) 
          %}
            {{ false }}
          {% else %}
            {% if states('sensor.mll8d7s00v_output_power')|float(0) > 0 %}
              {{ true }}
            {% else %}
              {{ false }}
            {% endif %}
          {% endif %}
        device_class: power

Packages should be configured exactly as you have it in Post #5.

Only your first sensor is showing up because the binary sensor configuration has a few issues:

  1. The configuration is using the incorrect configuration key value_template, the proper key is state
  2. There doesn’t appear to be a reason for it to be trigger-based. Removing the trigger will allow it to update whenever the forecast sensor or threshold number input are updated.
  3. The entity IDs need to be slugs… Hyphens are not an allowed character in entity ID’s they should be replaced with underscores.
  4. If you are not going to provide defaults for your float filters you should definitely set up an availability template.
# Irrigation Automation (dedicated file : packages_irrigation.yaml)

template:
  - trigger:
      - platform: time_pattern
        minutes: "/1"
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.ferme_myhome
        response_variable: daily
    sensor:
      - name: CUST-SE-irrigation-dailyRainForecast
        unique_id: CUST-SE-irrigation-dailyRainForecast
        state: "{{ daily['weather.ferme_myhome'].forecast[0].precipitation }}"
        unit_of_measurement: "mm"

  - binary_sensor:
      - name: CUST-SE-irrigation-zone1-irrigate
        unique_id: CUST-SE-irrigation-zone1-irrigate
        device_class: problem
        state: >
          {% set forecast = states('sensor.cust_se_irrigation_dailyRainForecast') | float %}
          {% set threshold = states('input_number.hl_irrigation_zone1_threshold') | float %}
          {{ forecast < threshold }}
        availability: "{{ has_value('sensor.cust_se_irrigation_dailyRainForecast') and has_value('input_number.HL_irrigation_zone1_threshold') }}"