Template sensor exceeds 256 chars

Hello,

to send me a summarized view of low battery devices or flowers, which needs water/soil I’m using that template:

    {%- set threshold = 300 -%}
    {%- for item in states['sensor'] if (item.state | int < threshold and item.state|int != 'unknown' and item.entity_id.endswith('conductivity')) -%}
    {{ item.attributes.friendly_name }}: {{item.state}}{{item.attributes.unit_of_measurement }} {%- if not loop.last %}, {% endif -%}
    {%- endfor -%}

Has anybody an idea how to avoid the error if the state exceeds 256 chars? Any other approach for that?

Why are you putting all the info into 1 string? Thats going to be huge!

Because I want to send one notification for all flowers once a day - Which other approach should I choose?

They way you have it implemented, there is no way to check the length of the string before reporting it.

You may want to get AppDaemon running and use python to handle this. You’d have more control over the strings you send out.

You could do one of the following as well:

  • shorten your fiendly names
  • remove your unit_of_measurement.
  • remove white space after colon and after comma.

Personally, i swapped all my automations to app daemon because I couldn’t do complicated functions with jinja because of the for loop restrictions, format restrictions, and general lack of functionality. Python is much better.

1 Like

Do you have an example how things like that:

…should look like in AppDaemon using Python:

low_battery:
  value_template: >
    {%- set threshold = 15 -%}
    {%- set domains = ['light', 'switch', 'sensor', 'binary_sensor', 'lock'] -%}
    {%- for domain in domains -%}
    {%- for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
        {{ item.attributes.friendly_name }}{%- if not loop.last %}, {% endif -%}
    {%- endfor -%}
    {%- endfor -%}

Don’t know if it works, but have you tried it without a template sensor?
Just templating the message you want to send?

 action:
    service: notify.pushbullet
    data_template: 
      title: 'New Home Assistant Release'
      target: 'YOUR_TARGET_HERE' #See Pushbullet component for usage
      message: "Home Assistant  {{ states.updater.updater.state }}  is now available."

Another solution would be to send a html mail via smtp.
This can be as long as you want.

EDIT: Works! Tried it with the example of the templates tool.

      - service: notify.pushsafer
        data_template: 
          message: >-
            {% for state in states.sensor -%}
              {%- if loop.first %}The {% elif loop.last %} and the {% else %}, the {% endif -%}
              {{ state.name | lower }} is {{state.state_with_unit}}
            {%- endfor %}
1 Like

Thanks a lot! You are right - There is no need to create a sensor, because it’s only needed for a notification.