# Best way to send to mqtt client the state of my sensor

**URL:** <https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943>\
**Category:** General\
**Created:** [27 January 2025 21:14 UTC](https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943 "2025-01-27T21:14:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yelkenli](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@yelkenli](https://discourse.nodered.org/u/yelkenli)\
**Post date:** [27 January 2025 21:14 UTC](https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943/1 "2025-01-27T21:14:56Z")

</div>

I have some GPIO sensors that node-red manages. Node-red sends via mqtt to another system that uses the sensor data.

I would like to send one mqtt message when my node-red system starts up with the state of my sensor. And maybe one when the other system starts up, though I think use of the retain setting would be sufficient for that.

I was thinking of a node that passes a message once and only once. This would occur on start-up as my GPIO nodes will inject the pin state when starting.

Any thoughts on how to achieve this?

---

<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:** [27 January 2025 21:41 UTC](https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943/2 "2025-01-27T21:41:15Z")

</div>

Welcome to the forum @yelkenli

Do you mean that on startup you want to publish the state of the GPIO pin, but never send it again? If that is what you mean then can you explain why you want to do that?

---

<div class="post-metadata">

**Author:** ![yelkenli](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@yelkenli](https://discourse.nodered.org/u/yelkenli)\
**Post date:** [27 January 2025 21:44 UTC](https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943/3 "2025-01-27T21:44:42Z")

</div>

The reason I want to do this is so my system will know the state of the sensor. It will be "unknown" until it receives a message. It only receives a message when the pin changes state. I am thinking this is low overhead.

I have flows that will report the pin state when it changes. As well, I have filters that only react to a pin that is on, and only during the night, so the GPIO setting of 'Read initial state of pin on deploy/restart?' will not necessarily send a message.

---

<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:** [27 January 2025 22:00 UTC](https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943/5 "2025-01-27T22:00:52Z")

</div>

I can't immediately think of a simple way to do it with core nodes, but this in a function node should do what you ask.

```auto
let firstTime = context.get("firstTime") ?? true
if (firstTime) {
    context.set("firstTime", false)
    return msg
}

```

This will send it once when node red restarts. It won't necessarily do it again after a deploy though, as the Deploy may not clear the node's context.

---

<div class="post-metadata">

**Author:** ![ziga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ziga/32/98138_2.png) [@ziga](https://discourse.nodered.org/u/ziga)\
**Post date:** [27 January 2025 23:10 UTC](https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943/6 "2025-01-27T23:10:09Z")

</div>

If question is how to trigger something once on startup and never later, then core input component can be used.

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

Or is the question in combination with how to also get the state of the device?

---

<div class="post-metadata">

**Author:** ![yelkenli](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@yelkenli](https://discourse.nodered.org/u/yelkenli)\
**Post date:** [27 January 2025 23:44 UTC](https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943/7 "2025-01-27T23:44:34Z")

</div>

On start-up, I would like to read the pin of the GPIO and send it to MQTT.

GPIO has a setting for reading on start-up.

I would think there is some start-up block that can inject a value or time stamp. That time-stamp can be used to enable a node to flow through the GPIO pin read for only a short period of time past start-up.

maybe the graphic you show above would work - is epoch the start-up timestamp?

What node is that?

---

<div class="post-metadata">

**Author:** ![ziga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ziga/32/98138_2.png) [@ziga](https://discourse.nodered.org/u/ziga)\
**Post date:** [27 January 2025 23:57 UTC](https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943/8 "2025-01-27T23:57:06Z")

</div>

~~input~~ **inject** node

---

<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:** [28 January 2025 07:45 UTC](https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943/9 "2025-01-28T07:45:41Z")

</div>

> [@yelkenli](#):
>
> I would think there is some start-up block that can inject a value or time stamp

Inject node with checkbox ticked to trigger at start up.

---

<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 April 2025 07:46 UTC](https://discourse.nodered.org/t/best-way-to-send-to-mqtt-client-the-state-of-my-sensor/94943/10 "2025-04-28T07:46:18Z")

</div>

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