Order of variables in automation?

I’m declaring some variables at the top of my automation to simplify some of the logic later. For example:

variables:
  is_daytime: >-
    {{ (now().hour * 60 + now().minute > 5 * 60 + 00) and (now().hour * 60 +
    now().minute < 23 * 60 + 00) }}
  is_outside_camera: "{{ source_camera != 'familyroom' and source_camera != 'familyroom_pool' }}"
  should_describe: "{{ (not is_daytime) or is_outside_camera }}"

I occasionally get errors in the logs that is_daytime is undefined, leading me to believe that HA is evaluating should_describe before is_daytime.

Is there a way to control evaluation order of the variables or is there a better approach?

Hi Ryan,

Maybe…

Appreciate the response. That post talks more about the scope and evaluation order across the automation. I’m trying to figure out evaluation order within the variables section (var_3 in the example you linked to).

That said, I wonder if I can hack this together by putting “base” variables in earlier sections and “dependent” variables in areas that post indicates get evaluated later.

The templates should be rendered in order, read from top to bottom, so the way you have them should be fine. Is the error showing up during runs of the automation or at some other time like restart?

You can simplify you templates a little bit using tuple comparison instead of calculating minutes and using the in test instead of 2 separate equality operations.

variables:
  is_daytime: "{{ (23,0) > (now().hour,now().minute) > (5,0) }}"
  is_outside_camera: "{{ source_camera not in ['familyroom', 'familyroom_pool'] }}"
  should_describe: "{{ (not is_daytime) or is_outside_camera }}"
1 Like

There’s also the Times Of Day binary sensor. If you use this night condition in a lot of places it would be easier to set one of these up.

1 Like

I’m seeing two problems:

  1. The script variables evaluate incorrectly. Specifically, should_describe evaluates to true even though is_daytime is true and is_outside_camera is false. I see that in the trace, here:
should_describe: true
is_daytime: true
is_outside_camera: false
  1. I see the following in the logs:
Template variable warning: 'is_daytime' is undefined when rendering '{{ (not is_daytime) or alert_during_daytime }}'

…indicating that should_describe is evaluated before is_daytime.

Here’s an oddity / hint that indicates the order in the yaml isn’t respected. The script [intentionally] orders them as I need them evaluated:

  is_daytime
  is_outside_camera
  should_describe

…but when I look at a traces “script config”, it shows the variables in a different order than the actual script:

  should_describe
  is_daytime
  is_outside_camera

…as seen in the following screenshot.

should_describe requires is_daytime and alert_during_daytime before they are defined.

Correct. That’s the order it’s defined in the script.
The trace->script config shows them in a different order than the script itself.

Reload the browser cache, then put the variables in the desired order and resave.

1 Like

yah just did the same right as you were replying. Also restart HA. It’s working now and the script config matches the order of the script itself. Very odd.

Thank you for the help, and also appreciate the code optimizations!