Node-RED, MQTT and templates

Hi.
I’m working on automating my thermostat after succesfully connecting an Opentherm gateway.
I have also succesfully created a climate entity. When I set the temperature in this climate entity, it calls an mqtt publish command that sets the temperature. So far, so good.
I can confirm that this works by publishing the following code in the dev sections of HA:

service: mqtt.publish
data:
  topic: opentherm-thermostat/setpoint-temperature/set
  payload: "{{ states('input_number.set_to_temperature_1')}}"

input_numer.set_to_temperature_1 is a helper I created. Let’s say I set this to 10. When I publish this mqtt code, it succesfully set’s the climate entity to 10 degrees Celcius. The thermostat also responds to this correctly by turning on or off depending on the current temperature.

Now, here’s the problem:
When I add the following code to node-red, it results in the climate entity being set to 0 degrees. No matter what I set the temperature to. When I don’t use a template code for the payload, but just a simple value, it DOES work. Somehow, it doesn’t like my template. The template in the topic DOES work, just not the one at the payload.

[{"id":"be279730a0dc1b8d","type":"api-call-service","z":"d3e6bcc99a40c340","name":"","server":"314cec54.7a6264","version":5,"debugenabled":false,"domain":"mqtt","service":"publish","areaId":[],"deviceId":[],"entityId":[],"data":"{\"payload\":\"{{ states('input_number.set_to_temperature_1')}}\",\"topic\":\"opentherm-thermostat/setpoint-temperature/set\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1010,"y":220,"wires":[[]]},{"id":"314cec54.7a6264","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

Any suggestions on how to proceed? I don’t see the difference in the template code that I ‘fire’ from the HA dev menu and the code in node-red… Why does it work in the dev menu, and not in node-red?

Mustache templates in Node Red and HA do different things. If you click on the “Accepts Mustache Links” link in the node’s documentation it shows examples like “{{ payload }}”, indicating that it’s substituting data from the incoming message, not HA.

You could change the type of data from JSON (image) to JSONata (image) and then use something along the lines of:

$globalContext('homeassistant').homeAssistant

to get access to HA states, however I’d suggest this is a bad idea as it hides away what is going on. Instead you should insert a “current state” node before this node that gets the state of your input number into “msg.set_temperature” (and remove other output properties). Then you could use:

{"payload":"{{ set_temperature }}","topic": "whatever"}

Then it is much more obvious this flow is dependent on that input.

2 Likes

thank you, that did it!