Can an automation be tested before it gets deployed?

I know we have Developer Tools in HA that offer a lot of stuff however I wonder if there is a way to “test” (at least the syntax) successfully an Automation? Right now I am not sure that the following condition would work:


and the only way I have found to test it is by restarting HA entirely and waiting on it to be back and then firing the event from my phone/watch.

Is there a way to log errors from the automation?

My logger section looks like:

logger:
  default: warning
  logs:
    custom_components.localtuya: debug
    custom_components.localtuya.pytuya: debug

A few things…

When you create or edit an automation in the UI you don’t need to restart HA for the changes to take effect. The automation will automatically reload and become active when it gets saved.

you can test the automation by going to the dev tools->services page and select automation.trigger in the drop down. then select the automation you want to trigger.

if you want to actually test the conditions then turn off the “skip conditions” switch in the UI. otherwise just the actions will run.

the condition as you have it won’t work because you are using the wrong type of bracket notation.

they need to be the {{ … }} type:

{{ state_attr('climate.home', 'current_temperature') < 70 }}

the {% type of brackets denote a process portion of the template (calculation, comparison, etc). The {{ brackets denote the actual returned value of the template

Lastly please don’t post screenshots of code. post actual text and use ``` to make sure it’s properly formatted.

it allows us to just copy/paste text instead of needing to write the whole thing out again (like I had to do above to correct your error). And we can see errors in syntax if it’s properly formatted.

You may want to add that automation.trigger never tests the automation’s triggers. Plus it cannot be used for testing if the automation employs the trigger (or this) variables.

Reference: Testing your automation

1 Like

I want to add that you can test a condition basically by clicking the 3 dots.

Invalid template syntax (like in your example):

Valid syntax:

Also you simulate states (Developer Tools —> States —> set state)

1 Like

When you use the Automation Editor, it checks the automation’s syntax the moment you click the Save button.

What the Save button won’t report are logic errors (i.e. the automation’s operation fails to meet your requirements; it doesn’t behave as expected). To properly test an automation, you should trigger it via its triggers. To expedite an entity’s state-change, you can force an entity’s state to a desired value in Developer Tools > States.

1 Like

I must say thanks to all of you. Learned something new today! The way I was testing automation was by turning on devices and setting the values as they should for the automation to work properly. :neutral_face:

I must be missing something here … this is what the automation looks like:

- id: '1675884634266'
  alias: Turn HVAC On
  description: ''
  trigger:
  - platform: event
    event_type: ios.action_fired
    event_data:
      actionName: HvacOn
  - platform: time
    at: '22:30:00'
  condition:
  - condition: state
    entity_id: climate.home
    attribute: climate_mode
    state: 'off'
  - condition: or
    conditions:
    - condition: template
      value_template: '{{ state_attr(''weather.home'', ''temperature'') > 76 }}'
  action:
  - service: climate.turn_on
    data: {}
    target:
      entity_id: climate.home
  - service: climate.set_hvac_mode
    data:
      hvac_mode: cool
    target:
      entity_id: climate.home
  - service: climate.set_fan_mode
    data:
      fan_mode: auto
    target:
      entity_id: climate.home
  - service: climate.set_temperature
    data:
      temperature: 72
    target:
      entity_id: climate.home

I went to Developer Tools > State and set the temperature to 70 and triggered the automation from Developer Tools > Services and no matter what I get a green checkmark. Should I get something different there?

The check mark just means that the service call to trigger the automation was successful.

it has nothing to do with saying that the actions actually ran or ran correctly. that’s up to you to decide.

and just because you set the state to 70 doesn’t mean it actually stayed at 70 during the test. those states will get updated to the actual values at the next update cycle, which could be just a few seconds.

That is actually the best way to test them because you then know for sure that the trigger and everything else works.

1 Like

There’s a debug trace in the automation UI.
Have the automation actions been executed?

I guess the problem here is what @finity said since I might not be fast enough to test before the state gets back to it is original.