Can I call call mqtt.publish from Jinja2 template engine?

In developers tools I would like to call mqtt.publish but I cannot find the way how:


{%
  if int(states('sensor.pzem_a_power_watts')) < int(states('sensor.pzem_b_power_watts'))
   and
   (float(states('sensor.a_voltage'))
   - float(states('sensor.b_voltage'))) > 1
%}
  #this is the pseudo code I need to use
  {
  - service: mqtt.publish
    data:
      topic: topic/1
      payload: '{"payload_a": 1}'
  }
{% else %}
    #this is the pseudo code I need to use
    - service: mqtt.publish
    data:
      topic: topic/1
      payload: '{"payload_b": 2}'
{% endif %}

can you advise how ?

You can’t template yaml configuration… instead use variables to define the values you need to use in your service call.

- variables:
   payload: >  
     {% if int(states('sensor.pzem_a_power_watts')) 
     < int(states('sensor.pzem_b_power_watts')) and
     (float(states('sensor.a_voltage'))
     - float(states('sensor.b_voltage'))) > 1%}
       '{"payload_a": 1}'
     {% else %}
       '{"payload_b": 2}'
     {% endif %}
- service: mqtt.publish
  data:
    topic: topic/1
    payload: "{{ payload }}"

so I cannot call service from developers tools? The code I am testing is just in developers tools. **image

Publish is a service. So Developer Tools → Services.

OK, thanks for clarification