MQTT Topic with template for automation & script

Hi there,

I’m trying to automate my climate system.
I’s composed by multiple air conditioner -> modbus interface -> plc -> custom reading interface (provided by the manufacturer of the plc) -> mqtt (through node-red)

The variable I can control are multiple per machine (mode of operation, on_off, swing, temperature, fanspeed) for 7 machines and they are managed by plc in form of array, so

ac_mode[]
ac_fanspeed[] 
ac_onoff[]
...

they are arrays of 7 elements that control the operations of the various machines.

I have created some input_select to select the parameter and one to select to wich machine to send them, but:

is it possible to create a varible MQTT topic based on a template?

To achieve something like

ac_mode[machine_number]
ac_fanspeed[machine_number]
ac_onoff[machine_number]
....

where machine_number is selected through an input_select…

Thank you!!!

Well you could use the MQTT Publish service and template the attributes

So something like this should work:

service: mqtt.publish
data_template:
  topic: >
    {{ "plc/set/ac_mode["  ~ states('input_select.machine_number') ~ "]" }}   # this should be plc/set/ac_mode[0]
  payload: >
    {{ states('input_select.ac_mode') }}

Let me try this and I will report back how it goes :+1:t2:

thank you!!

1 Like