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

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

Thanks for your sharing @maxtone66, it should help many people who are using HA.
I will share your code on the dedicated EcoCompteur topic.

Yes of course. You’re welcome

@maxtone66,

Could you share some screenshots in order to see the final result ?

Well I realized there were some small bugs on my package so here is the update.

Secrets.yaml

Legrand_Source_Data: http://192.168.X.X/data.json
Legrand_Source_Instantanes: http://192.168.X.X/inst.json
Legrand_Cmd_Tarif_Courant: curl -s 'http://192.168.X.X/data.json' | jq '.tarif_courant'
Legrand_Cmd_Intensite_Souscrite: curl -s 'http://192.168.X.X/data.json' | jq '.isousc'
Legrand_Cmd_Conso_HC: curl -s 'http://192.168.X.X/data.json' | jq '.conso_hc /1000 | floor'
Legrand_Cmd_Conso_HP: curl -s 'http://192.168.X.X/data.json' | jq '.conso_hp /1000 | floor'

package_legrand.yaml

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

  customize:

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

    ##########################################
    ##   Personnalisation Sensors Circuits   #
    ##########################################

# Circuit 1 = label_entree1
    sensor.legrand_puissance_circuit_1:
      <<: *customize
      friendly_name: "Legrand Puissance Totale Maison"
      icon: mdi:alpha-w-circle-outline

# Circuit 2 = label_entree2
    sensor.legrand_puissance_circuit_2:
      <<: *customize
      friendly_name: "Legrand Puissance Climatisation"
      icon: mdi:alpha-w-circle-outline

# Circuit 3 = label_entree3
    sensor.legrand_puissance_circuit_3:
      <<: *customize
      friendly_name: "Legrand Puissance Cumulus"
      icon: mdi:alpha-w-circle-outline

# Circuit 4 = label_entree4
    sensor.legrand_puissance_circuit_4:
      <<: *customize
      friendly_name: "Legrand Puissance Prises 1"
      icon: mdi:alpha-w-circle-outline

# Circuit 5 = label_entree5
#    sensor.legrand_puissance_circuit_5:
#      <<: *customize
#      friendly_name: "Legrand Conso. Prises 2"
#      icon: mdi:alpha-w-circle-outline



    #################################################################
    ##   Personnalisation Sensors Tableau Consommation Electrique   #
    #################################################################


    sensor.legrand_hchp:
      <<: *customize
      friendly_name: "Legrand Tarif en Cours"
      icon: mdi:av-timer


    sensor.legrand_intensite_souscrite:
      <<: *customize
      friendly_name: "Legrand Intensité Souscrite"
      icon: mdi:alpha-w-circle-outline


    sensor.legrand_puissance_souscrite:
      <<: *customize
      friendly_name: "Legrand Puissance Abonnement"
      icon: mdi:alpha-w-circle-outline


    ##########################################
    ##   Personnalisation Sensors Circuits   #
    ##########################################


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


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


    sensor.legrand_tarif_en_cours:
      <<: *customize
      friendly_name: "Legrand Indicateur Tarif en Cours"
      icon: mdi:counter

    ###########################################
    ##   Personnalisation Conso Electricité   #
    ###########################################

    sensor.legrand_conso_totale:
      <<: *customize
      friendly_name: "Legrand Conso. Totale"
      icon: mdi:counter

# Personnalisation Electricité Utility Meter

    sensor.energy_daily_cost:
      <<: *customize
      friendly_name: "Legrand Conso. Electr. Quotid."
      icon: mdi:counter

    sensor.energy_weekly_cost:
      <<: *customize
      friendly_name: "Legrand Conso. Electr. Hebdo."
      icon: mdi:counter

    sensor.energy_monthly_cost:
      <<: *customize
      friendly_name: "Legrand Conso. Electr. Mensuelle"
      icon: mdi:counter

    sensor.energy_yearly_cost:
      <<: *customize
      friendly_name: "Legrand Conso. Electr. Annuelle"
      icon: mdi:counter




    ###################################
    ##   Personnalisation Conso Eau   #
    ###################################

