# Process Data from Home-Assistant Entity in function to template

**URL:** https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645
**Category:** General
**Tags:** function-node, home-assistant
**Created:** [17 January 2024 11:49 UTC](https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645 "2024-01-17T11:49:41Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Orcmast](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/orcmast/32/89574_2.png) [@Orcmast](https://discourse.nodered.org/u/Orcmast)
#### Post date: [17 January 2024 11:49 UTC](https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645/1 "2024-01-17T11:49:41Z")

</div>

Hello,  
i have an entity `sun.sun` (from Homeassistant) with attribute rising `data.attributes.next_rising` with payload `2024-01-18T07:26:56.471638+00:00` and want to process this raw data to `hh:mm` (with hh+1)  
Like:  
2024-01-18T **07:26** :56.471638+00:00 \> 08:26 (8 cause hh+1)

I have read many topics about processing messages but have no clue how to proceed. It seems like all messages between objects in nodered are passed (default) with JavaScript. Do i have to build a JavaScript function between entity and template? And how i can get the data from the function?  
I have a working solution with raw-data in Mustache-Template:

```auto
{     
"text": "{{data.attributes.next_rising}}",
}

```

Thank you in advice

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 January 2024 12:04 UTC](https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645/2 "2024-01-17T12:04:25Z")

</div>

> [@Orcmast](#):
>
> Do i have to build a JavaScript function between entity and template?

no

> [@Orcmast](#):
>
> i have an entity `sun.sun` (from Homeassistant) with attribute rising `data.attributes.next_rising` with payload `2024-01-18T07:26:56.471638+00:00` and want to process this raw data to `hh:mm` (with hh+1)

Use a change node and `$moment`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/dbaae7005d1054a674da0429cd2f931e09d7ce9d.png)

Demo flow _(use `CTRL+I` to import)_

```auto
[{"id":"0152ae8510f48dce","type":"inject","z":"dda2f44974393278","name":"fake entity data","props":[{"p":"payload.data.attributes.next_rising","v":"2024-01-18T07:26:56.471638+00:00","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":710,"y":480,"wires":[["d8cd9d547b2c2b31","fd06c7fdfcc5aac4"]]},{"id":"d8cd9d547b2c2b31","type":"change","z":"dda2f44974393278","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload.data.attributes.next_rising).format(\"HH:mm\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":480,"wires":[["3ec93364c36afcc8"]]},{"id":"3ec93364c36afcc8","type":"debug","z":"dda2f44974393278","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":960,"y":540,"wires":[]},{"id":"fd06c7fdfcc5aac4","type":"debug","z":"dda2f44974393278","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload.data.attributes.next_rising","statusType":"msg","x":900,"y":420,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Orcmast](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/orcmast/32/89574_2.png) [@Orcmast](https://discourse.nodered.org/u/Orcmast)
#### Post date: [17 January 2024 14:15 UTC](https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645/3 "2024-01-17T14:15:19Z")

</div>

Thank you, this is working really fine! 🙂  
Just had to change  
`$moment(payload.data.attributes.next_rising).format("HH:mm")` to `$moment(data.attributes.next_rising).format("HH:mm")`

Just found documentation here as well: [node-red-contrib-moment (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-moment) Its described there.

Another question:  
Which JS library can i use for the following things:

%-value example:` 10,00%` to `10%` and  
cutting after a few characters example:  
`Whatever-text-here` to `Whatever` ?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 January 2024 14:35 UTC](https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645/4 "2024-01-17T14:35:43Z")

</div>

> [@Orcmast](#):
>
> %-value example:` 10,00%` to `10%`

Show us the source data

_(for example, if it is already a number, then string processing may be unnecessary)_

also, explain what you wish to do with the data.

_(For example, if it is to be displayed on a dashboard, the answer might be different)_

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [18 January 2024 04:49 UTC](https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645/5 "2024-01-18T04:49:56Z")

</div>

Steve-Mcl has you covered on this - but if it does need changing then look at the change node and jsonata within it to reformat

Craig

---

<div class="post-metadata">

### Author: ![Orcmast](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/orcmast/32/89574_2.png) [@Orcmast](https://discourse.nodered.org/u/Orcmast)
#### Post date: [18 January 2024 16:35 UTC](https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645/6 "2024-01-18T16:35:06Z")

</div>

Mhh Home-Assistant gave me (for exampl)e for Speedtest the following entity-value: "100.10 Mbit/s" but i can see that NodeRed is only recognizing the 100.10. Same with my mouse-entity. From Homeassistant its for example 30.00% but in Nodered 30. Nothing to fix so 😃  
Thank you guys

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [18 January 2024 16:59 UTC](https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645/7 "2024-01-18T16:59:21Z")

</div>

Attach a debug mode showing what is coming from ha and paste a screen shot of the debug output here.

Or do you mean all is well?

---

<div class="post-metadata">

### Author: ![Orcmast](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/orcmast/32/89574_2.png) [@Orcmast](https://discourse.nodered.org/u/Orcmast)
#### Post date: [18 January 2024 17:48 UTC](https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645/8 "2024-01-18T17:48:40Z")

</div>

Yes all is well. 🙂

---

<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: [1 February 2024 17:48 UTC](https://discourse.nodered.org/t/process-data-from-home-assistant-entity-in-function-to-template/84645/9 "2024-02-01T17:48:50Z")

</div>

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