# Get a value from a sensor with mqtt

**URL:** <https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079>\
**Category:** General\
**Created:** [26 July 2023 09:58 UTC](https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079 "2023-07-26T09:58:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![EcarDK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ecardk/32/76058_2.png) [@EcarDK](https://discourse.nodered.org/u/EcarDK)\
**Post date:** [26 July 2023 09:58 UTC](https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079/1 "2023-07-26T09:58:35Z")

</div>

Hallo all

First of all, thank you for all the help you give me.

i would like to use an "inject node" connected to an "mqtt out" to pull a "payload.illuminance\_lux" value so it comes out of an "mqtt in node"  
is it possible and how do you set it up?

```auto
[{"id":"8fac63a5a17dd303","type":"mqtt in","z":"fb88cea7421b6512","name":"","topic":"zigbee2mqtt/Sensor/illuminance_lux","qos":"2","datatype":"auto-detect","broker":"711011c119463c3b","nl":false,"rap":true,"rh":0,"inputs":0,"x":1020,"y":2260,"wires":[["93e8b16157eac356"]]},{"id":"dcb90c9a4f690539","type":"mqtt out","z":"fb88cea7421b6512","name":"","topic":"zigbee2mqtt/Sensor/illuminance_lux","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"711011c119463c3b","x":1230,"y":2200,"wires":[]},{"id":"93e8b16157eac356","type":"debug","z":"fb88cea7421b6512","name":"debug 14","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1300,"y":2260,"wires":[]},{"id":"333f5e04585b7f21","type":"inject","z":"fb88cea7421b6512","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":940,"y":2200,"wires":[["dcb90c9a4f690539"]]},{"id":"711011c119463c3b","type":"mqtt-broker","name":"MQTT Broker","broker":"192.168.1.106","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 July 2023 12:43 UTC](https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079/2 "2023-07-26T12:43:17Z")

</div>

Your flow seems to work - it passes a message from inject to debug via an MQTT broker.

Is the problem that this flow does not work for you? In that case your MQTT broker is not working.

Or do you need help requesting data from the sensor?

---

<div class="post-metadata">

**Author:** ![EcarDK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ecardk/32/76058_2.png) [@EcarDK](https://discourse.nodered.org/u/EcarDK)\
**Post date:** [26 July 2023 15:05 UTC](https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079/3 "2023-07-26T15:05:42Z")

</div>

I only get a timestamp out. 1690383855807. But I will like to get the payload.illuminance\_lux value out.

so yes. I need to get date out from the sensor

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 July 2023 15:42 UTC](https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079/4 "2023-07-26T15:42:56Z")

</div>

That depends how your sensor works.

Some send data on a serial port, some by Mqtt over wi-fi.

Some send it at regular intervals, some you have to request the data.

So to advise further we will need to know the kind of sensor, how it is connected and possibly the type of firmware it runs.

---

<div class="post-metadata">

**Author:** ![EcarDK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ecardk/32/76058_2.png) [@EcarDK](https://discourse.nodered.org/u/EcarDK)\
**Post date:** [26 July 2023 16:18 UTC](https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079/5 "2023-07-26T16:18:07Z")

</div>

I use a Philips Hue motion sensor

> **[Philips 9290012607 control via MQTT | Zigbee2MQTT](https://www.zigbee2mqtt.io/devices/9290012607.html)**
>
> Integrate your Philips 9290012607 via Zigbee2MQTT with whatever smart home infrastructure you are using without the vendor's bridge or gateway.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [26 July 2023 18:05 UTC](https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079/6 "2023-07-26T18:05:59Z")

</div>

I don't use zigbee2MQTT but I would think that the sensor will send periodic updates, particularly when there is a change of state on the sensor.

If you watch the incoming messages with a debug node do you see this data ?

EDIT just checked docs, you cannot force this reading so will have to wait for it to be sent to you.

### Illuminance\_lux (numeric)

Measured illuminance in lux. Value can be found in the published state on the `illuminance_lux` property. It's not possible to read (`/get`) or write (`/set`) this value. The unit of this value is `lx`.

---

<div class="post-metadata">

**Author:** ![EcarDK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ecardk/32/76058_2.png) [@EcarDK](https://discourse.nodered.org/u/EcarDK)\
**Post date:** [27 July 2023 08:24 UTC](https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079/7 "2023-07-27T08:24:36Z")

</div>

okay. to bad

I was trying to use it as a "events: state" node, but I have to use home assistant node then.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [27 July 2023 12:27 UTC](https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079/8 "2023-07-27T12:27:03Z")

</div>

What are you trying to do with the information that you need to poll the device ?

When a state msg arrives via MQTT you can take appropriate actions based on the lux value.

If you want to take actions at a later time, then you could store the lux values into a context variable as they arrive, then use the last stored value in your flows.

---

<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:** [25 September 2023 12:27 UTC](https://discourse.nodered.org/t/get-a-value-from-a-sensor-with-mqtt/80079/9 "2023-09-25T12:27:06Z")

</div>

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