MQTT json data parse specific value to sensor

Hi,

I need help with configuring sensor which receive data from MQTT.

So, when I listen to MQTT broker:
image

I get data like this:

Sporočilo 0 prejeto v OilFox ob 8:32:
{
...
    "devices": [
        {
...
            "measurementIntervalInSeconds": 86400,
            "metering": {
                "value": 12,
                "fillingPercentage": 95,
                "liters": 1803,
                "currentOilHeight": 134,
                "serverDate": 1589813339698,
                "battery": 100
            },
...
            }
        }
    ]
}

So how to configure sensor that I read specific data (in my case “liters”) which need be readed as integer type…

  - platform: mqtt
    name: "Trenutni nivo olja"
    state_topic: "OilFox"
    value_template: '{{ value_json.devices[0].metering[0].liters | int }}'
    icon: mdi:hydraulic-oil-level
    unit_of_measurement: L

THANK YOU for help…

If you had put the whole received JSON, we could look in the template editor what works. But seeing the …, it seems you commented pieces out. Difficult to help that way.

Try instead:
value_template: ‘{{ value_json.devices[0].metering.liters | int }}’

Tried, but the same error, seems as that sensor is not integer…

My complete JSON data - if this somehow help…

{
    "id": "XXXX",
    "title": "Herr",
    "firstName": "XXXX",
    "lastName": "XXXX",
    "email": "XXXX",
    "country": "XXXX",
    "locale": "XXXX",
    "passwordSet": true,
    "devices": [
        {
            "id": "XXXX",
            "hwid": "XXXX",
            "name": "XXXX",
            "tankShape": "ROUNDED",
            "tankHeight": 150,
            "tankVolume": 1900,
            "tankOffset": 3,
            "tankIsUsableVolume": true,
            "tankUsableVolume": 1900,
            "productId": "XXXX",
            "notificationInfoEnabled": true,
            "notificationInfoPercentage": 100,
            "notificationAlertEnabled": true,
            "notificationAlertPercentage": 25,
            "address": {
                "city": "XXXX",
                "country": "XXXX",
                "addressLine1": "",
                "zipCode": "1000"
            },
            "partner": {
                "id": "XXXX",
                "name": "XXXX",
                "displayName": "XXXX",
                "supportName": "",
                "supportPhone": "XXXX",
                "supportEmail": "XXXX",
                "accentColor": "#000000",
                "backgroundColor": "#F5CC38",
                "logoUrl": "XXXX",
                "minimumLiters": 500,
                "country": "XXXX",
                "phone": "XXXX",
                "fontPrimaryColor": "#000000",
                "fontSecondaryColor": "#000000",
                "priceHistoryFontColor": "#000000",
                "iconHighlightColor": "#000000",
                "primaryProducts": [
                    {
                        "id": "XXXX",
                        "name": "XXXX",
                        "imageUrl": "XXXX",
                        "measurementInterval": 1440,
                        "type": "HEATING_OIL",
                        "defaultProductId": "XXXX"
                    }
                ],
                "productGroups": [
                    {
                        "id": "XXXX",
                        "name": "Heizöl",
                        "imageUrl": "https://XXXX",
                        "measurementInterval": 1440,
                        "type": "HEATING_OIL",
                        "defaultProductId": "XXXX"
                    }
                ],
                "translationDeOrderButton": "Bestellen",
                "translationEnOrderButton": "Order",
                "translationFrOrderButton": "Commander"
            },
            "measurementIntervalInSeconds": 86400,
            "metering": {
                "value": 12,
                "fillingPercentage": 95,
                "liters": 1803,
                "currentOilHeight": 134,
                "serverDate": 1589813339698,
                "battery": 100
            },
            "chartData": {
                "consumptionData": [
                    {
                        "year": 2019,
                        "month": 6,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2019,
                        "month": 7,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2019,
                        "month": 8,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2019,
                        "month": 9,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2019,
                        "month": 10,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2019,
                        "month": 11,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2019,
                        "month": 12,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2020,
                        "month": 1,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2020,
                        "month": 2,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2020,
                        "month": 3,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2020,
                        "month": 4,
                        "fillingPercentage": 0,
                        "liters": 0
                    },
                    {
                        "year": 2020,
                        "month": 5,
                        "fillingPercentage": 95,
                        "liters": 1803
                    }
                ],
                "priceData": [],
                "forecastData": []
            }
        }
    ]
}