# Compteur Eau
    sensor.legrand_consommation_totale_eau_m3:
      <<: *customize
      friendly_name: "Legrand Conso. Totale Eau"
      icon: mdi:water


# Personnalisation Eau Utility Meter

    sensor.water_daily_total:
      <<: *customize
      friendly_name: "Legrand Conso. Eau Quotid."
      icon: mdi:water

    sensor.water_weekly_total:
      <<: *customize
      friendly_name: "Legrand Conso. Eau Hebdo."
      icon: mdi:water

    sensor.water_monthly_total:
      <<: *customize
      friendly_name: "Legrand Conso. Eau Mensuelle"
      icon: mdi:water

    sensor.water_yearly_total:
      <<: *customize
      friendly_name: "Legrand Conso. Eau Annuelle"
      icon: mdi:water

   #####################################
   ##                                 ##
   ##        SENSORS PUISSANCES       ##
   ##           INSTANTANEES          ##
   ##                                 ##
   #####################################

sensor:

#     ELECTRICITE


# Commande Récupération Valeur Puissances Instantanées Circuit 1
  - platform: rest
    resource: !secret Legrand_Source_Instantanes
    name: Legrand Puissance Circuit 1
    value_template: '{{ value_json.data1| round(0) }}'
    device_class: power
    unit_of_measurement: W

# Commande Récupération Valeur Puissances Instantanées Circuit 2

  - platform: rest
    resource: !secret Legrand_Source_Instantanes
    name: Legrand Puissance Circuit 2
    value_template: '{{ value_json.data2| round(0) }}'
    device_class: power
    unit_of_measurement: W



# Commande Récupération Valeur Puissances Instantanées Circuit 3
  - platform: rest
    resource: !secret Legrand_Source_Instantanes
    name: Legrand Puissance Circuit 3
    value_template: '{{ value_json.data3| round(0) }}'
    device_class: power
    unit_of_measurement: W


# Commande Récupération Valeur Puissances Instantanées Circuit 4
  - platform: rest
    resource: !secret Legrand_Source_Instantanes
    name: Legrand Puissance Circuit 4
    value_template: '{{ value_json.data4| round(0) }}'
    device_class: power
    unit_of_measurement: W

# Commande Récupération Valeur Puissances Instantanées Circuit 5
#  - platform: rest
#    resource: !secret Legrand_Source_Instantanes
#    name: Legrand Puissance Circuit 5
#    value_template: '{{ value_json.data5| round(0) }}'
#    device_class: power
#    unit_of_measurement: W



#     EAU


# Commande Récupération Valeur Volume Conso Total Eau (m3) 
  - platform: rest
    resource: !secret Legrand_Source_Instantanes
    name: Legrand Consommation Totale Eau m3
    value_template: '{{ value_json.data6m3 }}'
    unit_of_measurement: m3



   #####################################
   ##                                 ##
   ##       SENSORS & TEMPLATES       ##
   ##    CONSOMMATIONS ELECTRIQUE     ##
   ##                                 ##
   #####################################

# Tarif en Cours Donnée Brute
# Valeurs : 2 = Pleine / 1 = Creuse
  - platform: command_line
    name: Legrand Tarif en Cours
    command: !secret Legrand_Cmd_Tarif_Courant
    scan_interval: 10

# Transformation Tarif en Cours > Tranche Tarifaire
  - platform: template
    sensors:
      legrand_hchp:
        entity_id: sensor.legrand_tarif_en_cours
        value_template: >
          {% if is_state('sensor.legrand_tarif_en_cours', '2') %}
            Pleine
          {% else %}
            Creuse
          {% endif %}


   #####################################
   ##                                 ##
   ##             RELEVES             ##
   ##           ABONNEMENT            ##
   ##                                 ##
   #####################################

# Intensité Souscrite
  - platform: command_line
    name: Legrand Intensité Souscrite
    command: !secret Legrand_Cmd_Intensite_Souscrite
    scan_interval: 60
    unit_of_measurement: A

# Puissance Souscrite
  - 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: 'kVA'
        icon_template: mdi:gauge

