diff --git a/homeassistant/helpers/trigger_template_entity.py b/homeassistant/helpers/trigger_template_entity.py index fff25568222..24e66a30c45 100644 --- a/homeassistant/helpers/trigger_template_entity.py +++ b/homeassistant/helpers/trigger_template_entity.py @@ -23,6 +23,7 @@ from homeassistant.const import ( CONF_NAME, CONF_UNIQUE_ID, CONF_UNIT_OF_MEASUREMENT, + MAX_LENGTH_STATE_STATE, ) from homeassistant.core import HomeAssistant, State, callback from homeassistant.exceptions import TemplateError @@ -238,10 +239,12 @@ class ManualTriggerEntity(TriggerBaseEntity): attr.update(state_attributes) if extra_state_attributes := self.extra_state_attributes: attr.update(extra_state_attributes) - + entity_state = str(self.state) + if len(entity_state) > MAX_LENGTH_STATE_STATE: + entity_state = entity_state[:MAX_LENGTH_STATE_STATE] state = State( self.entity_id, - str(value), + str(entity_state), attr, now, now, diff --git a/tests/components/command_line/test_switch.py b/tests/components/command_line/test_switch.py index 549e729892c..ed8032d7789 100644 --- a/tests/components/command_line/test_switch.py +++ b/tests/components/command_line/test_switch.py @@ -564,7 +564,7 @@ async def test_templating(hass: HomeAssistant) -> None: "command_off": f"echo 0 > {path}", "value_template": '{{ value=="1" }}', "icon": ( - '{% if states("switch.test2")=="on" %} mdi:on {% else %} mdi:off {% endif %}' + '{% if states("switch.test")=="on" %} mdi:on {% else %} mdi:off {% endif %}' ), "name": "Test2", },