# Comparing data from API

**URL:** <https://discourse.nodered.org/t/comparing-data-from-api/68623>\
**Category:** General\
**Created:** [6 October 2022 06:33 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623 "2022-10-06T06:33:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sureshkumar.S](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@Sureshkumar.S](https://discourse.nodered.org/u/Sureshkumar.S)\
**Post date:** [6 October 2022 06:33 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/1 "2022-10-06T06:33:12Z")

</div>

I want compare get where it's a day time or night time. I'm getting sunrise and sunset time from the weather API. I don't how to compare the data using switch node.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [6 October 2022 07:07 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/2 "2022-10-06T07:07:52Z")

</div>

Format the sunrise and sunset times to UTC (milliseconds since 1 January 1970) then get the current time ([JavaScript Date getTime() Method](https://www.w3schools.com/jsref/jsref_gettime.asp)) and use a `switch` node or `function` node to do the comparison

---

<div class="post-metadata">

**Author:** ![Sureshkumar.S](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@Sureshkumar.S](https://discourse.nodered.org/u/Sureshkumar.S)\
**Post date:** [6 October 2022 07:51 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/3 "2022-10-06T07:51:43Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e5ca0aa3c51081c9f07678a89b8564df93a5dba6.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/eff2d84adb48de2196ed3b9e0d9694185ed5302c.png)

---

<div class="post-metadata">

**Author:** ![Sureshkumar.S](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@Sureshkumar.S](https://discourse.nodered.org/u/Sureshkumar.S)\
**Post date:** [6 October 2022 07:54 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/4 "2022-10-06T07:54:37Z")

</div>

msg.data, msg.sunrise and msg.sunset was getting correct but whenever i run this it giving otherwise output only

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [6 October 2022 08:15 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/5 "2022-10-06T08:15:15Z")

</div>

You will have two msgs entering the comparing node BUT they will not arrive at the same time. You would need to use a `join` node before the 'Comparing' node to get them to arrive at the same time.

If you move the 'Set msg.data right after the 'Weather data' node, you could have the current time in the same msg as the sunrise or sunset time.

---

<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:** [6 October 2022 08:20 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/6 "2022-10-06T08:20:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sureshkumar.S](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@Sureshkumar.S](https://discourse.nodered.org/u/Sureshkumar.S)\
**Post date:** [6 October 2022 11:19 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/7 "2022-10-06T11:19:58Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dcb532e62ce8ed41b1f48f40e380991e528ec62c.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f39009a646056912e05563c433e941f83bf3076.png)  
I'm getting output but the thing is, I used combining output as string is there any other option to get output instead of using string

---

<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:** [6 October 2022 11:31 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/8 "2022-10-06T11:31:04Z")

</div>

The join node is not what you want here.

Put all nodes in series and all properties into individual propertys (e.g. inject `msg.now = timestamp`, msg.sunrise = msg.payload.xxxx, msg.sunset = msg.payload.yyyy `

then your compare can do `between msg.sunset and msg.sunrise` etc.

If you are simply trying to figure out what point of the day it is, then I recommend node-red-contrib-cron-plus...

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

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

Demo flow...

```auto
[{"id":"a6d46dff.aca4d","type":"inject","z":"c876e57260edd2ab","name":"describe solar","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"command\":\"describe\",\"location\":\"35.45743499930747 139.63778346776962\",\"expressionType\":\"solar\",\"solarType\":\"selected\",\"solarEvents\":\"sunrise,sunset\",\"timeZone\":\"Europe/London\",\"offset\":60,\"info\":\"Get sunrise/sunset times and status at yokohama intercontinental (35.45743499930747 139.63778346776962)\"}]","payloadType":"json","x":730,"y":1660,"wires":[["4eb68abd.a39914"]]},{"id":"4eb68abd.a39914","type":"cronplus","z":"c876e57260edd2ab","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output2","defaultLocation":"","defaultLocationType":"default","outputs":2,"options":[],"x":1020,"y":1660,"wires":[[],["2d35c203.e28a0e"]]},{"id":"2d35c203.e28a0e","type":"debug","z":"c876e57260edd2ab","name":"Command result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1220,"y":1660,"wires":[]},{"id":"c4fd78d7.203dd8","type":"comment","z":"c876e57260edd2ab","name":"Simple demo of using the describe command to get solar state","info":"","x":860,"y":1620,"wires":[]},{"id":"a5ef21003ae53641","type":"cronplus","z":"c876e57260edd2ab","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output2","defaultLocation":"","defaultLocationType":"default","outputs":2,"options":[{"name":"ukne","topic":"uk/ne/solar_events","payloadType":"default","payload":"","expressionType":"solar","expression":"0 * * * * * *","location":"54.9990878447323 -1.4176440238952634","offset":"0","solarType":"selected","solarEvents":"sunrise,sunset"}],"x":960,"y":1780,"wires":[[],["3e7e072d1929f45d"]]},{"id":"3e7e072d1929f45d","type":"debug","z":"c876e57260edd2ab","name":"Command result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1220,"y":1780,"wires":[]},{"id":"fe54c07f1471b999","type":"comment","z":"c876e57260edd2ab","name":"Simple demo asking for status of a fixed solar schedule","info":"","x":840,"y":1740,"wires":[]},{"id":"f1c6f23ee6f27315","type":"inject","z":"c876e57260edd2ab","name":"status of ukne","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"status","payload":"ukne","payloadType":"str","x":730,"y":1780,"wires":[["a5ef21003ae53641"]]}]

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [7 October 2022 04:53 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/9 "2022-10-07T04:53:17Z")

</div>

> [@Steve-Mcl](#):
>
> Put all nodes in series

ie: (and remove the join node)

 ![SCR-20221007-9jx](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/39c142b242475fc7516b0849bcdf919626333698.png)

---

<div class="post-metadata">

**Author:** ![Sureshkumar.S](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@Sureshkumar.S](https://discourse.nodered.org/u/Sureshkumar.S)\
**Post date:** [7 October 2022 05:49 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/10 "2022-10-07T05:49:22Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/6/e60c3047ca8c732fefdb83f95009d2c53c42720c.png)  
Hurrah!!! It's worked thanks everyone

---

<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:** [21 October 2022 05:49 UTC](https://discourse.nodered.org/t/comparing-data-from-api/68623/11 "2022-10-21T05:49:53Z")

</div>

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