Trigger Automation with Mqtt Topic with json content

Hi,

from wallbox: i want to use a json formattet mqtt topic for number.input
to wallbox: number.input to serve the mqtt topic json content

topic: openWB/vehicle/template/charge_template/1

json content:

{
    "name": "Ladeprofil i5",
    "disable_after_unplug": false,
    "prio": false,
    "load_default": false,
    "time_charging": {
        "active": false
    },
    "chargemode": {
        "selected": "pv_charging",
        "pv_charging": {
            "min_soc_current": 10,
            "min_current": 0,
            "feed_in_limit": false,
            "min_soc": 0,
            "max_soc": 101
        },
        "scheduled_charging": {},
        "instant_charging": {
            "current": 16,
            "limit": {
                "selected": "soc",
                "amount": 40000,
                "soc": 100
            }
        }
    }
}

for the needed entry chargemode.pv_charging.max_soc json pathfinder say

x.chargemode.pv_charging.max_soc

i tried a lot of things didn´t find the key how to use the json entry

my last try without success

incoming automation:

 ## von openWB Mac Soc
  - id: openwb2_maxsoc_auswahl
    alias: "openWB2 MaxSoc Auswahl"
    description: "receive max soc from openwb2"
    trigger:
      - platform: mqtt
        topic: "openWB/vehicle/template/charge_template/1/chargemode/pv_charging/max_soc"
    action:
      service: input_number.set_value
      data_template:
        entity_id: input_number.openwb2_maxsoc
        value: "{{ trigger.payload }}"
    mode: single

outgoing automation:

   ## in openWB Max SOC setzen
  - id: openwb2_maxsoc
    alias: "openWB2 MaxSoc"
    description: "send max soc to openwbw"
    trigger:
      platform: state
      entity_id: input_number.openwb2_maxsoc
    action:
      service: mqtt.publish
      data:
        topic: "openWB/set/vehicle/template/charge_template/1/chargemode/pv_charging/max_soc"
        retain: true
        payload_template: "{{ states('input_number.openwb2_maxsoc') | int(0) }}"
    mode: single

how can i do that?

2nd question
is it possible to use a entity eg. car_id (number of selected car) instead of the fixed number 1 in
topic: openWB/vehicle/template/charge_template/1

If the JSON content is as shown in your example then for the incoming automation, use trigger.payload_json.chargemode.pv_charging.max_soc

    action:
      service: input_number.set_value
      target:
        entity_id: input_number.openwb2_maxsoc
      data:
        value: "{{ trigger.payload_json.chargemode.pv_charging.max_soc }}"

If that’s the MQTT topic then why are you using this longer topic in both automations?

openWB/set/vehicle/template/charge_template/1/chargemode/pv_charging/max_soc

It seems like you think you can use an MQTT topic to specify a specific key in a JSON value. That’s not how it works.

Yes. Use a wildcard in the topic.

openWB/vehicle/template/charge_template/#

If you do that, you will need to determine which car_id the topic represents in order to store the received max_soc value in the correct Input Number. Here’s one way to do that:

{% set car_id = trigger.topic.split('/')[-1] %}

Use the value of car_id to determine which Input Number to use.

thank you lot for you quick answer
i´ve tried different things so the last try was that with the longer (wrong) topic
the incoming automation works fine - can you help me with the outgoing automation?

Glad to hear it now works.

Please consider marking my post above with the Solution tag. It will automatically place a checkmark next to the topic’s title which signals to other users that this topic’s original question was resolved. This helps other users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

Sure. What exactly is the problem with it?

first - hopefully my english is good enough :wink:
the same as incoming topic the outgoing is also short and need json enhancement for .chargemode.pv_charging.max_soc
openWB/set/vehicle/template/charge_template/1 …json(xx)
when i use the long topic syntax (as above) then i make a new wrong entry - i can see in mqtt explorer but the wallbox don´t recognize

with the car_id there was a misunderstood
i want to use the car_id to make the automation more dynamic and avoid double or tripple automations
like openWB/set/vehicle/template/charge_template/{{states(‘car_id’)}}

forget the first part - after rebooting both HA an Wallbox the outgoing automation works as expected - thank you

maybe you can take a look at the second part with the car_id

Your second automation is designed to take the value of an Input Number and send it to the Open Wallbox device.

I’m unfamiliar with Open Wallbox so I don’t know how it lets you change its max_soc value. Simply publishing a max_soc value to openWB/set/vehicle/template/charge_template/1 is unlikely to work.

To help you with this, I would first need to familiarize myself with Open Wallbox’s programming interface. Do you have a link to its programming information that’s written in English?

as i wrote above after rebooting wallbox and HA the outgoing automation works as expected so no need to study openWB :wink:

last question (for today) can i use this as topic: openWB/set/vehicle/template/charge_template/{{states(‘car_id’)}}

I doubt it. The states() function expects to be supplied with an entity_id. If the value of car_id is not an entity_id then the states() will return none.


so it is
openWB/set/vehicle/template/charge_template/{{states(‘sensor.openWB_chargepoint/4/fahrzeug_id’)}} ?

Unfortunately, I don’t know what is the correct MQTT topic because I have no knowledge or experience with open Wallbox and the information I found for it doesn’t explain how to set the max_soc parameter.

I also searched the forum and found this example but it uses a different MQTT topic from what you suggested:

Now that you know that I am not an open Wallbox expert, and my suggestions might be incorrect, you may wish to try this version:

openWB/set/vehicle/template/charge_template/{{states('sensor.openWB_chargepoint_4_fahrzeug_id')}}

you don´t have to spent your time with openWB :wink:
i also use this integration

as in my above automation the topic for publishing is
topic: “openWB/set/vehicle/template/charge_template/1/chargemode/pv_charging/max_soc”
and i want to replace the number 1 with {{states(‘sensor.openWB_chargepoint_4_fahrzeug_id’)}}
so i have
topic: “openWB/set/vehicle/template/charge_template/{{states(‘sensor.openWB_chargepoint_4_fahrzeug_id’)}}/chargemode/pv_charging/max_soc”

when i use this in automation -

Got error ‘TemplateError: Use of ‘states’ is not supported in limited templates’ when setting up triggers for openWB2 MaxSoc Auswahl