Help with templates in automations

Hi everyone! I’m on HomeAssistant for a while but never tried more complex stuff.
Recently got a power problem that burnt my machine so I started a new installation from scratch.

Now trying to make an automation work using a template condition and happens that I can’t figure out what the problem is. If anyone could help me here I’d appreciate a lot.
So it is supposed to activate one of the scenes when triple click on a button in my house, depending on this boolean state. But instead it does nothing.
Thanks in advance!

- id: "65453467786"
  alias: Toggle Natal
  description: ""
  trigger:
    - platform: device
      domain: mqtt
      device_id: kljnfkd45346nj4235jnl2534
      type: button_triple_press
      subtype: button_1
      discovery_id: BTN_1_TRIPLE
  condition: []
  action:
    - service: scene.turn_on
      entity_id: >
        {% if is_state('input_boolean.natal', 'on') -%}
          scene.natal_apagado
        {% else -%}
          scene.natal_aceso
        {%- endif %}
  mode: single

When debugging an automation, the first step is to determine if the problem is due to a faulty trigger or action. Go to Configuration > Automations, find the automation Toggle Natal, and click EXECUTE. This will skip the automation’s trigger and simply execute the action. If either of the two scenes is turned on then you have confirmed the action works correctly and the problem may be due to an incorrect trigger. However, if neither scene is turned on, or the wrong scene, then you know the problem is in the action.

1 Like

I’ve tried that and in the logbook I can see it was triggered but nothing happens. What I did so far was to split it into two different automations, but I’d like to make it only one so it looks better. It seems to be just a simple mistake. I’ve used a template just like that to send a notification on other automation and it worked. Now I’m thinking if it is possible to use a template to define an action entity_id or if it’s not allowed at all.

Change the action to this:

  action:
    - service: scene.turn_on
      data:
        entity_id: >
          {% if is_state('input_boolean.natal', 'on') -%}
            scene.natal_apagado
          {% else -%}
            scene.natal_aceso
          {%- endif %}

Or to this (uses a compact inline-if statement):

  action:
    - service: scene.turn_on
      data:
        entity_id: "scene.natal_{{'apagado' if is_state('input_boolean.natal', 'on') else 'aceso' }}"

Thanks for the help!
Again it is not working. Neither the long and the short notation.
It looks like this now:

- id: "65453467786"
  alias: Toggle natal
  description: ""
  trigger:
    - platform: device
      domain: mqtt
      device_id: kljnfkd45346nj4235jnl2534
      type: button_triple_press
      subtype: button_1
      discovery_id: BTN_1_TRIPLE
  condition: []
  action:
    - service: scene.turn_on
      data:
        entity_id: >
          {% if is_state('input_boolean.natal', 'on') -%}
            scene.natal_apagado
          {% else -%}
            scene.natal_aceso
          {%- endif %}
  mode: single

Are there error messages in Configuration > Logs?

Paste this into Developer tools > Template Editor then change the state of input_boolean.natal from on to off to on. As you change its state, the Template Editor should report scene.natal_apagado then scene.natal_aceso.

scene.natal_{{'apagado' if is_state('input_boolean.natal', 'on') else 'aceso' }}

Nothing on logs and in the templates it works. When I change the boolean, it switches from natal_aceso to natal_apagado. Super weird haha

You confirmed:

  • The automation triggers automatically because it reports a LastTriggered time (so trigger works)
  • The action executes when the automation is triggered manually (so action works)
  • There are no errors reported (no syntax or execution errors).

Those three don’t add up to explain why the automation fails to execute the action when triggered automatically. Some other factor is involved here.

Yeah! It’s super weird. I mean. On the Templates section it works well. The trigger works. I’ve used templates to do other if else in other automations and they work well. This one is reeeeally weird.
But anyway, thanks a lot for you help with this :slight_smile: