Some calculations if min value is ... and max value is ... (I don't know how to explain)

Hi!

I have an issue in making something which I don’t know how to explain exactly, I hope someone can understand what I want, maybe he/she can help me with this…

I have a media volume entity with a range between 0.00 and 1.00 and I want to make a template sensor or just a variable which gives me the following result:

If the volume entity is at 0.00, the variable should give me 2.9, if the volume entitiy is at 1.00, the variable value should be 4… So… there is something about the minimum variable’s value which could be 2.9 and maximum which could be 4… this variable value should change according the volume entity position…

I hope someone will understand, thank you in advance!

This is what I have tried till now, but of course, this only calculate a range between 0 and 4…

{% set volume_level =  state_attr("media_player.dressing_speaker", "volume_level") | round(2) %}
{{ volume_level * 4 }}

You’re most of the way there

You want to multiply it by 4 - 2.9 (your range) and then add 2.9 (your minimum)

1 Like

And I tried some other formulas, but I never ended like this.

Thank you so much!

{{ (volume_level * (4 - 2.9)) + 2.9 }}
1 Like

@Tinkerer I have one more question regarding this topic…

What if the volume entity have a range between 0.05 and 1.00 ? :smiley: How can I “map” the values into my situation… If the volume entity is at value 0.05 to have the variable giving the 2.9 value and if is at 1.00 to get the variable as 4 ?

Here’s an example of how you fit a linear equation to two points: https://community.home-assistant.io/t/use-of-entity-values-in-compensation/617789/11?u=tom_l

First work out the slope (m) then the y intercept (c). You then have an equation y = mx + c that transforms one value to the other.

1 Like

I am trying to understand but it is far over me :frowning:

I am still trying :smiley:

You can always use this instead:

Just feed it your two points and it will do all the work.

1 Like

Yes, compensation integration should do the work fine but I wanted to not create another new entity… :expressionless:

Edit: in fact, I never used a compensation integration… now, seeng how is it, I don’t know if would work… The variable should know the min/max of the volume entity, but the value of the variable, should change automatically, according the volume entity value…

I am trying to see how this integration it is working regarding my issue

Edit2: Oh, yes, now I got it how it is working… My bad. But still, the perfect solution for me, should be without creating another new entity (as the compensation it is doing)

I got into this till now, but not the full perfect result…

{% set volume_level = state_attr("media_player.bedroom_speaker","volume_level") | round(2) %}

{% set range = 1 - 0.05 %}
{% set vol_calc = volume_level - 0.05 %}
{{ ((vol_calc * (6 - 3.4)) + 3.4 | float ) | round (2)  }}

With this, If I set the min value of the volume entity (0.05), the result of the variable it is ok (3.4), but if I put the max value for the volume entity (1.00), the result of the variable it is not what I wanted (6.0), it is (5.87) …

Any help? :frowning:

Edit: I’m stupid… I forgot to add a small calculation line, now it is ok :))

{% set volume_level = state_attr("media_player.bedroom_speaker","volume_level") | round(2) %}

{% set range = 1 - 0.05 %}
{% set vol_calc = volume_level - 0.05 %}
{% set perc = (vol_calc) / range %}
{{ ((perc * (6 - 3.4)) + 3.4 | float ) | round (2)  }}

Your two points (x, y)

(0.5,2.9)
(1,4)

You want an equation of the form y = mx + c

m = (y2 -y1) / (x2 - x1)
m = (4 - 2.9) / (1 - 0.5)
m = 2.2

c = y1 - mx1
c = 2.9 - 2.2 * 0.5
c = 1.8

→ y = 2.2x - 1.8

1 Like

I done it, finally…
100% In the next life, I won’t miss math hours at school anymore :smiley:

All these to make a dynamic “Riemann sum” integration, for a device which does not have energy monitoring.

The device is a Google Home device and the consumption changes (of course) according the volume, if is lower, higher or nothing plays… So I needed to make an automation which change the value of the Riemann sum sensor, according the state of the device, in order to know the consumption and to have it into the energy dashboard…

If anyone it is interested, this is the automation:

alias: Data2Energy - Google Home Bedroom
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.sonoff_google_home_bedroom
    to: "on"
    id: switch_on
    alias: Google Home Switch to On
  - platform: state
    entity_id:
      - switch.sonoff_google_home_bedroom
    to: "off"
    id: switch_off
    alias: Google Home Switch to Off
  - platform: state
    entity_id:
      - media_player.bedroom_speaker
    to: playing
    id: playing
    alias: Playing
  - platform: state
    entity_id:
      - media_player.bedroom_speaker
    from: playing
    id: not_playing
    alias: Not Playing
  - platform: state
    entity_id:
      - media_player.bedroom_speaker
    attribute: volume_level
    id: volume_change
    alias: Volume Changes
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - switch_on
        sequence:
          - service: variable.update_sensor
            data:
              value: 2.6
            target:
              entity_id: sensor.variable_bedroom_google_kwh
      - conditions:
          - condition: trigger
            id:
              - switch_off
        sequence:
          - service: variable.update_sensor
            data:
              value: 0
            target:
              entity_id: sensor.variable_bedroom_google_kwh
    alias: Switch on/off
  - alias: Playing / Not playing
    choose:
      - conditions:
          - condition: trigger
            id:
              - playing
          - condition: template
            value_template: >-
              {{ state_attr("media_player.bedroom_speaker","volume_level") > 0
              }}
        sequence:
          - service: variable.update_sensor
            data:
              value: >
                {% set volume_level = 
                state_attr("media_player.bedroom_speaker",
                "volume_level") | round(2) %}
                {% set range = 1 - 0.01 %}
                {% set vol_calc = volume_level - 0.01 %}
                {% set perc = (vol_calc) / range %}
                {{ ((perc * (6 - 3.4)) + 3.4 | float ) | round (2) }}
            target:
              entity_id: sensor.variable_bedroom_google_kwh
      - conditions:
          - condition: trigger
            id:
              - not_playing
        sequence:
          - service: variable.update_sensor
            data:
              value: 2.6
            target:
              entity_id: sensor.variable_bedroom_google_kwh
  - alias: Volume Changes
    if:
      - condition: trigger
        id:
          - volume_change
      - condition: state
        entity_id: media_player.bedroom_speaker
        state: playing
    then:
      - choose:
          - conditions:
              - condition: template
                value_template: >-
                  {{ state_attr("media_player.bedroom_speaker","volume_level") >
                  0 }}
            sequence:
              - service: variable.update_sensor
                data:
                  value: >
                    {% set volume_level = 
                    state_attr("media_player.bedroom_speaker",
                    "volume_level") | round(2) %}
                    {% set range = 1 - 0.01 %}
                    {% set vol_calc = volume_level - 0.01 %}
                    {% set perc = (vol_calc) / range %}
                    {{ ((perc * (6 - 3.4)) + 3.4 | float ) | round (2) }}
                target:
                  entity_id: sensor.variable_bedroom_google_kwh
          - conditions:
              - condition: template
                value_template: >-
                  {{ state_attr("media_player.bedroom_speaker","volume_level")
                  == 0 }}
            sequence:
              - service: variable.update_sensor
                data:
                  value: 2.6
                target:
                  entity_id: sensor.variable_bedroom_google_kwh
mode: parallel
max: 5

The “Riemann sum” sensor, it is getting the value from the variable, making the sensor act how it should.

I bet that there are far more ways to achieve this, but this is the most easy way for me to do…

Thank you both for helping me. Have a good night!