# Conso. HC
  - platform: command_line
    name: Legrand Consommation HC
    command: !secret Legrand_Cmd_Conso_HC
    scan_interval: 10
    unit_of_measurement: kWh

# Conso. HP
  - platform: command_line
    name: Legrand Consommation HP
    command: !secret Legrand_Cmd_Conso_HP
    scan_interval: 10
    unit_of_measurement: kWh






   #####################################
   ##                                 ##
   ##         SENSORS CALCULS         ##
   ##           CONSOMMATION          ##
   ##                                 ##
   #####################################


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



   #####################################
   ##                                 ##
   ##          SENSORS COUTS          ##
   ##      FACTURATION ELECTRICITE    ##
   ##                                 ##
   #####################################

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

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

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






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

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

## CALCULS FACTURATION ELECTRICITE

# 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.electricite_prix_abo') | float %}
          {% set h = states('sensor.electricite_prix_kwh_hp') | float %}
          {% set c = states('sensor.electricite_prix_kwh_hc') | float %}
          {{ ((a * 12 / 365 ) + (p * h) + (c * o)) | round(2) }}
        unit_of_measurement: '€'
        icon_template: mdi:currency-eur

# 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.electricite_prix_kwh_hp') | float %}
          {% set c = states('sensor.electricite_prix_kwh_hc') | float %}
          {{ ((a * 7 ) + (p * h) + (c * o)) | round(2) }}
        unit_of_measurement: '€'
        icon_template: mdi:currency-eur

# 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.electricite_prix_abo') | float %}
          {% set h = states('sensor.electricite_prix_kwh_hp') | float %}
          {% set c = states('sensor.electricite_prix_kwh_hc') | float %}
          {{ (a + (p * h) + (c * o)) | round(2) }}
        unit_of_measurement: '€'
        icon_template: mdi:currency-eur

# 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.electricite_prix_abo') | float %}
          {% set h = states('sensor.electricite_prix_kwh_hp') | float %}
          {% set c = states('sensor.electricite_prix_kwh_hc') | float %}
          {{ ((a * 12) + (p * h) + (c * o)) | round(2) }}
        unit_of_measurement: '€'
        icon_template: mdi:currency-eur



# Calcul Consommation TOTALE Quotidienne ELECTRICITE
  - platform: template
    sensors:
      energy_daily_total:
        friendly_name: Electricité 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

# Calcul Consommation TOTALE Hebdo ELECTRICITE
  - platform: template
    sensors:
      energy_weekly_total:
        friendly_name: Electricité 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

# Calcul Consommation TOTALE Mensuelle ELECTRICITE
  - platform: template
    sensors:
      energy_monthly_total:
        friendly_name: Electricité 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

# Calcul Consommation TOTALE Annuelle ELECTRICITE
  - platform: template
    sensors:
      energy_yearly_total:
        friendly_name: Electricité 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         ##
       #########################

# Calcul Consommation Totale Quotidienne EAU (LITRES)
  - platform: template
    sensors:
      water_daily_total:
        friendly_name: Eau Consommation Quotidienne Totale
        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'


# Calcul Consommation Totale Hebdomadaire EAU (LITRES)
  - platform: template
    sensors:
      water_weekly_total:
        friendly_name: Eau Consommation Hebdo Totale
        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'


# Calcul Consommation Totale Mensuelle EAU (LITRES)
  - platform: template
    sensors:
      water_monthly_total:
        friendly_name: Eau Consommation Mensuelle Totale
        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'


# Calcul Consommation Totale Annuelle EAU (LITRES)
  - platform: template
    sensors:
      water_yearly_total:
        friendly_name: Eau Consommation Annuelle Totale
        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'








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

utility_meter:


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


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



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


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








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

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

  legrand_prix_abo:
    name: Electricité Prix Abo Mensuel

  legrand_prix_kwh_hc:
    name: Electricité Prix kWh HC

  legrand_prix_kwh_hp:
    name: Electricité Prix kWh HP






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

automation:

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

  - alias: '[ELECTRICITE] Passage Heures Pleines'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.legrand_tarif_en_cours
