Error rendering data template within automation

Hi all,

I have an automation that calculates how much my garden must be watered based on rain values and then runs the sprinklers. The calculation is done within a template condition like this:

action:
  - if:
      - condition: time
        weekday:
          - wed
          - sun
        enabled: false
    then:
      - choose:
          - conditions:
              - condition: template
                value_template: >
                  {% set regenist = (states('sensor.rain_last_3_days') | float)
                  + (states('sensor.gw1100a_v2_2_3_daily_rain_rate') | float)%}
                  {% set regensoll = (12 | float) %}  {{ regenist < (0.125 *
                  regensoll) }}
            sequence:
              - service: input_select.select_option
                data:
                  option: "1.00"
                target:
                  entity_id: input_select.bewasserung_zeitwahl
              - service: notify.mobile_app_handynermin
                data:
                  title: Auto Bewässerung
                  message: >
                    Gieße volle Gießdauer (Regen IST ({{ regenist }}mm) weniger
                    als 1/8 des SOLL ({{(0.125 * regensoll)}}mm))

Error that I get when I run the automation is this: Error rendering data template: UndefinedError: ‘regensoll’ is undefined

When I put the same code into the template editor it work perfectly. but within the automation something goes wrong.

What mistake am i making here? Any ideas?

thx in advance,
zavjah

anyone any indea?

regensoll is only in scope for the block it’s in. You’ll have to recalculate it for the message.

if you make this a variable then you don’t need to recalculate anything.

action:
  - if:
      - condition: time
        weekday:
          - wed
          - sun
        enabled: false
    then:
      - variables:
          regenist: >
            {{ states('sensor.rain_last_3_days') | float + states('sensor.gw1100a_v2_2_3_daily_rain_rate') | float }}
          regensoll: 12
          mm: "{{ 0.125 * regensoll }}"
      - choose:
          - conditions:
              - condition: template
                value_template: "{{ regenist < mm }}"
            sequence:
              - service: input_select.select_option
                data:
                  option: "1.00"
                target:
                  entity_id: input_select.bewasserung_zeitwahl
              - service: notify.mobile_app_handynermin
                data:
                  title: Auto Bewässerung
                  message: >
                    Gieße volle Gießdauer (Regen IST ({{ regenist }}mm) weniger
                    als 1/8 des SOLL ({{ mm }}mm))
1 Like

Hello @petro ,

thx for your optimization, I’ll take that :smiley:

Hello @Troon ,

thx very much - I’ll rewrite and check if it works

Hello @Troon,
hello @petro,

I’ve rewritten the code as suggested by you @petro and doing it I also found out that I had a typo in one of the sensor names. It looks much better now and works like a charm. Thy guys