How to limit calculations for several entity derives from one value using templates

Hello Everyone.
This is my frist post so please excuse any lack of etiquette that I might not be familiar with.

I’m looking to create multiple entities (‘regular sensors’) from a single value.
Long story short, we have a system to calculate the price of power based on type of day and hour in the day.

  • 3 types of day (bleu, blanc rouge) → Guess the country :slight_smile:
  • 2 types of hours for each (low-cost, high cost)
    => For a total of 6 kWhr prices

The idea is to create 4 entities giving me the following values abse on the “power” of an équipement.

  1. Price (in €) for a day “bleu”
  2. Price (in €) for a day “blanc”
  3. Price (in €) for a day “rouge”
  4. Price (in €) for a whole year (300 days “blanc” + 43 days “bleu” + 22 days “red”

This is my current template test in dev tools (woring great !!! ) :heart_eyes:

But if I want all thoses values, I need to create 4 sensors.
Is there a way to avoid having to retype all those variables dfinitions and définitions for each sensor ?

{## Initate available variables: ##}
{% set pwr = states('input_number.tempo_calc')| int %}
{% set bleu_HC = 10.56 %}
{% set bleu_HP = 13.69 %}
{% set blan_HC = 12.46 %}
{% set blan_HP = 16.54 %}
{% set roug_HC = 13.28 %}
{% set roug_HP = 73.24 %}
{% set heur_HC = 16.00 %}
{% set heur_HP =  8.00 %}
{% set nb_bleu = 300.0 %}
{% set nb_blan =  43.0 %}
{% set nb_roug =  22.0 %}

{## Calcul du cout / jour pour chaque type de jour ##} 
{% set cout_jour_bleu =  pwr * (bleu_HC * heur_HC + bleu_HP + heur_HP) /100000 %}
{% set cout_jour_blan =  pwr * (blan_HC * heur_HC + blan_HP + heur_HP) /100000 %}
{% set cout_jour_roug =  pwr * (roug_HC * heur_HC + roug_HP + heur_HP) /100000 %}

{## Calcul du cout annuel ##} 
{% set cout_annee = cout_jour_bleu * nb_bleu + cout_jour_blan * nb_blan + cout_jour_roug * nb_roug %}

Puissance : {{ pwr }} W.
Jour bleu  : {{ cout_jour_bleu | round(2) }} €
Jour blanc : {{ cout_jour_blan | round(2) }} €
Jour rouge : {{ cout_jour_roug | round(2) }} €
COUT ANNUEL : {{ cout_annee | round(2) }} €

HI, I am aware of the system (also in France but not using this). Have you already tried posting in the french HACF forum as they may have a solution already ?

Hello,
No, I’m used to deal with english stuff rather than the sometimes limited ressources in french.
I’ll try to have a look.
Have a good day

It is not that bad but yes…logically (?) a lot less people on the french forum and 2(!) discord channels. Just suggesting as one them may already have a solution that fits.

Macros are good to go

Thanks, This is exactly that I was looking for !!!
I just finished my design. I’ll pos it here if it helps others, but mostly for me to find it back later :- :rofl:

Estimate the cost /day and /year for a tempo appliance based on it’s power
Estimatation du cout journalier et annuel pour un appareil en fonction de sa puissance
Screenshot 2023-08-18 104552

## tempo_cost.yaml (loaded in configuration.yaml as a template integration)

- sensor:
  - name: "Cout jour Bleu"
    unique_id: "tempo.cout.bleu"
    device_class: monetary
    unit_of_measurement: "€"
    state: >
      {% from 'tempo.jinja' import cout_tempo %}
      {{ cout_tempo('input_number.tempo_calc','bleu') }}
- sensor:
  - name: "Cout jour Blanc"
    unique_id: "tempo.cout.blanc"
    device_class: monetary
    unit_of_measurement: "€"
    state: >
      {% from 'tempo.jinja' import cout_tempo %}
      {{ cout_tempo('input_number.tempo_calc','blanc') }}
- sensor:
  - name: "Cout jour Rouge"
    unique_id: "tempo.cout.rouge"
    device_class: monetary
    unit_of_measurement: "€"
    state: >
      {% from 'tempo.jinja' import cout_tempo %}
      {{ cout_tempo('input_number.tempo_calc','rouge') }}
- sensor:
  - name: "Cout jour Année"
    unique_id: "tempo.cout.annee"
    device_class: monetary
    unit_of_measurement: "€"
    state: >
      {% from 'tempo.jinja' import cout_tempo %}
      {{ cout_tempo('input_number.tempo_calc' ,'annee') }}

## unique id is used in order to customize the number of digits to display in the GUI

And the Jinja Macro file

## custom_templates\tempo.jinja

{% macro cout_tempo(power, type) %}
  {% set pwr = states(power) | int %}
  {% set bleu_HC = 10.56 %}
  {% set bleu_HP = 13.69 %}
  {% set blan_HC = 12.46 %}
  {% set blan_HP = 16.54 %}
  {% set roug_HC = 13.28 %}
  {% set roug_HP = 73.24 %}
  {% set heur_HC = 16.00 %}
  {% set heur_HP =  8.00 %}
  {% set nb_bleu = 300.0 %}
  {% set nb_blan =  43.0 %}
  {% set nb_roug =  22.0 %}

  {% set cout_jour_bleu =  pwr * (bleu_HC * heur_HC + bleu_HP * heur_HP) /100000 %}
  {% set cout_jour_blan =  pwr * (blan_HC * heur_HC + blan_HP * heur_HP) /100000 %}
  {% set cout_jour_roug =  pwr * (roug_HC * heur_HC + roug_HP * heur_HP) /100000 %}
  {% set cout_annee = cout_jour_bleu * nb_bleu + cout_jour_blan * nb_blan + cout_jour_roug * nb_roug %}

  {%   if type == 'bleu'  %} 
    {{ cout_jour_bleu }}
  {% elif type == 'blanc' %} 
    {{ cout_jour_blan }}
  {% elif type == 'rouge' %} 
    {{ cout_jour_roug }}
  {% elif type == 'annee' %} 
    {{ cout_annee }}
  {% else %} 
    {{ -1 }}
  {% endif %}
{% endmacro %}

In my example, I used an input_number HELPER named tempo.calc to intiate the calculations. It stores the value in watts and is changed in the dashboard with a slider.
Because the cost is linear, I prefered to have more control from 0 to 100 and multiply the result by powers of ten.
For example : 1500 W is 100 * 15 so I get the cost or 15 watts and multiply by 100

NOTE
For reference, here is the yaml code for the vertical stack of the dashboard.
It needs mushroom_cards and card_mod installed

type: vertical-stack
cards:
  - type: custom:mushroom-number-card
    entity: input_number.tempo_calc
    name: Puissance en Watt
    icon_color: lime
    fill_container: true
    secondary_info: state
    display_mode: slider
  - type: entities
    entities:
      - entity: sensor.cout_jour_bleu
        style: |
          :host {
            --card-mod-icon-color: Dodgerblue;
            color: Dodgerblue;
            font-size: 90%
          }
      - entity: sensor.cout_jour_blanc
        style: |
          :host {
            --card-mod-icon-color: azure;
            color: azure;
            font-size: 90%
          }
      - entity: sensor.cout_jour_rouge
        style: |
          :host {
            --card-mod-icon-color: Crimson;
            color: Crimson;
          }
      - entity: sensor.cout_jour_annee
        style: |
          :host {
            --card-mod-icon-color: darkkhaki;
            color: darkkhaki;
            font-weight: bold;
            font-size: 120%
          }
  - type: markdown
    content: |-
      *Note :*
      -
      Le cout de l'éléctricité est linéaire. 
      ***200W** coûte **10x** plus cher que **20W***
      Pour **2500W** par exemple, choisir **25W** puis faire **x100**

Please ask if you have any questions.