MQTT Number integration and Victron - how to format the payload on the command topic

My Victron system is integrated with Home Assistant using MQTT. I have no issue reading any parameter from Victron by defining each variable as a sensor. However, I also want to write stuff back to Victron to change certain parameters (example: change the max allowed input amperage on the inverter). My thinking is to use the MQTT Number integration for that.

My numbers.yaml looks like this:

- platform: mqtt
  name: "Max Input Current"
  state_topic: "venus-home/N/<serial>/vebus/276/Ac/In/1/CurrentLimit"
  unique_id: "280_1"
  value_template: "{{ value_json.value }}"
  min: 0
  max: 50
  command_topic: "venus-home/W/<serial>/vebus/276/Ac/In/1/CurrentLimit"

The issue is that Victron is sending and expecting to receive payload in JSON format. I can deal with the input configuration using a value template. But how do I send JSON to the proper command topic. Currently HA is just sending the variable to victron without formatting it as a JSON. The number integration doesn’t have an option to specify templating on the command topic. Any suggestions on how to handle this?

1 Like

You can do the old way with an automation that uses a State Trigger listening to an input_number and publishing its value in the desired JSON format. Easy peasy.

How do I pass variables in an automation? I set it up so that I can an incoming MQTT message with the desired number as payload. Then, I want to construct an outgoing MQTT message, properly formatted to pass that number to Victron. How do I reference the incoming payload?

This is what my Action Yaml look like:

service: mqtt.publish
data:
  topic: venus-home/W/<serial>/vebus/276/Ac/In/1/CurrentLimit
  payload_template: '{% set obj = {"value": states(trigger.payload)} %} {{ obj|to_json }} '

arggh never mind… trigger.payload does work. Without the states()

service: mqtt.publish
data:
  topic: venus-home/W/<serial>/vebus/276/Ac/In/1/CurrentLimit
  payload_template: '{% set obj = {"value": trigger.payload} %} {{ obj|to_json }} '

I was trying to do the same thing and ended up using command_template to set the payload. Seems a bit simpler, so I’m posting it here in case it helps anyone else:

- platform: mqtt
  name: "Max Input Current"
  state_topic: "venus-home/N/<serial>/vebus/288/Ac/In/1/CurrentLimit"
  unique_id: "280_1"
  value_template: "{{ value_json.value }}"
  min: 0
  max: 15
  command_topic: "venus-home/W/<serial>/vebus/288/Ac/In/1/CurrentLimit"
  command_template: '{"value" : "{{value}}"}'
1 Like

Hello I try the same, but I cant set a value. In the command_template I try value or burnmaxpower.
Thanks

    - name: "Kessel 2 maximale Brennerleistung"
      unique_id: kessel_2_maximale_brennerleistung
      state_topic: "ems-esp2/boiler_data"
      value_template: "{{ value_json.burnmaxpower }}"
      availability_topic: "ems-esp2/status"
      command_topic: "ems-esp2/boiler_data"
      command_template: '{"burnmaxpower" : "{{value}}"}'
      mode: box
      min: 25
      max: 100
      step: 1
      unit_of_measurement: "%"

in the MQTT Explorer it looks like this