How to setup automation with helper as number variable used in condition and actions

I’m still very new to HA, but I’ve been banging my head against a wall for what I feel is a stupid simple automation setup. I’m trying to utilize a variable (I’m familiar enough in Python & Jinja) and effectively just want to set some basic value which I can subsequently utilize in the automation: both in the conditions and in the subsequent actions.

My end goal is to have an automation run at a set time every day which checks whether or not any of my printer’s ink is below a certain percentage (the variable) and subsequently send a notification if it is.

How I have this setup with the functional and non functional components:

  • Working: I have created an input_number Helper with my desired ink threshold (real: 11.0)
  • Working: I have notification actions which successfully sends a message with the desired threshold variable included
  • Not Working: conditions which correctly only cause the automation to trigger actions if below the set threshold

What I’ve tried:

  • Using jinja to populate the below with the helper value. This produces a dictionary error when I try to save the automation saying it’s expecting a float value
  • Using the native UI to reference the helper value. Every time I subsequently complete a test run my actions are triggered to send a notification even though none of my printer’s ink sensors actually report a percentage below the set helper value… this YAML I’ve included below:
alias: "PRINTER: Notify on low printer toner"
description: If below 'input_number.printer_ink_min_percent', notify
trigger:
  - platform: time
    at: "10:00:00"
condition:
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: sensor.hp_officejet_pro_6970_black_ink
        below: input_number.printer_ink_min_percent
      - condition: numeric_state
        entity_id: sensor.hp_officejet_pro_6970_cyan_ink
        below: input_number.printer_ink_min_percent
      - condition: numeric_state
        entity_id: sensor.hp_officejet_pro_6970_magenta_ink
        below: input_number.printer_ink_min_percent
      - condition: numeric_state
        entity_id: sensor.hp_officejet_pro_6970_yellow_ink
        below: input_number.printer_ink_min_percent
action:
  - service: notify.mobile_app_pixel_8_pro
    data:
      message: >-
        One or more toner cartridges are below {{ states('input_number.printer_ink_min_percent')|round }}%
      title: Printer Toner Low
  - service: notify.persistent_notification
    metadata: {}
    data:
      title: Printer Toner Low
      message: >-
        One or more toner cartridges are below {{ states('input_number.printer_ink_min_percent')|round }}%
mode: single

What am I doing wrong or missing to achieve my desired goal?

Thanks in advance!

You cannot use templating everywhere. In conditions, they can only be used in Template conditions. The same is mostly true for triggers, though you can use them in a limit fashion in Numeric State triggers.

Using “Run” from the options menu is only for testing the action, it skips the trigger and conditions completely. There’s nothing wrong with your example conditions, just your testing method.

alias: "PRINTER: Notify on low printer toner"
description: If below 'input_number.printer_ink_min_percent', notify
variables:
  min_per: "{{ states('input_number.printer_ink_min_percent')|round }}"
  ink_list: 
    - sensor.hp_officejet_pro_6970_black_ink
    - sensor.hp_officejet_pro_6970_cyan_ink
    - sensor.hp_officejet_pro_6970_magenta_ink
    - sensor.hp_officejet_pro_6970_yellow_ink
trigger:
  - platform: time
    at: "10:00:00"
condition:
  - condition: template
    value_template: |
      {{ expand(ink_list)|map(attribute='state')|map('float')|select('lt', min_per) | list | count > 0  }}
action:
  - service: notify.mobile_app_pixel_8_pro
    data:
      message: >-
        One or more toner cartridges are below {{ min_per }}%
      title: Printer Toner Low
  - service: notify.persistent_notification
    metadata: {}
    data:
      title: Printer Toner Low
      message: >-
        One or more toner cartridges are below {{min_per }}%
mode: single

Thanks for the information! I didn’t realize that the test run didn’t do any of the conditional evaluation, which is admittedly weird…

The command used to be named “Run Actions”.

Yet even then other users made the same mistake you did and assumed it executed the trigger (although I can’t imagine how), tested the condition and, if it evaluated to true, executed the actions. Which is admittedly a lot to assume of a command that’s says it will simply “Run actions”.

Now that it’s just called “Run” it’s even easier to make those all of those assumptions.

To test your automation, go to Developer Tools > Services and execute the automation.trigger service call, ensuring you turn off Skip Conditions. That will exercise the condition and, if it’s true, execute the actions.

Even automation.trigger is a bit of a misnomer; it doesn’t actually exercise the automation’s trigger(s). For example, your automation uses a Time Trigger set to 10:00:00 and automation.trigger doesn’t force the system clock to be that time in order to exercise the Time Trigger.

For more information about testing/debugging your automations, refer to the following documentation: Troubleshooting your automation

Appreciate the context! Thanks :slight_smile: