Create sensor by parsing json file

Hi,

I am trying to retrieve values of gas consuption from a JSON wich is daily updated by pulling data from the gas provider to update a new sensor with long term statistic to feed the Energy dashboard.

The Json file seems to be correctly structured:

{"29/09/2021": {"kwh": 1, "mcube": 0.17}, "30/09/2021": {"kwh": 4, "mcube": 0.39}, "01/10/2021": {"kwh": 0, "mcube": 0.0}, "02/10/2021": {"kwh": 3, "mcube": 0.33}, "03/10/2021": {"kwh": 4, "mcube": 0.39}, "04/10/2021": {"kwh": 1, "mcube": 0.17}, "05/10/2021": {"kwh": 4, "mcube": 0.41}, "06/10/2021": {"kwh": 8, "mcube": 0.73}, "07/10/2021": {"kwh": 3, "mcube": 0.33}, "08/10/2021": {"kwh": 3, "mcube": 0.27}, "09/10/2021": {"kwh": 0, "mcube": 0.05}, "10/10/2021": {"kwh": 2, "mcube": 0.18}}

With an online json parser we can find the structure where each date is composed by sub values “kwh” and “mcube”.

I want to extract the value from the last day (more recent) and store it in a sensor for all this day, then the sensor will be updated each day at 7pm with the new (yesterday) value from the JSON of the day. I dont know yet how to deal with this 24h shift on the sensor timeline.

I had trying someting like:

  - platform: file
    sensor:
      name: gas daily consuption
      file_path: /config/gazpar/conso_par_jour.json
      value_template: "{{ value_json.29/09/2021.mcube | float }}"
      unit_of_measurement: 'm³'
      state_class: "measurement"
      device_class: "gas"
      attributes:
        last_reset: '1970-01-01T00:00:00+00:00'

Where ‘29/09/2021’ must be called dynamically with the current date.

I have done some research but dit not find how to deal with JSON structure.

Thanks a lot for your help,
Regards

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

Just keep in mind that the file: platform only consider the last line of the file

If you want the Template Sensor to update just once a day at a specific time, you will need to create a Trigger-based Template Sensor.

Thanks for your anwers

OK, I had tested the trigger based sensor to update at a specific time and because it was easier for me I use it to copy a sensor with the same information.

This sensor has his value reseted each day to -1 in order to have the ability to check on an automation that the value has not been upated yet (the script that pulls the info from gas provider need to be lauch several times because it often leads to a timeout). Here is the sensor:

The issue is that it copies all the sensor each day at a specific time, not a value at a specific time. My goal is to have in the new sensor (to feed Energy dashboard) for each day (meaning from 00:00:00AM to 11:59:59PM) the consuption value (no -1 value in between).

The other idea was to put the JSON values who are specified by day into this sensor. I will try @koying value_template but I don’t see how it will be written is the sensor in term of time axis.

I will finally use the a sensor value ‘total_increasing’ of the current month consumption. The Energy dashboard will calculate the daily consumption at each update of the monthly consumption sensor.
Nevertheless, I am interested to declare an offset of the monthly_consuption value. The value from day D-1 is retrieved and store for the day D.