The template of @amaximus works for me in the templates tool.

Auswahl_336
Json for better readability converted to one line.

Tried it too, it works.

So… what should I try that this would work for me…
Do I have error in my mqtt sensor or I did forget something?

What error do you get ?

On this way:

{% set value_json = {"id":"XXXX","title":"Herr","firstName":"XXXX","lastName":"XXXX","email":"XXXX","country":"XXXX","locale":"XXXX","passwordSet":true,"devices":[{"id":"XXXX","hwid":"XXXX","name":"XXXX","tankShape":"ROUNDED","tankHeight":150,"tankVolume":1900,"tankOffset":3,"tankIsUsableVolume":true,"tankUsableVolume":1900,"productId":"XXXX","notificationInfoEnabled":true,"notificationInfoPercentage":100,"notificationAlertEnabled":true,"notificationAlertPercentage":25,"address":{"city":"XXXX","country":"XXXX","addressLine1":"","zipCode":"1000"},"partner":{"id":"XXXX","name":"XXXX","displayName":"XXXX","supportName":"","supportPhone":"XXXX","supportEmail":"XXXX","accentColor":"#000000","backgroundColor":"#F5CC38","logoUrl":"XXXX","minimumLiters":500,"country":"XXXX","phone":"XXXX","fontPrimaryColor":"#000000","fontSecondaryColor":"#000000","priceHistoryFontColor":"#000000","iconHighlightColor":"#000000","primaryProducts":[{"id":"XXXX","name":"XXXX","imageUrl":"XXXX","measurementInterval":1440,"type":"HEATING_OIL","defaultProductId":"XXXX"}],"productGroups":[{"id":"XXXX","name":"Heiz\u00f6l","imageUrl":"https:\/\/XXXX","measurementInterval":1440,"type":"HEATING_OIL","defaultProductId":"XXXX"}],"translationDeOrderButton":"Bestellen","translationEnOrderButton":"Order","translationFrOrderButton":"Commander"},"measurementIntervalInSeconds":86400,"metering":{"value":12,"fillingPercentage":95,"liters":1803,"currentOilHeight":134,"serverDate":1589813339698,"battery":100},"chartData":{"consumptionData":[{"year":2019,"month":6,"fillingPercentage":0,"liters":0},{"year":2019,"month":7,"fillingPercentage":0,"liters":0},{"year":2019,"month":8,"fillingPercentage":0,"liters":0},{"year":2019,"month":9,"fillingPercentage":0,"liters":0},{"year":2019,"month":10,"fillingPercentage":0,"liters":0},{"year":2019,"month":11,"fillingPercentage":0,"liters":0},{"year":2019,"month":12,"fillingPercentage":0,"liters":0},{"year":2020,"month":1,"fillingPercentage":0,"liters":0},{"year":2020,"month":2,"fillingPercentage":0,"liters":0},{"year":2020,"month":3,"fillingPercentage":0,"liters":0},{"year":2020,"month":4,"fillingPercentage":0,"liters":0},{"year":2020,"month":5,"fillingPercentage":95,"liters":1803}],"priceData":[],"forecastData":[]}}]}%}
{{ value_json.devices[0].metering.liters | int }}

I get value 1803.

But on sensor I do not get integer.

Or I’m missing something… it saying that received data is not number…

Sensor settings:

  - platform: mqtt
    name: "Trenutni nivo olja"
    state_topic: "OilFox"
    value_template: ‘{{ value_json.devices[0].metering[0].liters | int }}’
    icon: mdi:hydraulic-oil-level
    unit_of_measurement: L

Your sensor is still the old definition.

  - platform: mqtt
    name: "Trenutni nivo olja"
    state_topic: "OilFox"
    value_template: '{{ value_json.devices[0].metering.liters | int }}'
    icon: mdi:hydraulic-oil-level
    unit_of_measurement: L

Yea, I fixed that, but still I had problem with non-integer value.
image

But solution was:

  - platform: mqtt
    name: "Trenutni nivo olja"
    state_topic: "OilFox"
    value_template: "{{ value_json.devices[0].metering.liters | int }}"
    icon: mdi:hydraulic-oil-level
    unit_of_measurement: L

So the problem was in " ", I changed with " " ".

Thank you everyone for helping me this out…