Assistance or clarification of conditions on action in template trigger sensor

The home assistant docs point here to document how conditions can be set to prevent an action from happening. They also point to this in the template trigger documentation since action has been added to template sensors (2023.9).

I tried this configuration, but the condition doesn’t prevent the action as defined. Any ideas where I’m going wrong? I’ve tried to simplify the template config to the bare minimum. Even though the condition template evaluates to false, the debug log is still written.

template:
  - trigger:
    - trigger: state
      entity_id:
        - sensor.example_1
        - sensor.example_2
    # debugging functionality
    action: 
      - action: notify.send_message
        target: 
          entity_id: notify.debug_log
        data:
          message: |
            {{ trigger }}
      - condition: "{{ 'TEMPHADEBUG' in states('input_text.amn_template_debug_target') }}"
    sensor:
      - name: "Example Sensor"
        state: >
          {% set temp = [states('sensor.example_1'), states('sensor.example_2')] | select('ne', 'unknown') | first | round(0, default=-99) %}
          {{ temp }}

Any eyes and comments appreciated.

You have made a mash up of standard and shorthand styles for condition configuration. Just use the standard notation:

template:
  - trigger:
    - trigger: state
      entity_id:
        - sensor.example_1
        - sensor.example_2
    # debugging functionality
    condition:
      - alias: Template must resolve to true for the action to be performed and the sensor state to update.
        condition: template
        value_template: "{{ 'TEMPHADEBUG' in states('input_text.amn_template_debug_target') }}"
    action: 
      - action: notify.send_message
        target: 
          entity_id: notify.debug_log
        data:
          message: "{{ trigger }}"
    sensor:
      - name: "Example Sensor"
        state: >
          {% set temp = [states('sensor.example_1'), states('sensor.example_2')] 
          | select('ne', 'unknown') | map('round', 0, default=-99) | sort(reverse=true) | first %}
          {{ temp }}

EDIT: Moved condition block and added alias to help demonstrate the order of actions.

So could I also have fixed that by just adding an “s”? From the docs:

Template condition shorthand notation

The template condition has a shorthand notation that can be used to make your scripts and automations shorter.

For example:

conditions: "{{ (state_attr('device_tracker.iphone', 'battery_level')|int) > 50 }}"

Thanks for the quick assist!

I don’t think the trigger-based template sensor config has been updated to accept the pluralized keys yet. I think it should work using the shorthand in the condition block, but I haven’t tested it yet.

Example with shorthand
template:
  - trigger:
    - trigger: state
      entity_id:
        - sensor.example_1
        - sensor.example_2
    # debugging functionality
    action: 
      - action: notify.send_message
        target: 
          entity_id: notify.debug_log
        data:
          message: |
            {{ trigger }}
    condition:
      - "{{ 'TEMPHADEBUG' in states('input_text.amn_template_debug_target') }}"
    sensor:
      - name: "Example Sensor"
        state: >
          {% set temp = [states('sensor.example_1'), states('sensor.example_2')] 
          | select('ne', 'unknown') | map('round', 0, default=-99) | first %}
          {{ temp }}

I added this exact code for the condition, but it didn’t make a difference. Not sure if it’s a bug or me.

I have just now tested both the standard format and the shorthand, and they both work fine on my instance. Double check your configuration.

Yep, it was me. Thanks for your help, sorry for the additional effort!

Hmm, it seems adding the condition to the action also prevents the sensor state from updating. Would that be expected?

That’s the entire point of adding the condition… :upside_down_face:

image

Is there something specific you are trying to do?

lol, yeah, right? :astonished:

I was thinking the action and sensor updates weren’t coupled, but obviously reading the documentation, examples and why the action was added, it’s fairly clear they are.

I’m trying to add logic in the template that can be executed when a flag is set for debugging purposes to write debugging data to a file, mainly details about the jinja2 code to make sure what I think is happening at template trigger is actually happening. Hoping to make this reuseable in the future.

I can always remove or comment out the action block that is doing the debug file, I was just hoping for something easier to turn on/off with an entity.

I guess instead of having the action write the file I could call a script and decide there if any file writing should occur, but it would still have the overhead of the initial script call. Maybe not that bad a tradeoff for occasional debugging purposes.

Or maybe I should just figure out how to test my code better in the developer tools template pane. :grinning:

If the goal is to have the sensor always update but not have the action fire unless a condition is met, use an If/Then action.

Can you give me an example of what that might look like, or point me to an example?

template:
  - trigger:
    - trigger: state
      entity_id:
        - sensor.example_1
        - sensor.example_2
    # debugging functionality
    action:
      - if:
          - condition: template
            value_template: |
              {{ 'TEMPHADEBUG' in states('input_text.amn_template_debug_target') }}
        then:
          - action: notify.send_message
            target: 
              entity_id: notify.debug_log
            data:
              message: "{{ trigger }}"
    sensor:
      - name: "Example Sensor"
        state: >
          {% set temp = [states('sensor.example_1'), states('sensor.example_2')] 
          | select('ne', 'unknown') | first | round(0, default=-99) %}
          {{ temp }}

I had no idea you could put and if then block in the action. Thanks, I’ll give it a shot!

AFAIK, all action types are allowed in the action block for trigger-based template entities.

1 Like

Just to follow through, your suggestion worked like a champ! Thanks for the help!

1 Like