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

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?

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?

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.

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.

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.

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

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

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?

input inject node

Inject node with checkbox ticked to trigger at start up.