Reading MQTT subtopics topic with wildcards into a sensor with attributes

Hi,

I try to read different MQTT subtopics into a sensor with attributes.

In the MQTT-Explorer it looks like this:

I tried following code to read this out:

    - name: "Test Heizung heute Energieverbrauch"
      state_topic: "open3e/vitocal250/680_548_EnergyConsumptionCentralHeating/+"
      state_class: total_increasing
      unique_id: 680_548_TestEnergyConsumptionCentralHeatingToday
      object_id: 548_TestEnergyConsumptionCentralHeatingToday
      device:
        name: "Open3e_Vitocal250A"
        identifiers: !secret vitocal_sn
        manufacturer: "Viessmann"
        model: "Vitocal 250-A"
      icon: mdi:home-lightning-bolt
      value_template: '{{value_json.Today}}'
      unit_of_measurement: "kWh"
      device_class: energy
      json_attributes_topic: "open3e/vitocal250/680_548_EnergyConsumptionCentralHeating/+"
      json_attributes_template: '{{ value_json | tojson }}'

However the sensor remains ‘unknown’ with no contents in the attributes.

What works, is reading all subtopics in single steps and then templating them together in a Helper. While the effort is acceptable with 6 subtopics, it would be favourable to read out more complex Topics in one step automaticly.

Like this one:

Do you have any ideas?
Best regards
Hendrik

By doing this (adding a + to the end of the topic):

open3e/vitocal250/680_548_EnergyConsumptionCentralHeating/+

your MQTT Sensor is subscribed to the following six sub-topics:

Today
Past7Days
CurrentMonth
PastMonth
CurrentYear
PastYear

The value published to each of those topics (the “payload”) is not in JSON format, it is simply a number.

That’s why this template doesn’t work, and produces an unknown result, because it mistakenly assumes the received payload is in JSON format. It’s not.

value_template: '{{value_json.Today}}'

The same problem exists for attributes and that’s why it fails to produce a result.

json_attributes_topic: "open3e/vitocal250/680_548_EnergyConsumptionCentralHeating/+"
json_attributes_template: '{{ value_json | tojson }}'

The variables named value and value_json represent the received payload. By subscribing to six topics, your MQTT Sensor receives six separate payloads (sequentially, not concurrently). You can’t use a JSON oriented template like value_json.Today to distinguish one sub-topic’s received value from another.

If you want the MQTT Sensor to report the Today value you will need to subscribe exclusively to that one topic.

open3e/vitocal250/680_548_EnergyConsumptionCentralHeating/Today

Then either remove the value_template option or set its template to {{ value }}.

In this case, you can’t create attributes for the other 5 sub-topics because json_attributes_topic and json_attributes_template are meant for processing the JSON payload of a single topic.

Thanks for the information, that helped me a lot.

I managed, that the MQTT Topic (last Image in first post) delivers now a payload JSON format:

{
  "CurrentMonth": {
    "10": 0,
    "11": 0,
    "12": 0,
    "13": 0,
    "14": 0,
    "15": 0,
    "16": 0,
    "17": 0,
    "18": 0,
    "19": 0,
    "20": 0,
    "21": 0,
    ... and so on

I now used following Code in the mqtt sensor to read the attributes:

      json_attributes_topic: "open3e/vitocal250/680_1294_EnergyConsumptionCentralHeatingMonthMatrix"
      json_attributes_template: >-
        { "10.": {{value_json.CurrentMonth.10}},
          "11.": {{value_json.CurrentMonth.11}},
          "12.": {{value_json.CurrentMonth.12}},
          "13.": {{value_json.CurrentMonth.13}},
          "14.": {{value_json.CurrentMonth.14}},
          "15.": {{value_json.CurrentMonth.15}},
          "16.": {{value_json.CurrentMonth.16}},
          "17.": {{value_json.CurrentMonth.17}},
          "18.": {{value_json.CurrentMonth.18}},
          "19.": {{value_json.CurrentMonth.19}},
          "20.": {{value_json.CurrentMonth.20}},
          "21.": {{value_json.CurrentMonth.21}},
          "22.": {{value_json.CurrentMonth.22}},
          "23.": {{value_json.CurrentMonth.23}},
          "24.": {{value_json.CurrentMonth.24}},
          "25.": {{value_json.CurrentMonth.25}},
          "26.": {{value_json.CurrentMonth.26}},
          "27.": {{value_json.CurrentMonth.27}},
          "28.": {{value_json.CurrentMonth.28}},
          "29.": {{value_json.CurrentMonth.29}},
          "30.": {{value_json.CurrentMonth.30}},
          "31.": {{value_json.CurrentMonth.31}}
           ... and so on }

I get the error message:
TemplateSyntaxError: expected token ‘end of print statement’, got ‘integer’

What did I wrong? Are there too many attributes (31)?
Thanks in advance for your help
Hendrik

Not sure if this is the only thing that’s off, but it looks to me like you’re misusing {{ }} print statements.

Although I haven’t worked with json_attributes_template before, I think that at minimum:

  1. You need to enclose the whole dictionary (with the keys “10.”, “11.” etc.) in a single {{ }}.
  2. You need to remove the {{ }} surrounding the individual value_json references.

That’s consistent with the error message.

Also, I assume that the ... and so on} is not part of your actual code. In general, it’s not helpful to modify your code with off-syntax edits like that for posting in the forum because it can easily obscure problems or mislead people when they suggest solutions.

Hi Dominik,

thanks for your hint. It was not the solution, however goes in the right direction. After consulting other threads in the forum and a lot of trial and error, I found the solution.

The templates work as following:

          "01": {{value_json.CurrentMonth['01']}},

With this, all the attributes are inserted in the sensor entity correctly.

Thank for the support
Hendrik