# ESPHOME yaml: How could i pass an int received by mqtt.payload to use as a delay time

**URL:** https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482
**Category:** Hardware
**Created:** [19 October 2020 08:40 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482 "2020-10-19T08:40:55Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)
#### Post date: [19 October 2020 08:40 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/1 "2020-10-19T08:40:56Z")

</div>

Posted these question on several forum's, and decided to post it here too...

I am building a pump system, and everything is done in Node-Red . So i have calculated time for which i want to run the pump for( to output desired liquid amount). So i decided to send the delay to the esp32 (because i have +20 pumps and the mqtt protocol can't gets delayed and it is not reliable for this type of task) but i can't figure out how to call the delay with the received message inside on\_message ... Does anyone have an idea?  
Administrators if you think this post is off topic, and not for this forum, please delete it.

---

<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: [19 October 2020 09:21 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/2 "2020-10-19T09:21:07Z")

</div>

So hang on - explain a little more - are you going to have 20 separate systems that will be running 20 different pumps with variable times ?

What is the interface that you will be using to pass the delay time for each system ??

What have you configured device wise in ESPHome ?

I use ESPHome but have not really looked at the code to see if there is something like a countdown system - not really sure it is going to be the best solution overall

Craig

---

<div class="post-metadata">

### Author: ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)
#### Post date: [19 October 2020 09:49 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/3 "2020-10-19T09:49:25Z")

</div>

I have one esp32 configured and connected to my mqtt broker, and connected 20 relays to it.  
So far i added the switch option with name and id and pin number for each pump(pump1,pump2...) (When using MQTT with command topics from Iog the relays are switching perfect on/off) plan to use MQTT to send the interval from my node-red to every pump topic command so for example:  
Topic:pump\_system/pump1  
Payload: 5000 (ms)  
The esp itself should turn on the relay, wait for X time(in this case 5000) and then switch off.  
I am at my job and can't access the HA to send the code...

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 October 2020 13:00 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/4 "2020-10-19T13:00:33Z")

</div>

Based on the documentation it looks like you could be able to do this with a lambda function triggered on MQTT message receive. See the examples at this section [https://esphome.io/components/mqtt.html#on-message-trigger](https://esphome.io/components/mqtt.html#on-message-trigger) and the `on_json_message` example that follows it.

---

<div class="post-metadata">

### Author: ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)
#### Post date: [19 October 2020 16:06 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/5 "2020-10-19T16:06:34Z")

</div>

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

But never mind that, even when i paste the sample from the link i get the following error...

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

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 October 2020 16:39 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/6 "2020-10-19T16:39:34Z")

</div>

I haven't played around with ESPHome much at all but did you try if it still compiles? Maybe the configuration syntax is not 100% valid YAML?

---

<div class="post-metadata">

### Author: ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)
#### Post date: [19 October 2020 16:48 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/7 "2020-10-19T16:48:23Z")

</div>

Well i tried in the browser... let me check with the pip package...

Still the same error:

> ←[32mINFO Reading configuration...←[0m  
> ←[31mERROR Error while reading config: mapping values are not allowed here  
> in "esphome.yml", line 28, column 11←[0m

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 October 2020 16:54 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/8 "2020-10-19T16:54:26Z")

</div>

Strange. Maybe they've got a documentation bug then.

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 October 2020 16:57 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/9 "2020-10-19T16:57:54Z")

</div>

If you look at line 41 the syntax highlighting breaks. Try changing the outer `"`'s to single quotes?

---

<div class="post-metadata">

### Author: ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)
#### Post date: [19 October 2020 16:59 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/10 "2020-10-19T16:59:50Z")

</div>

Still the same error...

I will give up on MQTT , and go with the ESP-HOME API , and try to delay it on the node-red side, if not then i am better off using Arduino IDE to program it (had at least some experience in the past with it) and i think i will find more documentation for it...

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 October 2020 17:01 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/11 "2020-10-19T17:01:44Z")

</div>

At least it'll be a lot more flexible.

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 October 2020 18:29 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/12 "2020-10-19T18:29:54Z")

</div>

One more idea. Did you try with only a one line lambda (if the issue is with the multiline ones)? Maybe you could use a global variable for the delay, update it from a separate topic and then use the variable within the normal switch trigger (from another MQTT topic).

TBH I have no idea if the global variable can be used like this but somehow the automation template / lambda system seemed very versatile.

---

<div class="post-metadata">

### Author: ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)
#### Post date: [19 October 2020 20:36 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/13 "2020-10-19T20:36:20Z")

</div>

Well passed to use the rest api for the node-red to HA communication. Its really fast , the ping is about 5ms and its working okay i belive. Have to finish this project but if i ever stumble upon this one more time I would have to do it the right way 😁

---

<div class="post-metadata">

### Author: ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)
#### Post date: [20 October 2020 20:56 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/14 "2020-10-20T20:56:45Z")

</div>

I am really not sure how can i program that. I really dont have experience with it , and too busy to learn the esphomeyaml...

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [20 October 2020 21:58 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/15 "2020-10-20T21:58:56Z")

</div>

Yep, aren't we all. 🙂

I still don't quite understand why do you consider MQTT less reliable than a REST API (if QoS is just properly set)?

---

<div class="post-metadata">

### Author: ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)
#### Post date: [21 October 2020 03:59 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/16 "2020-10-21T03:59:57Z")

</div>

Mostly latency between the hassio and esp32

---

<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: [20 December 2020 04:00 UTC](https://discourse.nodered.org/t/esphome-yaml-how-could-i-pass-an-int-received-by-mqtt-payload-to-use-as-a-delay-time/34482/17 "2020-12-20T04:00:00Z")

</div>

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