Automation to set bed and tool temp in octoprint

I want to set my bed and tool temp for my printer via an automation so I can set it directly from my dashboard.

I installed the mqtt, homeassistant discovery and mqtt service plugins in octoprint.

Using mqtt explorer I can successfully set the bed and tool temps however it isn’t working when I try to do it in home assistant, either by calling the mqtt_publish service or via an automation.

This is my automation code:

alias: Mega X test
description: ""
trigger:
  - platform: template
    value_template: "{{ states.input_number.mega_x_bed_temp_helper.state | int }}"
condition: []
action:
  - service: notify.notify
    data:
      message: Mega X bed temp has been set to the inputted value
  - service: mqtt.publish
    data:
      qos: 0
      retain: false
      topic: octoprint/cmd/bed
      payload: {'"temp":30'}
mode: single

I have tried variations on the above using call-service, service_data and multiple variations on the payload but none of them change the temp on octoprint.

Additionally, I want to input the number inputted from the dashboard stated in the trigger as the payload, I’m just using a hardcoded number above to try and get it working.

Can anyone help me to get this working?

Thanks

without seeing what the payload is normally it’s hard to know for sure but try:

 payload: "{{'temp':30}}"

Thanks for the suggestion @finity, unfortunately I get an error when I try that payload format:

Message malformed: template value should be a string for dictionary value @ data[‘action’][1][‘data’]

This was the post that I followed so far but I couldn’t get it to work using this format or variations of it

Remove the single quotes from this:

      payload: {'"temp":30'}

to make it into proper JSON like this:

      payload: "{\"temp\":30}"

Reference: mqtt.publish