# 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: '[ELECTRICITE] Passage Heures Creuses'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.legrand_tarif_en_cours
# 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: '[EAU] Passage Heures Pleines'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.legrand_tarif_en_cours
# 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: '[EAU] Passage Heures Creuses'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.legrand_tarif_en_cours
# 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

And here are the new screenshots:

2 Likes

Thank you so much for sharing this.
I have updated a lit bit your code.
You call 5 times the rest service te get “Puissances Instantanées”.
You could do it only once, and so get them more often.

Here is an example :

sensor:
  - platform: rest
    resource: !secret Legrand_Source_Instantanes
    value_template: "{{ value_json.Date_Time }}"
    name: legrand_source_instantanes
    scan_interval: 5
    json_attributes:
      - data1
      - data2
      - data3
      - data4
      - data5

  - platform: template
    sensors:
      legrand_data1:
        value_template: >
          {{ state_attr('sensor.legrand_source_instantanes','data1') | round(0) }}
        device_class: power
        unit_of_measurement: W
        friendly_name: "Legrand Puissance Data1"
      legrand_data2:
        value_template: >
          {{ state_attr('sensor.legrand_source_instantanes','data2') | round(0) }}
        device_class: power
        unit_of_measurement: W
        friendly_name: "Legrand Puissance Data2"
      legrand_data3:
        value_template: >
          {{ state_attr('sensor.legrand_source_instantanes','data3') | round(0) }}
        device_class: power
        unit_of_measurement: W
        friendly_name: "Legrand Puissance Data3"
      legrand_data4:
        value_template: >
          {{ state_attr('sensor.legrand_source_instantanes','data4') | round(0) }}
        device_class: power
        unit_of_measurement: W
        friendly_name: "Legrand Puissance Data4"
      legrand_data5:
        value_template: >
          {{ state_attr('sensor.legrand_source_instantanes','data5') | round(0) }}
        device_class: power
        unit_of_measurement: W
        friendly_name: "Legrand Puissance Data5"
2 Likes

hello,
first of all I would like to thank you for your work on the eco-counter and your code sharing for HA.
I am a beginner, so please be indulgent …
your code works fine if i copy / paste package_legrand.yaml directly to configuration.yaml. My problem is that I can not create a line of code to call a package or other, I always have errors “because” of your code too complicated for me;). I do not know how you did it but if you can help me even if it is not too related to the eco-meter.
Thanking you in advance and sorry for the translation

Hi @JBHA1,

Everyone must have started one day so no problem.

Well instead of adding this code directly into the configuration.yaml file you can just put this into :

packages: !include_dir_named packages

Then create a folder ‘packages’ in your root folder and put inside a file ‘package_legrand.yaml’ with his code.

If you want more exemples you can read this : Packages - Home Assistant

Good luck

Hello,
thank you for your comeback. indeed, I had already tried to do as you described it to me … but I got a message "integration error: packages - intregration ‘packages not found’ ‘. I added’ home assistant 'above line and nothing found on Google … Sorry to bother you with my little bugs and thank you for helping me.

Bonjour Maxtonne66 j’ai utilisé ton script pour la gestion de l’éco compteur et te remercie pour le travail effectué !!!
J’ai une subtilité chez moi je suis en offre TEMPO avec 6 tarifs différents et là du coup toutes les formules de calcul ne fonctionnent plus.

Offre Tempo=6 tarifs
Heures pleines bleu
Heures creuses bleu
Heures pleines blanc
Heures creuses blanc
Heures pleines rouge
Heure creuses rouge

Si tu pouvais m’aider (ainsi que la communauté car cette offre revient à la mode)

Merci à toi

Hi @maxtone66
Is it your last version of the scripts ?
Did you take into account the suggested optimisation ?
Is there any code for the dashboard ?
Thank you for you help.
Lyes

Hi @ablyes
I don’t use this device for over 2 years since I sold the house, so I won’t be helpful.
I use HA now in an RV.
Maybe you’ll get an answer from someone else who still uses it…
Good luck