How to decode special JSON ? Ex => "conso_hc" : 006699756,

no…
I would probably do something about the source.
You could perhaps use something to parse the incorrect data and feed HA with correct json.

@Hellis81,
No problem, thank you for your time !

You could probably do this with node red.
It has JavaScript in the function nodes. That is probably a good way to parse the data.

Just use an inject node to start the sequence timed and have it populate a input text with the value.

My brother will try to help me tomorrow evening (french time).
If no sucess, If somebody have time to help me, I can organize a teamviewer session :slight_smile:

Sorry won’t have time for that.
I have two kids that need my attention.

But a node red sequence is not very hard to set up.

Take inject node, http node and debug node to start with.
Connect the three and set debug to output complete message.
Add the URL of the json in the http node and press the inject node and see what the debug console shows.

If that gives you the json(-ish) then it’s fairly simple to do the rest

1 Like

Maybe you can use a command_line sensor with curl to get and filter out the values.

1 Like

@Hellis81,

Thanks to your message I have installed node-red and build your test => Really simple to use
Thanks to this test, I can confirmed that JSON doesn’t work due to zero value

Great.

Now between the http node and debug node insert a function node.

In the function node add the following: (keep the line that is there and add this above)

var pattern1 = new RegExp("\"conso_hc\"\s+:\s+(\d+)");
msg.conso_hc = patt.test(msg.payload);

var pattern2 = new RegExp("\"conso_hp\"\s+:\s+(\d+)");
msg.conso_hp= patt.test(msg.payload);

Now click on the inject again after you deployed it.
This should mean the message should include the two values as separate values.

You will see that as msg.payload and the arrow you clicked to expand it (don’t do that this time), but below this there should be a msg.conso_hp and msg.conso_hc.
If I didn’t screw up…

Seems not working but perhaps I did something wrong, I’m beginner in HA & Node-Red.


Sorry I was unclear.
The return msg should be the last line

Ok, I still got an issue.

My bad again…

var pattern1 = new RegExp("\"conso_hc\"\s+:\s+(\d+)");
msg.conso_hc = pattern1.match(msg.payload);

var pattern2 = new RegExp("\"conso_hp\"\s+:\s+(\d+)");
msg.conso_hp= pattern2.match(msg.payload);

return msg;

Many thanks for your time ! I really appreciate.

I think we are no far from the result but unfortunately your last proposal is not working too.

@Hellis81,

Below the link of my ecocompteur JSON data, it could help you to try directly on your HA?
http://92.90.224.84:8010/data.json
=> Now desactivated

Thanks in advance

That helped :slight_smile:

pattern1 = /"conso_hc"\s+:\s+(\d+)/;
msg.conso_hc = msg.payload.match(pattern1);

pattern2 = /"conso_hp"\s+:\s+(\d+)/;
msg.conso_hp = msg.payload.match(pattern2);

return msg;

Now add a text helper with the name conso_hc and add a call service node.

{"value":"{{conso_hc.1}}"}

image

Dear all,

Thanks to all of you and my friends, I found the a solution :slight_smile:

My configuration.yaml file :

  - platform: rest
    name: "EcoCompteur_conso_hp"
    resource: http://192.168.1.92/data.json
    value_template: '{{ (((value|regex_replace("([ :])0+(\\d+),", "\g<1>\g<2>,")|from_json).conso_hp) | float * 1 / 1000) | round(1) }}'
    device_class: energy
    unit_of_measurement: "kWh"
    
  - platform: rest
    name: "EcoCompteur_conso_hc"
    resource: http://192.168.1.92/data.json
    value_template: '{{ (((value|regex_replace("([ :])0+(\\d+),", "\g<1>\g<2>,")|from_json).conso_hc) | float * 1 / 1000) | round(1) }}'
    device_class: energy
    unit_of_measurement: "kWh"

The result :

  • image

Usefull links

1 Like

Just keep in mind that your pattern requires at least one zero before the value.
So it will break if there is no zero before the number.

Good job!

1 Like

It’s true, thanks to highlitgh this point, so finally it’s a temporary solution…

I will continue to work on it with you if you agree.
My target is to find a robust solution.

@Hellis81,

I have checked with my friend and I think that our solution is finally the final one because it should have no issue if there is no left zero in the future.

Indeed, the target of our code is just to remove left zero values in the original http response in order to use normal JSON decoding . If there is no zero value on the left, this means that the original http answer is already in a correct JSON format. In this case, it’s normal that our code is not doing something.

Explanation :

'{{ (((value|regex_replace("([ :])0+(\\d+),", "\g<1>\g<2>,")|from_json).conso_hp) | float * 1 / 1000) | round(1) }}'
  • ’{{ (((value => This is the raw string got from http

  • "([ :])0+(\d+)," => Select the data in two group => If there is zero left value, it select in Group1 is the left zero value and Group2 the decimal values without the left zero value. If there is no left zero values, this code will have no impact on the raw string from http and finally correspond to a normal Json.

  • "\g<1>\g<2>," => Replace Group1 by Group2 => The consequence is that the http response is corrected into a Json format. If they not exist, this code will have no impact on the raw string from http and finally correspond to a normal Json

  • from_json).conso_hp => Extract the value from JSON

I spent a long time to have something working for this device so I can share the package I use. Maybe it can help you…


#---
   ############################################################
   ##                                                        ##
   ##                PACKAGE LEGRAND ECO-COMPTEUR            ##
   ##                                                        ##
   ############################################################
homeassistant:
   ############################################################
   ##                                                        ##
   ##                     CUSTOMIZE                          ##
   ##                                                        ##
   ############################################################

  customize:

    ################################################
    ## Node Anchors
    ################################################
    package.node_anchors:
      customize: &customize
        package: 'package_legrand'

    ################################################
    ## Equipements
    ################################################

    sensor.legrand_ecocompteur_conso_hc:
      <<: *customize
      friendly_name: "Consommation HC"

    sensor.legrand_ecocompteur_conso_hp:
      <<: *customize
      friendly_name: "Consommation HP"

    sensor.legrand_ecocompteur_intensite_souscrite:
      <<: *customize
      friendly_name: "Intensité Maximale"
      icon: mdi:alpha-w-circle-outline

    sensor.legrand_ecocompteur_puissance_inst_prises:
      <<: *customize
      friendly_name: "Prises"
      icon: mdi:alpha-w-circle-outline

    sensor.legrand_ecocompteur_puissance_inst_refroidissement:
      <<: *customize
      friendly_name: "Pompe à Chaleur"
      icon: mdi:alpha-w-circle-outline

    sensor.legrand_ecocompteur_puissance_inst_totale:
      <<: *customize
      friendly_name: "Puissance Totale"
      icon: mdi:alpha-w-circle-outline

    sensor.legrand_ecocompteur_puissance_inst_eau_chaude:
      <<: *customize
      friendly_name: "Cumulus"
      icon: mdi:alpha-w-circle-outline

    sensor.legrand_ecocompteur_consommation_totale_eau:
      <<: *customize
      friendly_name: "Eau"
      icon: mdi:counter

    sensor.legrand_hchp:
      <<: *customize
      friendly_name: "Période Tarifaire"
      icon: mdi:av-timer

    sensor.legrand_consommation_heures_pleines:
      <<: *customize
      friendly_name: "Conso Heures Pleines"
      icon: mdi:counter

    sensor.legrand_consommation_heures_creuses:
      <<: *customize
      friendly_name: "Conso Heures Creuses"
      icon: mdi:counter


   #####################################
   ##                                 ##
   ##             SENSORS             ##
   ##                                 ##
   #####################################

sensor:

# Commande JSON Récupération Valeur Puissances Instantanées Climatisation
  - platform: rest
    resource: http://192.168.X.X/inst.json
    name: Legrand Ecocompteur Puissance Inst. Climatisation
    value_template: '{{ value_json.data2| round(0) }}'
    device_class: power
    unit_of_measurement: W

 - platform: rest
    resource: http://192.168.X.X/inst.json
    name: Legrand Ecocompteur Puissance Inst. Eau Chaude
    value_template: '{{ value_json.data3| round(0) }}'
    device_class: power
    unit_of_measurement: W

 - platform: rest
    resource: http://192.168.X.X/inst.json
    name: Legrand Ecocompteur Puissance Inst. Prises
    value_template: '{{ value_json.data4| round(0) }}'
    device_class: power
    unit_of_measurement: W

###############
#     EAU     #
###############

 - platform: rest
    resource: http://192.168.X.X/inst.json
    name: Legrand Ecocompteur Consommation Totale Eau m3
    value_template: '{{ value_json.data6m3 }}'
    unit_of_measurement: m3



##############################

# Tranche Tarifaire Brute
# Commande JSON Récupération Valeur Tarif Courant (HCHP)

  - platform: command_line
    name: Legrand tarif courant
    command: curl -s 'http://192.168.X.X/data.json' | jq '.tarif_courant'
    scan_interval: 10

# Template convertisseur Tarif Courant issu de la commande JSON > vers valeurs "Heures Creuses" / "Heures Pleines"
  - platform: template
    sensors:
      legrand_hchp:
        friendly_name: Legrand Heure Tarifaire
        entity_id: sensor.legrand_tarif_courant
        value_template: >
          {% if is_state('sensor.legrand_tarif_courant', '2') %}
            Pleine
          {% else %}
            Creuse
          {% endif %}

##############################

  - platform: command_line
    name: Legrand Intensité Souscrite
    command: curl -s 'http://192.168.X.X/data.json' | jq '.isousc'
    scan_interval: 60
    unit_of_measurement: A

  - platform: command_line
    name: Legrand Consommation Heures Creuses
    command: curl -s 'http://192.168.X.X/data.json' | jq '.conso_hc /1000 | floor'
    scan_interval: 10
    unit_of_measurement: kWh

  - platform: command_line
    name: Legrand Consommation Heures Pleines
    command: curl -s 'http://192.168.X.X/data.json' | jq '.conso_hp /1000 | floor'
    scan_interval: 10
    unit_of_measurement: kWh


# Calcul Consommation Totale (HP + HC)
  - platform: template
    sensors:
      legrand_conso_totale_hphc:
        friendly_name: "Legrand Consommation Totale"
        unit_of_measurement: "kWh"
        value_template: >-
          {{ float(states.sensor.legrand_consommation_heures_pleines.state) + float(states.sensor.legrand_consommation_heures_creuses.state) | round(2) }}


# Puissance Souscrite Compteur
  - platform: template
    sensors:
      legrand_puissance_souscrite:
        friendly_name: "Legrand Puissance Souscrite"
        value_template: >-
          {% set i = states('sensor.legrand_intensite_souscrite') | float %}
          {{ ( i / 5 ) | round(2) }}
        unit_of_measurement: 'kWA'
        icon_template: mdi:gauge




##    TEMPLATES SENSORS COÛTS FACTURATION ELECTRICITE

# Template Sensor Input Text Abonnement
  - platform: template
    sensors:
      energy_electricite_price_abo:
        friendly_name: "Legrand Prix Abo"
        unit_of_measurement: "€"
        value_template: >
          {{ states('input_text.energy_elec_price_abo_input') | round(2) }}
        icon_template: mdi:currency-eur

# Template Sensor Input Text Prix kwh Heures Pleines
  - platform: template
    sensors:
      energy_electricite_price_hp:
        friendly_name: "Legrand Prix kWh HP"
        unit_of_measurement: "€"
        value_template: >
          {{ states('input_text.energy_elec_price_kwh_hp_input') | round(2) }}
        icon_template: mdi:currency-eur

# Template Sensor Input Text  Prix Kwh Heures Creuses
  - platform: template
    sensors:
      energy_electricite_price_hc:
        friendly_name: "Legrand Prix kWh HC"
        unit_of_measurement: "€"
        value_template: >
          {{ states('input_text.energy_elec_price_kwh_hc_input') | round(2) }}
        icon_template: mdi:currency-eur


     ###########################################
     ##     CALCULS CONSOMMATIONS TOTALES     ##
     ###########################################

       #########################
       ##     ELECTRICITE     ##
       #########################

## TEMPLATES CALCULS FACTURATION ELECTRICITE

# Template Calcul Coût Quotidien
  - platform: template
    sensors:
      energy_daily_cost:
        friendly_name: Electricité Coût Quotdien
        value_template: >-
          {% set p = states('sensor.legrand_daily_energy_peak') | float %}
          {% set o = states('sensor.legrand_daily_energy_offpeak') | float %}
          {% set a = states('sensor.energy_electricite_price_abo') | float %}
          {% set h = states('sensor.energy_electricite_price_hp') | float %}
          {% set c = states('sensor.energy_electricite_price_hc') | float %}
          {{ ((a * 12 / 365 ) + (p * h) + (c * o)) | round(2) }}
        unit_of_measurement: '€'
        icon_template: mdi:currency-eur

# Template Calcul Coût Hebdomadaire
      energy_weekly_cost:
        friendly_name: Electricité Coût Hebdo
        value_template: >-
          {% set p = states('sensor.legrand_weekly_energy_peak') | float %}
          {% set o = states('sensor.legrand_weekly_energy_offpeak') | float %}
          {% set a = states('sensor.energy_daily_cost') | float %}
          {% set h = states('sensor.energy_electricite_price_hp') | float %}
          {% set c = states('sensor.energy_electricite_price_hc') | float %}
          {{ ((a * 7 ) + (p * h) + (c * o)) | round(2) }}
        unit_of_measurement: '€'
        icon_template: mdi:currency-eur

# Template Calcul Coût Mensuel
      energy_monthly_cost:
        friendly_name: Electricité Coût Mensuel
        value_template: >-
          {% set p = states('sensor.legrand_monthly_energy_peak') | float %}
          {% set o = states('sensor.legrand_monthly_energy_offpeak') | float %}
          {% set a = states('sensor.energy_electricite_price_abo') | float %}
          {% set h = states('sensor.energy_electricite_price_hp') | float %}
          {% set c = states('sensor.energy_electricite_price_hc') | float %}
          {{ (a + (p * h) + (c * o)) | round(2) }}
        unit_of_measurement: '€'
        icon_template: mdi:currency-eur

# Template Calcul Coût Annuel
      energy_yearly_cost:
        friendly_name: Electricité Coût Annuel
        value_template: >-
          {% set p = states('sensor.legrand_yearly_energy_peak') | float %}
          {% set o = states('sensor.legrand_yearly_energy_offpeak') | float %}
          {% set a = states('sensor.energy_electricite_price_abo') | float %}
          {% set h = states('sensor.energy_electricite_price_hp') | float %}
          {% set c = states('sensor.energy_electricite_price_hc') | float %}
          {{ ((a * 12) + (p * h) + (c * o)) | round(2) }}
        unit_of_measurement: '€'
        icon_template: mdi:currency-eur


# Template Calcul Consommation Totale Quotidienne ELECTRICITE
  - platform: template
    sensors:
      energy_daily_total:
        friendly_name: Consommation Quotidienne Totale
        value_template: >-
          {% set p = states('sensor.legrand_daily_energy_peak') | float %}
          {% set o = states('sensor.legrand_daily_energy_offpeak') | float %}
          {{ (o + p) | round(2) }}
        unit_of_measurement: 'kWh'
        icon_template: mdi:counter

# Template Calcul Consommation Totale Hebdo ELECTRICITE
  - platform: template
    sensors:
      energy_weekly_total:
        friendly_name: Consommation Hebdo Totale
        value_template: >-
          {% set p = states('sensor.legrand_weekly_energy_peak') | float %}
          {% set o = states('sensor.legrand_weekly_energy_offpeak') | float %}
          {{ (o + p) | round(2) }}
        unit_of_measurement: 'kWh'
        icon_template: mdi:counter

# Template Calcul Consommation Totale Mensuelle ELECTRICITE
  - platform: template
    sensors:
      energy_monthly_total:
        friendly_name: Consommation Mensuelle Totale
        value_template: >-
          {% set p = states('sensor.legrand_monthly_energy_peak') | float %}
          {% set o = states('sensor.legrand_monthly_energy_offpeak') | float %}
          {{ (o + p) | round(2) }}
        unit_of_measurement: 'kWh'
        icon_template: mdi:counter

# Template Calcul Consommation Totale Annuelle ELECTRICITE
  - platform: template
    sensors:
      energy_yearly_total:
        friendly_name: Consommation Annuelle Totale
        value_template: >-
          {% set p = states('sensor.legrand_yearly_energy_peak') | float %}
          {% set o = states('sensor.legrand_yearly_energy_offpeak') | float %}
          {{ (o + p) | round(2) }}
        unit_of_measurement: 'kWh'
        icon_template: mdi:counter

       #########################
       ##         EAU         ##
       #########################

# Template Calcul Consommation Totale Quotidienne EAU (LITRES)
  - platform: template
    sensors:
      water_daily_total:
        friendly_name: Consommation Quotidienne Totale Eau
        value_template: >-
          {% set p = states('sensor.legrand_daily_water_peak') | float %}
          {% set o = states('sensor.legrand_daily_water_offpeak') | float %}
          {{ (o + p) * 1000 | round(2) }}
        unit_of_measurement: 'l'
        icon_template: mdi:water

# Template Calcul Consommation Totale Hebdomadaire EAU (LITRES)
  - platform: template
    sensors:
      water_weekly_total:
        friendly_name: Consommation Hebdo Totale Eau
        value_template: >-
          {% set p = states('sensor.legrand_weekly_water_peak') | float | round(2) %}
          {% set o = states('sensor.legrand_weekly_water_offpeak') | float | round(1) %}
          {{ (o + p) * 1000 }}
        unit_of_measurement: 'l'
        icon_template: mdi:water

# Template Calcul Consommation Totale Mensuelle EAU (LITRES)
  - platform: template
    sensors:
      water_monthly_total:
        friendly_name: Consommation Mensuelle Totale Eau
        value_template: >-
          {% set p = states('sensor.legrand_monthly_water_peak') | float %}
          {% set o = states('sensor.legrand_monthly_water_offpeak') | float %}
          {{ (o + p) * 1000 | round(2) }}
        unit_of_measurement: 'l'
        icon_template: mdi:water

# Template Calcul Consommation Totale Annuelle EAU (LITRES)
  - platform: template
    sensors:
      water_yearly_total:
        friendly_name: Consommation Annuelle Totale Eau
        value_template: >-
          {% set p = states('sensor.legrand_yearly_water_peak') | float %}
          {% set o = states('sensor.legrand_yearly_water_offpeak') | float %}
          {{ (o + p) * 1000 | round(2) }}
        unit_of_measurement: 'l'
        icon_template: mdi:water


   #####################################
   ##                                 ##
   ##         UTILITY METER           ##
   ##                                 ##
   #####################################

utility_meter:


       #########################
       ##     ELECTRICITE     ##
       #########################


  legrand_daily_energy:
    source: sensor.legrand_conso_totale_hphc
    cycle: daily
    tariffs:
      - peak
      - offpeak
  legrand_weekly_energy:
    source: sensor.legrand_conso_totale_hphc
    cycle: weekly
    tariffs:
      - peak
      - offpeak
  legrand_monthly_energy:
    source: sensor.legrand_conso_totale_hphc
    cycle: monthly
    tariffs:
      - peak
      - offpeak
  legrand_yearly_energy:
    source: sensor.legrand_conso_totale_hphc
    cycle: yearly
    tariffs:
      - peak
      - offpeak



       #########################
       ##         EAU         ##
       #########################


  legrand_daily_water:
    source: sensor.legrand_ecocompteur_consommation_totale_eau_m3
    cycle: daily
    tariffs:
      - peak
      - offpeak
  legrand_weekly_water:
    source: sensor.legrand_ecocompteur_consommation_totale_eau_m3
    cycle: weekly
    tariffs:
      - peak
      - offpeak
  legrand_monthly_water:
    source: sensor.legrand_ecocompteur_consommation_totale_eau_m3
    cycle: monthly
    tariffs:
      - peak
      - offpeak
  legrand_yearly_water:
    source: sensor.legrand_ecocompteur_consommation_totale_eau_m3
    cycle: yearly
    tariffs:
      - peak
      - offpeak


   ############################################################
   ##                                                        ##
   ##               INPUT TEXT ENERGY PRICES                 ##
   ##                                                        ##
   ############################################################
   
input_text:

       ###############################
       ##       ELECTRICITE         ##
       ###############################

  energy_elec_price_abo_input:
    name: Prix Abo Mensuel Electricité

  energy_elec_price_kwh_hc_input:
    name: Prix Electricité kWh HC

  energy_elec_price_kwh_hp_input:
    name: Prix Electricité kWh HP






   ############################################################
   ##                                                        ##
   ##                       AUTOMATION                       ##
   ##                                                        ##
   ############################################################

automation:

       #########################
       ##     ELECTRICITE     ##
       #########################

  - alias: '[POWER] Passage Heures Pleines'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.legrand_tarif_courant
# Valeur 2 = Heures Pleines
        to: '2'
    action:
      - service: utility_meter.select_tariff
        data:
          entity_id:
            - utility_meter.legrand_daily_energy
            - utility_meter.legrand_weekly_energy
            - utility_meter.legrand_monthly_energy
            - utility_meter.legrand_yearly_energy
          tariff: peak

  - alias: '[POWER] Passage Heures Creuses'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.legrand_tarif_courant
# Valeur 1 = Heures Creuses
        to: '1'
    action:
      - service: utility_meter.select_tariff
        data:
          entity_id:
            - utility_meter.legrand_daily_energy
            - utility_meter.legrand_weekly_energy
            - utility_meter.legrand_monthly_energy
            - utility_meter.legrand_yearly_energy
          tariff: offpeak


       #########################
       ##         EAU         ##
       #########################


  - alias: '[POWER] Passage Heures Pleines Eau'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.legrand_tarif_courant
# Valeur 2 = Heures Pleines
        to: '2'
    action:
      - service: utility_meter.select_tariff
        data:
          entity_id:
            - utility_meter.legrand_daily_water
            - utility_meter.legrand_weekly_water
            - utility_meter.legrand_monthly_water
            - utility_meter.legrand_yearly_water
          tariff: peak

  - alias: '[POWER] Passage Heures Creuses Eau'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.legrand_tarif_courant
# Valeur 1 = Heures Creuses
        to: '1'
    action:
      - service: utility_meter.select_tariff
        data:
          entity_id:
            - utility_meter.legrand_daily_water
            - utility_meter.legrand_weekly_water
            - utility_meter.legrand_monthly_water
            - utility_meter.legrand_yearly_water
          tariff: offpeak

1 Like