Formulas for variables

Hello,
I am new to HA and I am trying to migrate from Homey to HA.
I can set the value of two variables I have defined.
Now I need to substract them multiply by 3600000 and devide by value Y
Value Y needs to be calculated as the difference between 900 seconds and the remaining seconds on a countdown timer.
Also need tome help to calculate value Y.
I could set the time(now) when the first automation starts and save that in a variable and then substract that from the time(now) when this automation runs.

This is the code I already have:

description: ""
trigger:
  - platform: state
    entity_id:
      - timer.piek_couter1
    to: idle
condition: []
action:
  - service: var.set
    data:
      entity_id: var.total_usage_lopend
      value: "{{ states('sensor.p1_meter_5c2faf055da6_total_power_import') }}"
  - service: timer.start
    data: {}
    target:
      entity_id: timer.piek_couter1
  - service: var.set
    data:
      entity_id: var.kwartierpiek_current
      value: "{{ (states('var.total_usage_lopend')) - (states('var.total_usage_begin_lopend')) * 3600 / 'Y'}}
      mode: single

I also could skip setting value of var.total_usage_lopend and use it directly in the calculation string

Thx!

What’s your goal here? This is completely the opposite of how HA is intended to be used. In most cases you don’t need to use that custom variable integration.

Hello Pedro,

At the beginning of each 15 minutes I want to record the value of my P1 meter.
Then at every minute I want to calculate my powerconsuption and what would be the outcome if all consumption remains the same for that 15 minutes.
That would allow me to try and reduce my total power consumption with 15 minutes, when the prediction is too high in relation to a predefined value.

I already have this

 value:  >
     {% set begin = states('var.total_usage_begin_lopend') | float %}
     {% set lopend = states('var.total_usage_lopend') | float %}

     {{ ((lopend - begin) *3600000) | round(1, default=0)}}

Now I need to work on value Y that calculates the elapsed time in that 15 minutes window.

If I get what you’re trying to do, just trigger on the power sensor when it’s above a certain value for a certain time. Look at the numeric state trigger here.