Problem with input_text json formatting

Hi,

I have a flow that sets some dictionary values into msg, and I want to pass all the dictionary into an input_text helper as json string.
I first tested such a sample flow, where the input text is node red object. This works fine…
image
The node config is trivially simple:

But, I can’t use it this way, because I have multiple input_text helpers and I select them dynamically based on one of the payload values. I have regular input_text helpers created in HA.
This is part of the real flow:


With such settings, instead of this json string:
{"presence":true,"icon":"mdi:motion-sensor","text":"Biuro"}
I get instead:
{"presence":true,"icon":"mdi:motion-sensor","text":"Biuro"}

Effectively the whole json formatted status is passed as single string value…

If I change data formula to:
{"value" : {{{status}}} }
I get this error message:
"HomeAssistantError: value should be a string for dictionary value @ data['value']"

I’ve already spent hours googling and trying different options, but no help.
The only workaround I’ve found is manually formatting the json string inside the Data field, but that is quite annoying…
Any ideas how to pass an already properly formatted json string to be recognized as json?

Are you using the text entity just to persist data across restarts? If so, you can use Node-RED’s file-system context to store any type of data without needing to convert it to a string.

https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system

Change the type from JSON to JSONata and enter it as:

{ "value" : status }

Templating within the JSON is causing the escaping.

No, I want to store a json string into input_text helper, so I can refer to it later in my dashboard, using mushroom template cards:

Not sure what you mean. I can either select output data as “JSON” or “Expression”.
Is there some add-on needed for what you suggested?

image

J expression = jsonata. The terms are interchangeable. When using json it reads {{ }} as a variable this is why you would use jsonata. try

[{"id":"f2caab7de877430f","type":"inject","z":"0a325c35fc29f44e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{% if is_state(\"sun.sun\", \"below_horizon\") -%}","payloadType":"str","x":370,"y":4040,"wires":[["a789899abe5abfb7"]]},{"id":"a789899abe5abfb7","type":"api-call-service","z":"0a325c35fc29f44e","name":"","server":"6b1110b5.183a4","version":7,"debugenabled":false,"action":"input_text.set_value","floorId":[],"areaId":[],"deviceId":[],"entityId":["input_text.test"],"labelId":[],"data":"{\"value\":payload}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","blockInputOverrides":true,"domain":"input_text","service":"set_value","x":600,"y":4040,"wires":[[]]},{"id":"6b1110b5.183a4","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"id","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

You’d be better off storing it in an attribute, and not as a string. The state can only be 256 characters long, whereas attributes can be any length. For example, if you sent it to an MQTT sensor and loaded the JSON into an attribute, then you could get it out with:

{{ state_attr('sensor.my_mqtt_sensor', 'my_json').presence) }}

How to store such attribute from NodeRed?

You could use the hass-node-red custom integration in HA, which allows you to use the sensor node in NR to create any sensor you like with attributes of your choosing.

However my preference is to use MQTT (partly because I’ve been using it from before the above existed, so I’ve only ever toyed with sensor). Then if you create an MQTT sensor in HA along the lines of:

sensor:
  - name: MB Test MQTT
    unique_id: mb_test_mqtt
    state_topic: "mb/mbtest"
    value_template: "{{ value_json.state }}"
    json_attributes_topic: "mb/mbtest"
    force_update: true

And then use an mqtt out node to publish whatever JSON you want (to “mb/test” in my example), then all of the attributes in the JSON will be loaded as attributes on the MQTT sensor (except ‘state’ in my example above, which loads as the HA entity state).

Thanks for hints, I’ll have to experiment with this when I have some more time.
For now the 256 characters limitation doesn’t hurt me and it works for me as above, but will keep this in mind for the future.