Some templating help appreciated

I have a message:

        data:
          title: "Koi pond..."
          message: "...main pump has stopped"

and would like the word “stopped” to change between stopped, blocked and started based on the state of three separate input_booleans.

Where do I start with this?

I know that it needs to be along the lines of:

“… main pump has (if input_boolean.main_pump_stopped, “stopped”, else if input_boolean.main_pump_blocked, “blocked”, else_if input_boolean.main_pump_running, “started”)”

I struggle with the correct template syntax and formatting and haven’t yet found an idiots guide.

Any help will be much appreciated.

Hopefully you’re using the Template Editor to test out your template. There are a couple of links on that page to the documentation (the generic Jinja2 doc, as well as HA’s enhancements.)

But here’s a possible starting point for you:

data_template:
  title: "Koi pond..."
  message: >
    {% if is_state('input_boolean.main_pump_stopped', 'on') %}
      stopped
    {% elif is_state('input_boolean.main_pump_blocked', 'on') %}
      blocked
    {% elif is_state('input_boolean.main_pump_running', 'on') %}
      started
    {% else %}
      Um, well, not stopped, blocked or started!
    {% endif %}

BTW, always provide a final else clause.

Brilliant! That looks like what I need.

To concatenate that I’d just need…

"...main pump is     {% if is_state('input_boolean.main_pump_stopped', 'on') %}
      stopped
    {% elif is_state('input_boolean.main_pump_blocked', 'on') %}
      blocked
    {% elif is_state('input_boolean.main_pump_running', 'on') %}
      started
    {% else %}
      Um, well, not stopped, blocked or started!
    {% endif %}"

So everything within the {} just ignored and only the result included within the “”?

What is the order of precedence if two of the input_booleans were on at the same time?

The last seven characters of each input_boolean’s name represents its state. We can use that naming consistency to create a simple template.

- alias: 'Example 1'
  trigger:
    platform: state
    entity_id:
      - input_boolean.main_pump_stopped
      - input_boolean.main_pump_blocked
      - input_boolean.main_pump_running
    to: 'on'
  action:
    service: notify.notify
    data_template:
      title: "Koi pond..."
      message: "{{ trigger.object_id[-7:] }}"
1 Like

I did not enclose the template in double qoute characters. I used multi-line YAML instead.

Sorry I forgot the first part of the message. Try this:

data_template:
  title: "Koi pond..."
  message: >
    ...main pump is {% if is_state('input_boolean.main_pump_stopped', 'on') -%}
      stopped
    {% elif is_state('input_boolean.main_pump_blocked', 'on') -%}
      blocked
    {% elif is_state('input_boolean.main_pump_running', 'on') -%}
      started
    {% else -%}
      Um, well, not stopped, blocked or started!
    {% endif %}

The “order of precedence” is based on how the statement is constructed. First the expression after “if” is evaluated. If that’s true then “stopped” is the result. If not, then the expression after the first “elif” is evauated. If that’s true then “blocked” is the result. If not, … And if none of them evaluate to true, then the part under the “else” is the result.

Thank you. I’ve got that now.

I appreciate your time.

1 Like

I had, of course, forseen the possibility of your solution and deliberately chose 7 letter descriptors! :wink:

Brilliant solution, and well spotted. Thank you.

message: "{{ trigger.object_id.rsplit('_', 1)[-1] }}"

would also work (for any number of characters after the last underscore.)

EDIT:

Of course, you never showed what the automation’s trigger was, or even if the snippet came from an automation. In the future it helps to post the entire automation, script, configuration, whatever, that is relavant, for this exact reason. :slightly_smiling_face:

1 Like