# Creating a sensor with an attribute in Home Assistant

**URL:** <https://discourse.nodered.org/t/creating-a-sensor-with-an-attribute-in-home-assistant/92770>\
**Category:** General\
**Tags:** home-assistant\
**Created:** [29 October 2024 12:54 UTC](https://discourse.nodered.org/t/creating-a-sensor-with-an-attribute-in-home-assistant/92770 "2024-10-29T12:54:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DIMMonchik](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dimmonchik/32/95914_2.png) [@DIMMonchik](https://discourse.nodered.org/u/DIMMonchik)\
**Post date:** [29 October 2024 12:54 UTC](https://discourse.nodered.org/t/creating-a-sensor-with-an-attribute-in-home-assistant/92770/1 "2024-10-29T12:54:07Z")

</div>

I need to create a sensor with an attribute containing a Mikrotik connection table in HA for further displaying the table using a flex-table-card. To do this, a function was written that sends data to the sensor node attribute 'clients' as a string

```auto
  - interface: '2G'
  rx_rate: '13Mbps-20MHz/1S'
  tx_rate: '14.4Mbps-20MHz/1S/SGI'
  uptime: '1w1d22h36m47s'
  last_activity: '280ms'
  signal_strength: '-65@5.5Mbps'
  signal_to_noise: '33'
  signal_strength_ch0: '-68'
  signal_strength_ch1: '-76'
  tx_ccq: '30'
  distance: '2'
  last_ip: '192.168.0.11'
  authentication_type: 'wpa2-psk'
  encryption: 'aes-ccm'
- interface: '2G'
  rx_rate: '54Mbps'
  tx_rate: '72.2Mbps-20MHz/1S/SGI'
  uptime: '1w1d22h36m44s'
  last_activity: '1s360ms'
  signal_strength: '-54@6Mbps'
  signal_to_noise: '44'
  signal_strength_ch0: '-58'
  signal_strength_ch1: '-58'
  tx_ccq: '87'
  distance: '1'
  last_ip: '192.168.0.39'
  authentication_type: 'wpa2-psk'
  encryption: 'aes-ccm'

```

But I can't figure out how to get rid of | in the attribute value

```auto
clients: |
  - interface: '2G'
    rx_rate: '13Mbps-20MHz/1S'
    tx_rate: '9Mbps'
    uptime: '1w1d22h35m47s'
    last_activity: '360ms'
    signal_strength: '-63@5.5Mbps'
    signal_to_noise: '37'
    signal_strength_ch0: '-67'
    signal_strength_ch1: '-75'
    tx_ccq: '32'
    distance: '2'
    last_ip: '192.168.0.11'
    authentication_type: 'wpa2-psk'
    encryption: 'aes-ccm'
  - interface: '2G'
    rx_rate: '54Mbps'
    tx_rate: '72.2Mbps-20MHz/1S/SGI'
    uptime: '1w1d22h35m44s'
    last_activity: '1s360ms'
    signal_strength: '-53@6Mbps'
    signal_to_noise: '47'
    signal_strength_ch0: '-57'
    signal_strength_ch1: '-58'
    tx_ccq: '96'
    distance: '1'
    last_ip: '192.168.0.39'
    authentication_type: 'wpa2-psk'
    encryption: 'aes-ccm'

```

Tell me, please, what am I doing wrong?

```auto
[{"id":"54c6e062bb19fa85","type":"group","z":"264869427f0f7cb8","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["96154c702dc64b8d","f0fe72fe67491246","e05582db968be14b","2c015140a31d62c5"],"x":1014,"y":1039,"w":852,"h":182},{"id":"96154c702dc64b8d","type":"inject","z":"264869427f0f7cb8","g":"54c6e062bb19fa85","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1150,"y":1120,"wires":[["f0fe72fe67491246"]]},{"id":"f0fe72fe67491246","type":"mikrotik","z":"264869427f0f7cb8","g":"54c6e062bb19fa85","device":"10e65d39218dd615","name":"Connected WiFi","action":"2","x":1340,"y":1120,"wires":[["2c015140a31d62c5"]]},{"id":"e05582db968be14b","type":"ha-sensor","z":"264869427f0f7cb8","g":"54c6e062bb19fa85","name":"Connected WiFi","entityConfig":"8476da6722585417","version":0,"state":"payload","stateType":"msg","attributes":[{"property":"clients","value":"clients","valueType":"msg"}],"inputOverride":"allow","outputProperties":[],"x":1760,"y":1080,"wires":[[]]},{"id":"2c015140a31d62c5","type":"function","z":"264869427f0f7cb8","g":"54c6e062bb19fa85","name":"Create Table","func":"let data = msg.payload; \nlet count = msg.payload.length; \ndata.forEach(item => {\n delete item.ap;\n delete item.wds;\n delete item.bridge;\n delete item.packets;\n delete item.bytes;\n delete item.frames;\n delete item['frame-bytes'];\n delete item['hw-frames'];\n delete item['tx-frames-timed-out'];\n delete item['p-throughput'];\n delete item['management-protection'];\n delete item['wmm-enabled'];\n delete item['tx-rate-set'];\n delete item['strength-at-rates'];\n delete item['802.1x-port-enabled'];\n delete item['group-encryption'];\n delete item['.id'];\n delete item['hw-frame-bytes'];\n});\n\nlet keys = Object.keys(data[0]);\n\nlet text = '';\n\ndata.forEach(item => {\n text += '-';\n keys.forEach((key, index) => {\n text += ` ${key.replace(/-/g, '_')}: '${item[key]}'\\n`;\n if (index < keys.length - 1) {\n text += ' ';\n }\n });\n //text += '\\n';\n});\n\nmsg.clients = text; \nmsg.payload = count;\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":1570,"y":1180,"wires":[["e05582db968be14b"]]},{"id":"10e65d39218dd615","type":"mikrotik-device","host":"*.*.*.*","port":"8728","username":"*","password":"*"},{"id":"8476da6722585417","type":"ha-entity-config","server":"31f480d3.0e3b28","deviceConfig":"","name":"Mikrotik Connected WiFi","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Mikrotik Connected WiFi"},{"property":"icon","value":""},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""}],"resend":false,"debugEnabled":false},{"id":"31f480d3.0e3b28","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":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 October 2024 13:39 UTC](https://discourse.nodered.org/t/creating-a-sensor-with-an-attribute-in-home-assistant/92770/2 "2024-10-29T13:39:34Z")

</div>

Can not run your flow as we do not have the output from your HA mikrotik node.

Saying that it looks like the issue is in HA's code, you may need to ask on HA forum.

---

<div class="post-metadata">

**Author:** ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)\
**Post date:** [30 October 2024 02:24 UTC](https://discourse.nodered.org/t/creating-a-sensor-with-an-attribute-in-home-assistant/92770/3 "2024-10-30T02:24:54Z")

</div>

The pipe | is yaml trying to preserve the new line entries. Rather than sending a string with new lines create an object with the key and value and push each one into an array. Here is an example how I send an array to get a list in yaml

```auto
let sensorData = [];

for (let i = 0; i < upcoming.length; i++) {
    const element = upcoming[i];
    const time = element.start;
    const title = element.heading;
    const event = element.summary;
    const links = element.location.split(',');
    const image = links[0];
    const site = links[1];
    const data = {
        "title": event,
        "airdate": time,
        "release": title,
        "poster": image,
        "fanart": image,
        "deep_link": site
    };

    sensorData.push(data);

};

```

Result

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f0b182fa4bdf33af6923e40e79f6d75c44cc4b72.png)

---

<div class="post-metadata">

**Author:** ![DIMMonchik](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dimmonchik/32/95914_2.png) [@DIMMonchik](https://discourse.nodered.org/u/DIMMonchik)\
**Post date:** [30 October 2024 07:50 UTC](https://discourse.nodered.org/t/creating-a-sensor-with-an-attribute-in-home-assistant/92770/4 "2024-10-30T07:50:37Z")

</div>

Thank you very much! You've been very helpful!!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 January 2025 07:50 UTC](https://discourse.nodered.org/t/creating-a-sensor-with-an-attribute-in-home-assistant/92770/5 "2025-01-28T07:50:57Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
