MQTT: publish - script without value


I am using self made ESP sensors with MQTT discovery to easily assign them as a device. Everything works flawlessly except one thing - when I want to make discovery script (in HASS), just to have some backup in case of MQTT broker failure, I use following part to change readout of my ESP:

"value_tepmplate": "{{(float(value)*0.007142857143) | round(2)}}"  

The problem is Home Assistant doesn’t allow me to send payload with such statement because " ‘value’: is undefined".

Of course there is no problem with sending such a payload with MQTT Explorer or python - the sensor is then read out without problem.

Is there any workaround to NOT use … {{value}} … as a variable - just want HASS to treat payload as plain text.

If you don’t want the template to use value as the reserved word use square bracket notation, i.e. ['value'] in place of value.

Unfortunately it still wants to render data template.

..."value_template": "{{(float(['value'])*0.007142857143) | round(2)}}"...

“Error rendering data template: ValueError: Template error: float got invalid input ‘[‘value’]’ when rendering template”.

Well, basically I just want whole payload as a plain text, and HA tries to do it’s magic I guess.

If you want it as plain text you have to remove the curly braces. Or not use value_template.

I really don’t understand what you are trying to do here.

When you remove curly braces the mqtt discovery payload is not treated as value to use afterwards.

I really don’t understand what you are trying to do here.

I just want to create HA script to send MQTT discovery payload AND use value_template to change ESP readout value. (Sometimes you want to adjust some values, especially when testing. It’s handy to have premade MQTT message in HA instead of Python).

And as I said, sending whole payload with MQTT explorer or Python with all curly braces, float(value), and stuff works without problem - the problem is that HA wants to treat plain text as value template, which does not make any sense. There should be just option to use plain text.

Anyway, I’m going to stick with python script then.

Show the whole script.


"value_template":"{% raw %}{{(float(['value'])*0.007142857143) | round(2)}}{% endraw %}"

as in this example

      - service: mqtt.publish
          retain : true
          topic: homeassistant/sensor/xiron_4503/temperature/config
          payload: '{"unit_of_measurement":"°C","device_class":"temperature","value_template":"{% raw %}{{value_json.TEMP}}{% endraw %}","state_class": "measurement","state_topic":"rflink/Xiron-4503","name":"boven_temperature","unique_id":"Xiron_4503_temp","device":{"identifiers":["xiron_4503"],"name":"xiron_4503","model":"Digoo temp & humidity sensor","manufacturer":"Digoo"}}'

1 Like

That’s perfect, exactly what I was looking for.
Thank You very much, apparently I didn’t dig deep enough in template designer documentation.