Template is not rendering the JSON value

I would like to have a graphical history of my gas consumption, for now, I will manually add the entries in a JSON file, maybe in the future I’ll create some external service to get that data.

Here the JSON I’ve created (one line):

{"08/11/2022": {"value": 3521}, "14/11/2022": {"value": 3522}, "21/11/2022": {"value": 3523}}

Here the configuration I put in configuration.yml

 - platform: file
   name: contador_gas
   file_path: /config/sensores_manuales/contadorgas.json
   value_template: "{{ value_json[now().strftime('%d/%m/%Y')].value | float }}"
   unit_of_measurement: 'm³'

The sensor is well registered, config is validated, but I can’t see the data in the history graph.
Any idea?

Thank you in advance!

You can’t use .value. In a template, that is a reserved word that means “return all the raw value” (like value_json returns json encoded values). Use ['value'] instead, so:

value_template: "{{ value_json[now().strftime('%d/%m/%Y')]['value']| float(0) }}"
1 Like

Thank you for your answer!

I’ve changed the .value to [‘value’], then I restarted HA, stills without any data in the history chart.
I tried to change the field name “value” to “cubicMeters”, but same result…

What state do you get if you just use:

value_template: "{{ value }}"

Before it was unknown and now (with value_template: "{{ value }}") it’s 0, so… I think there’s something wrong reading the JSON ? maybe the date format ?

Maybe the list of allowd paths?

    - /config/sensores_manuales

Oh. Remove the unit of measurement. Just using {{ value }} should return your entire string:

{"08/11/2022": {"value": 3521}, "14/11/2022": {"value": 3522}, "21/11/2022": {"value": 3523}}

Now I have the entire string in the entity card:

What should I do to make it work as a history card ?

Thank you again!

Ok so far so good. What does this return in the template editor:

{{ now().strftime('%d/%m/%Y') }}

It returns the current date


That’s not the template editor (Developer Tools → Templates) that’s your sensor. That will do but it’s a long way of testing it :slight_smile:

I think the problem may be that your date is not enclosed in quotes in the square brackets. You can’t just add quotes or it will return that bit of the template literally ('now()…etc).

Maybe this:

value_template: "{{ value_json[ '' ~ now().strftime('%d/%m/%Y') ~ '' ]['value']| float(0) }}"

Those are two single quotes. Not double quotes.

Edit: hang on, do you have a data point in the file for today’s date?

1 Like

Finally the solution was getting the last result of the JSON using the filter last:

value_json [value_json | last]

Thank you for all!