Simple Automation Example [Solved]

Hi,

I am new to HA and running it on an Ubuntu machine at home and trying to get hands on with automation. So used the automation editor to create a simple automation on Brother’s printer and create a notification if the page counter exceeds 1,000, the counter is currently at 1,100 so notification should pop-up.
I followed following steps:
Step-1:


Step-2:

Step-3:

Step-4:

After completing these steps, the automation is not getting saved nor am I getting any error so not sure if I am doing something wrong or how to correct it.

Any pointers will be appreciated!
Thanks.

This automation will only trigger when the page count goes from below 1000 to above 1000.

If it’s already above 1000 it won’t trigger.

@tom_l thanks for pointing that out, the current counter is at 1173, so I raised the condition value to 1175.

But I am still not able to save the template and getting no error either.

Regards.

So pressing the orange save button lower right does nothing?

How about instead of using a state trigger and a numeric condition you just use a numeric state trigger and no condition?

Yes, pressing the orange button is not doing anything. At first I got a YAML error due to a typo in Actions tags. So based on your suggestion I simplified my example even further and deleted Condition and modified triggers as:

But still getting no error but unable to save automation. I also played around with variations for "for: field using values such as “00:00:05” but still cannot save the automation.

So I took another approach and wanted to write the same automation in a YAML file, the VSC does not show any errors of tags:

- alias: 'Test automation'
  trigger:
    - platform: state
      entity_id: sensor.mfc_j485dw_page_counter
  condition: and
    {%  if (states('sensor.mfc_j485dw_page_counter') | integer ) > 1174 %}

  action:
    - service: persistent_notification.create
      data:
        title: "Brother Printer Page Counter Message"
        message: "Page counter has exceeded 1174 pages now"

But the configuration is showing following error:

Invalid config for [automation]: Expected a dictionary @ data['condition'][0]. Got None. (See /config/configuration.yaml, line 9).

Any pointers

Your automation’s condition is invalid. I suggest you review the examples shown in the documentation.

Thanks @123. I finally got it working and had to tweak a lot of things:

- alias: "Printer Test Automation"
  trigger:
    - platform: state
      entity_id: sensor.mfc_j485dw_page_counter
  action:
    - choose:
        # IF page cpunter is more than 1,000
        - conditions:
            - condition: template
              value_template: "{{ sensor.mfc_j485dw_page_counter > 1000 }}"
          sequence:
            - service: persistent_notification.create
              data:
                title: "Printer has printed more than 1,000 pages"
                message: "Need to get the printer serviced"
      # ELSE (i.e. page counter is less than 1,000)
      default:
        - service: persistent_notification.create
          data:
            title: "Printer has printed less than 1,000 pages"
            message: "Your printer is in good health"

Thanks for pointing me in the right direction!

Here’s another way to do the same thing:

- alias: "Printer Test Automation"
  trigger:
    - platform: state
      entity_id: sensor.mfc_j485dw_page_counter
  action:
    - service: persistent_notification.create
      data_template:
        title: >
          Printer has printed {{ 'more' if trigger.to_state.state|int > 1000 else 'less' }} than 1,000 pages
        message: >
          {{ 'Need to get the printer serviced' if trigger.to_state.state|int > 1000 else 'Your printer is in good health' }} 

Thanks @123 , your code seems more elegant but I was not getting notification executing it. A look at the logs revealed following error:

2020-08-23 18:35:04 ERROR (MainThread) [homeassistant.components.automation.printer_test_automation_v2_0] Printer Test Automation v2.0: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined

Any idea how to fix it?

How did you trigger the automation?

If you triggered it manually, that skips the automation’s trigger (and condition if it has one) and executes the action only. When it does that, the Trigger State Object is undefined because the automation’s trigger never executed.

From the documentation:

Be aware that if you reference a trigger state object in templates of automation action , attempting to test that automation by calling the automation.trigger service or by clicking EXECUTE in the More Info box for the automation will not work. This is because the trigger state object doesn’t exist in those contexts

I triggered using the “execute” feature manually, your explanation makes sense.

Thanks and appreciate all these insights :+1: :smile:

1 Like