# How do I use my project (code) in Node RED?

**URL:** <https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463>\
**Category:** General\
**Created:** [4 June 2022 19:23 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463 "2022-06-04T19:23:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![wachcio](https://avatars.discourse-cdn.com/v4/letter/w/a87d85/32.png) [@wachcio](https://discourse.nodered.org/u/wachcio)\
**Post date:** [4 June 2022 19:23 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463/1 "2022-06-04T19:23:19Z")

</div>

I want to use Node RED to control the Tasmota device that will turn on the heater. In Poland, where I come from, electricity is cheaper at certain hours and days and I want to use it. I wrote a project in Typescript ([GitHub - wachcio/cheap-electricity-tariff: The program shows whether there is a cheap electricity tariff now or at a given time and date.](https://github.com/wachcio/cheap-electricity-tariff)) and would like to use it with NodeRED. The function result _isCheapTariff = ( dateTime: Date | Dayjs = new Date(), tariff: Tariff = Tariff.Energa\_G12W, ): boolean_ would control the heater. What's the easiest way to do this?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [4 June 2022 21:14 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463/2 "2022-06-04T21:14:38Z")

</div>

Not sure what exactly that you are asking.

Are you wanting to find a way to enable a Boolean payload to control a tasmota device using node-RED?

If so, that can be done via MQTT.  
If not, can you describe in more detail exactly what help that you would like.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 June 2022 21:34 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463/3 "2022-06-04T21:34:32Z")

</div>

Assuming that you produce a valid package (the package.json doesn't appear to correctly define the main entry point at present since you need a js output not ts and you've defined the package as an ECMA module instead of a CommonJS module), you can simply require it and assign to a global variable in settings.js. Alternatively, you can add it to a function node.

---

<div class="post-metadata">

**Author:** ![wachcio](https://avatars.discourse-cdn.com/v4/letter/w/a87d85/32.png) [@wachcio](https://discourse.nodered.org/u/wachcio)\
**Post date:** [5 June 2022 22:46 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463/4 "2022-06-05T22:46:48Z")

</div>

I just have a problem with proper conversion to JS and importing in NodeRED

---

<div class="post-metadata">

**Author:** ![9toejack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/9toejack/32/56868_2.png) [@9toejack](https://discourse.nodered.org/u/9toejack)\
**Post date:** [7 June 2022 06:33 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463/5 "2022-06-07T06:33:52Z")

</div>

so you want to change the heating depending on whether or not the electricity is cheap or expensive, rather then turn on when at a specific temp, or are you trying to control your heat as well.  
I do not use boolean I use on or off in my mqtt message to my tasmota device.  
This is for cooling a fermenter , but hopefully gives you an idea.

```auto
[{"id":"06ea2d4d1d36cae0","type":"switch","z":"f6858fe416b029b9","name":"","property":"target","propertyType":"flow","rules":[{"t":"gte","v":"ambient","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":974.9296836853027,"y":182.63451766967773,"wires":[["f59a64501d3e4995","b757ad2e9a21172d"],["dca9643d538d9c10","efbd0245b8dd1ad5"]]},{"id":"b757ad2e9a21172d","type":"debug","z":"f6858fe416b029b9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1130.926254272461,"y":148.74212837219238,"wires":[]},{"id":"f59a64501d3e4995","type":"change","z":"f6858fe416b029b9","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1152.926368713379,"y":183.62065601348877,"wires":[["8e8ffc3b58648b48","be56299a7516053c"]]},{"id":"dca9643d538d9c10","type":"change","z":"f6858fe416b029b9","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1153.926368713379,"y":220.6223258972168,"wires":[["e4614f962524aec5","be56299a7516053c"]]},{"id":"efbd0245b8dd1ad5","type":"debug","z":"f6858fe416b029b9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1121.92626953125,"y":252.74561882019043,"wires":[]},{"id":"8e8ffc3b58648b48","type":"debug","z":"f6858fe416b029b9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1321.9333801269531,"y":152.74212837219238,"wires":[]},{"id":"be56299a7516053c","type":"change","z":"f6858fe416b029b9","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/Wferm/POWER","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1333.0086555480957,"y":199.00171947479248,"wires":[["0cf85aaa0a7b0507"]]},{"id":"e4614f962524aec5","type":"debug","z":"f6858fe416b029b9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1323.9279975891113,"y":256.74909019470215,"wires":[]},{"id":"0cf85aaa0a7b0507","type":"mqtt out","z":"f6858fe416b029b9","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"31a99116.50a74e","x":1481.0086784362793,"y":199.0017604827881,"wires":[]},{"id":"31a99116.50a74e","type":"mqtt-broker","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![9toejack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/9toejack/32/56868_2.png) [@9toejack](https://discourse.nodered.org/u/9toejack)\
**Post date:** [7 June 2022 06:49 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463/6 "2022-06-07T06:49:09Z")

</div>

the switch node in that example I had set guidlines. in the switch node you may need to enter your parameters, how ever your are needing something with dates and times as well.

 ![switchnodeexampletoday](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2ffcb82a729be424195f781bcc44f259ba8b1ec5.png)

also check this out for heating.

> **[node-red-contrib-ramp-thermostat](https://flows.nodered.org/node/node-red-contrib-ramp-thermostat)**
>
> A Node-RED node that emulates a programmable thermostat.

communication with a tasmota device is done with mqtt. Also if the status of the electricity is on a web page looking into html nodes and see if you can just have it queried in and convert data into the information you need.  
If you have a flow started and are stuck,  
share the flow with us so we can try to help you out.  
make sure you follow these guidelines when sharing a flow.

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![9toejack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/9toejack/32/56868_2.png) [@9toejack](https://discourse.nodered.org/u/9toejack)\
**Post date:** [7 June 2022 06:56 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463/7 "2022-06-07T06:56:30Z")

</div>

have you looked here?  
[http://www.howtocreate.co.uk/tutorials/jsexamples/syntax/prepareInline.html](http://www.howtocreate.co.uk/tutorials/jsexamples/syntax/prepareInline.html)  
this is how to covert regular text into Javascript

---

<div class="post-metadata">

**Author:** ![wachcio](https://avatars.discourse-cdn.com/v4/letter/w/a87d85/32.png) [@wachcio](https://discourse.nodered.org/u/wachcio)\
**Post date:** [7 June 2022 15:09 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463/8 "2022-06-07T15:09:12Z")

</div>

I have established MQTT communication with my Tasmota device (relay device).  
Now I'm trying to make an NPM bundle out of my TypeScript project with [GitHub - tomchen/example-typescript-package: Example TypeScript Package ready to be published on npm & Tutorial / Instruction / Workflow for 2021](https://github.com/tomchen/example-typescript-package) so I can add it to NodeRED. It is supposed to return 'true' or 'false' and then using MQTT I want to send it to the device

---

<div class="post-metadata">

**Author:** ![wachcio](https://avatars.discourse-cdn.com/v4/letter/w/a87d85/32.png) [@wachcio](https://discourse.nodered.org/u/wachcio)\
**Post date:** [8 June 2022 21:32 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463/9 "2022-06-08T21:32:05Z")

</div>

I was able to build the NPM library.  
I set it up in the "functionGlobalContext" section of the NodeRED setup.  
Then using "global.get" I used it in a function and send the result (boolean) via MQTT to the Tasmot device.  
Everything works ok 🙂

---

<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:** [7 August 2022 21:32 UTC](https://discourse.nodered.org/t/how-do-i-use-my-project-code-in-node-red/63463/10 "2022-08-07T21:32:21Z")

</div>

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