If statement in automation message

Hi there, i cant handle this on my own :confused: Can someone point me to a resolution ?

Automation looks like this

- id: '1586175443173'
  alias: test
  description: ''
  trigger:
  - platform: template
    value_template: '{{ state_attr(''sensor.zielone'',''days'') == 1 }}'
  condition: []
  action:
  - data_template: >
      message: >
      {%- if {{ state_attr('sensor.zmieszane','days') == 1 }} -%}
      "Jutro wywóz śmieci {{ state_attr(''sensor.zmieszane'',''friendly_name'') }}"
      {%- elif {{ state_attr('sensor.zielone','days') == 1 }} -%}
      "Jutro wywóz śmieci {{ state_attr(''sensor.zielone'',''friendly_name'') }}"
      {%- elif {{ state_attr('sensor.segregowane','days') == 1 }} -%}
      "Jutro wywóz śmieci {{ state_attr(''sensor.segregowane'',''friendly_name'') }}"
      {%- else -%}
      "Dindu Nuffin"
      {%- endif -%}
    service: notify.smieci

And the error i get after configuration validation:

Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None. (See /share/CE_CACHEDEV1_DATA/.qpkg/Home-Assistant/CONFIG_DIR/configuration.yaml, line 7).

That is completely unrelated cuz configuration.yaml, line 7 is:

group: !include groups.yaml

That was never a problem and will wanish when i delete the automation.

you don’t need the > after data_template:

  data_template:
    message: >
1 Like

Now there is

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['action'][0]['data_template']['message']. Got None. (See /share/CE_CACHEDEV1_DATA/.qpkg/Home-Assistant/CONFIG_DIR/configuration.yaml, line 7).

Indent everything under message.

      message: >
        {%- if {{ state_attr('sensor.zmieszane','days') == 1 }} -%}
        "Jutro wywóz śmieci {{ state_attr(''sensor.zmieszane'',''friendly_name'') }}"
        {%- elif {{ state_attr('sensor.zielone','days') == 1 }} -%}
        "Jutro wywóz śmieci {{ state_attr(''sensor.zielone'',''friendly_name'') }}"
        {%- elif {{ state_attr('sensor.segregowane','days') == 1 }} -%}
        "Jutro wywóz śmieci {{ state_attr(''sensor.segregowane'',''friendly_name'') }}"
        {%- else -%}
        "Dindu Nuffin"
        {%- endif -%}

Tried that, still nothing changed

- id: '1586175443173'
  alias: test
  description: ''
  trigger:
  - platform: template
    value_template: '{{ state_attr(''sensor.zielone'',''days'') == 1 }}'
  condition: []
  action:
  - data_template:
      message: >
        {%- if {{ state_attr('sensor.zmieszane','days') == 1 }} -%}
        "Jutro wywóz śmieci {{ state_attr(''sensor.zmieszane'',''friendly_name'') }}"
        {%- elif {{ state_attr('sensor.zielone','days') == 1 }} -%}
        "Jutro wywóz śmieci {{ state_attr(''sensor.zielone'',''friendly_name'') }}"
        {%- elif {{ state_attr('sensor.segregowane','days') == 1 }} -%}
        "Jutro wywóz śmieci {{ state_attr(''sensor.segregowane'',''friendly_name'') }}"
        {%- else -%}
        "Dindu Nuffin"
        {%- endif -%}
    service: notify.smieci

It’s like one of those picture games “Find everything wrong with the picture”. Nothing looks wrong immediately, but looking closer, there’s so many things!

      message: >
        {% if state_attr('sensor.zmieszane','days') | int == 1 %}
         Jutro wywóz śmieci {{ state_attr('sensor.zmieszane','friendly_name') }}
        {% elif state_attr('sensor.zielone','days') | int == 1 %}
          Jutro wywóz śmieci {{ state_attr('sensor.zielone','friendly_name') }}
        {% elif state_attr('sensor.segregowane','days') | int == 1 %}
          Jutro wywóz śmieci {{ state_attr('sensor.segregowane','friendly_name') }}
        {% else %}
          Dindu Nuffin
        {% endif %}

Inside {% %} bracets, you’re already in a template, so don’t use {{ states(XXX) }} in there.

There’s no need to use quotes inside the multi line formatting block…unless you want those quotes to appear in the resulting message that is.

Need to convert your sensors to an int before comparing them to a number.

2 Likes

Configuration valid!

Let me quote some classics - Yo Ma Ha, Yo Ma So !

Thank you, im into coding like a hooker into bios so much to learn for me :wink:

Haha, I get that! Try, fail, ask questions, learn, repeat.

Good luck!