Input_text can't handle over 255 characters

I know the state entity was modified to be limited to 255 characters and the work around was to use the input_text field.

But it appears that input_text is also limited.

Also you can set the max allowed; and if you set something over 255 no error is emitted but it certainly does fail when you try to populate it over 255 characters.

So how can I create a big-ass-string without custom components?

I asked something similar recently and the answer I got was pretty much that you can’t.

I’m hoping someone else now says that answer was wrong :slight_smile:

Correct, you can’t. The state of any entity is limited to 255 characters. I believe that attributes aren’t limited like that though.

Ahhhhh, that is the magic hint. I assumed input_text was handled like that.

Any kind soul point me to an example of setting an attribute and text into that?

1 Like

You can use the custom-card state_card-value_only.html, and have massive cards…
check and see my adaptation of it, to provide some more coloring options

You can create a python script. What are you trying to do with the big-ass-string? I may be able to help with a solution, but there are many. A direction might steer towards a solution.

Hi,

I use it typically to build some alerts; like what lights/switches/fans are on; and it can go over 255.

It used to work but now with the 255 limit I get exceptions at times when house has lots of lights, fans, etc, on.

Here is an example: (and as you are the jinja wizard please feel free to suggest other improvements!)

 - platform: template
    sensors:
      lights_on:
        value_template: >
          {%- macro get_lights_on() -%}
            {%- for group in states.light|groupby('state') -%}
              {%- for entity in group.list -%}
                {%- if  entity.state == 'on' -%}
                  {{ entity.entity_id }}{{ ' ' }}
                {%- endif -%}
              {%- endfor -%}
            {%- endfor -%}
          {%- endmacro -%}
          {{ get_lights_on()|trim|replace(' ', ',') }}

  - platform: template
    sensors:
      light_check:
        value_template: >
          {%- macro friendly_list(list) -%}
            {%- set temp = list|replace('_',' ')|replace(' ,',',')|replace(',',', ')|trim|title -%}
            {%- if list.split(',')|length > 1 -%}
              {%- set temp = temp+'^' -%}
              {%- set lastitem = temp.split(',')|last -%}
              {{ temp|replace(lastitem, ' and' + lastitem)|replace('^','') }}
            {%- else -%}
              {{ temp }}
            {%- endif -%}
          {%- endmacro -%}
          {%- macro light_check() -%}
            {%- if states.sensor.lights_on.state|length != 0 -%}
              {%- set lights_on_count = states.sensor.lights_on.state.split(',')|length -%}
              {%- set lights_on_friendly = friendly_list(states.sensor.lights_on.state|replace('light.','')|replace('switch.','')|replace('light','')|replace('level','')) -%}
              {%- if lights_on_count == 0 -%}
                No lights on.
              {%- elif lights_on_count == 1 -%}
                The {{ lights_on_friendly }} light is on.
              {%- else -%}
                There are {{ lights_on_count }} lights on right now.
                The {{ lights_on_friendly }} lights are on.
              {%- endif -%}
            {%- endif -%}
          {%- endmacro -%}
          {{ light_check() }}

you might be able to use this, not this has the custom-card commented out. depending on the amount of your lights, this might not need it.
Note 2, this creates a badge with a text.

    ##########################################################################################
# Lights:
# Badges images: /config/www/lights
# https://community.home-assistant.io/u/Mariusthvdb/
##########################################################################################
lightEntities = 'group.all_lights_only'
lights_on = []

for entity_id in hass.states.get(lightEntities).attributes['entity_id']:
    state = hass.states.get(entity_id)
    dt = state.last_changed + datetime.timedelta(hours=2)
    time = '%02d:%02d' % (dt.hour, dt.minute)

    if hass.states.get(entity_id).state is 'on': #and entity_id not in excluded
        lights_on.append(hass.states.get(entity_id).attributes['friendly_name'])

if len(lights_on) > 0:
    picture = '/local/lights/hue_pl.png'
    message = ', '.join(lights_on)
    sensor_message = 'Lights on: ' + message
    lights_desc = '=- Lights on: {} -- {} since {}'.format(lights_on, message,time)
    uom = 'Lights'

    if len(lights_on) == 1:
        uom = 'Light'
else:
    picture = '/local/lights/bulboff.png'
    message= ''
    sensor_message= 'No lights on'
    uom = 'Lights'

sensor_lights_desc = '{}'.format(sensor_message)

hass.states.set('sensor.lampjes_badge', len(lights_on), {
#     'custom_ui_state_card': 'state-card-value_only',
    'text': sensor_message,
    'unit_of_measurement': uom,
    'friendly_name': time,
    'entity_picture': picture
     })

You can stick it in a MQTT topic.

The length of the actual topic string is at most 65536 bytes. This is a limit imposed by the mqtt spec, you can’t change it. It is also worth noting that the topic is encoded with utf-8, so you may have less than 65536 characters available.

The payload of the message is limited to 268,435,456 byte

4 Likes

First off, you can simplfy your first lights_on template like this:

  - platform: template
    sensors:
      lights_on:
        value_template: >
          {{ states.light | selectattr('state','eq','on') | map(attribute='entity_id') | join(',') }}

With such a short template, you can use it in notifications and it won’t be 9000 lines of code. So you can probably drop the template sensor. Also, it looks like your second template uses this. Which is the only reason you have it. So you could just delete this all together and use the following optimized template:

  - platform: template
    sensors:
      light_check:
        value_template: >
          {% set lights_on = states.light | selectattr('state','eq','on') | map(attribute='name') | list %}
          {% if lights_on | length == 0 %}
            No lights on.
          {% elif lights_on | length == 1 %}
            The {{ lights_on[0] }} light is on.
          {% elif lights_on | length == 2 %}
            The {{ lights_on[0] }} and {{ lights_on[1] }} lights are on.
          {% else %}
            The {{ lights_on[:-1] | join(', ') }}, and {{ lights_on[-1] }} lights are on.
          {% endif %}

With these short templates, you can place the logic directly into your notifications without crowding up your yaml (instead of using the template, therefore being limited to the 255 characters).

1 Like

this is very nice indeed!
you’d need a skip-list to prevent automatically created light groups from showing up, (of which tradfri and hue make a few)(reason I use lightEntities = 'group.all_lights_only' in the python script and other automations), and some further lineup embellishment, but indeed way cool. thx!
copied to my templating cook-book

1 Like

I don’t think I was clear enough:

If you place this into a script, then call the script when you need to notify someone, you won’t need the value template:

script:
  light_notification:
    sequence:
      # This is Home Assistant Script Syntax
      - service_template: {{ notify_entity_id }}
        data_template:
          message: >
            {% set lights_on = states.light | selectattr('state','eq','on') | map(attribute='name') | list %}
            {% if lights_on | length == 0 %}
              No lights on.
            {% elif lights_on | length == 1 %}
              The {{ lights_on[0] }} light is on.
            {% elif lights_on | length == 2 %}
              The {{ lights_on[0] }} and {{ lights_on[1] }} lights are on.
            {% else %}
              The {{ lights_on[:-1] | join(', ') }}, and {{ lights_on[-1] }} lights are on.
            {% endif %}

Then call the script with the following service:

   - service: script.light_notification
     data_template:
       notify_entity_id: notify.myiphone

It’s like having a template, you just can’t see it in the interface.

The first response was enough, I was busy re-doing a bunch of things that your suggestion made obvious could be drastically improved!!

Thanks so much for the help – really appreciated.:+1:

1 Like

yep.
could even do with:

        {% set lights_on = states.light | selectattr('state','eq','on') | map(attribute='name') | list %}
        {% if lights_on | length == 0 %}
          No lights on.
        {% elif lights_on | length == 1 %}
          The {{ lights_on[0] }} light is on.
        {% else %}
          The {{ lights_on[:-1] | join(', ') }}, and {{ lights_on[-1] }} lights are on.
        {% endif %}

btw, it needed this too:

  - service_template: >
      {{ notify_entity_id }}

or quotes of course, but I prefer the multiline notation

The max is explained here: