Adding multiple sensors values into one!

Hello all,

My first post in this community. Mostly noobish person here so if my questions sound wrong my apologizes in advance.

I have HA up and running. Devices added. Everything is there. What I am struggling with is creating templates/sensors from multiple sensor values. I have tried copying/mimicking many guides to no avail.

For example from entities:
sensor value 1: sensor.inverter1qpigs_pv1_charging_power
sensor value 2: sensor.inverter2qpigs_pv1_charging_power

How do I go about adding these values together to create new sensor? Is more information needed?

Thank you all,

I suggest using the modern sensor format. This YAML goes into your configuration.yaml-

template:
  - sensor:
      - name: "Total Charging Power"
        unit_of_measurement: "W"
        device_class: power
        state: >
          {{ [ states('sensor.inverter1qpigs_pv1_charging_power'), 
               states('sensor.inverter2qpigs_pv1_charging_power') ]
               | map('float') | sum }}
        availability: >
          {{ not 'unavailable' in 
             [ states('sensor.inverter1qpigs_pv1_charging_power'), 
               states('sensor.inverter2qpigs_pv1_charging_power') ] }}

Try pasting the following template in Developer Tools → Template → If it is displayed correctly, proceed with the sensor creation.

          {{ [ states('sensor.inverter1qpigs_pv1_charging_power'), 
               states('sensor.inverter2qpigs_pv1_charging_power') ]
               | map('float') | sum }}
5 Likes

Excellent. Thank you. That works under developer->template. How do I go and create sensor? Under coniguration.yaml. Sorry for asking noob questions?

Yes, copy and paste the complete template sensor in your configuration.yaml. If this is your first time creating template sensor, you will need to restart Home Assistant from Configuration → Server Control → Restart.

Next time, if you add more template sensor you can just reload Template Entities from Configuration → Server Control → YAML configuration reloading → Template Entities.

copy paste everything except:

 availability: >
          {{ not 'unavailable' in 
             [ states('sensor.inverter1qpigs_pv1_charging_power'), 
               states('sensor.inverter2qpigs_pv1_charging_power') ] }}

Does it work? You should have a new entity_id of sensor.total_charging_power.

I have copy pasted the following into configuration.yaml

template:
  - sensor:
      - name: "Total Charging Power"
        unit_of_measurement: "W"
        device_class: power
        state: >
          {{ [ states('sensor.inverter1qpigs_pv1_charging_power'), 
               states('sensor.inverter2qpigs_pv1_charging_power') ]
               | map('float') | sum }}
        availability: >
          {{ not 'unavailable' in 
             [ states('sensor.inverter1qpigs_pv1_charging_power'), 
               states('sensor.inverter2qpigs_pv1_charging_power') ] }}

It does work. I do have new sensor named total_charging-power

Can I use this template for other sensors? I have bunch of them that I need to do the same thing with.

Thank you so much,

1 Like

Yes. The format should look like the following-

template:
  - sensor:
      - name: "Total Charging Power"
        unit_of_measurement: "W"
        device_class: power
        state: >
          {{ [ states('sensor.inverter1qpigs_pv1_charging_power'), 
               states('sensor.inverter2qpigs_pv1_charging_power') ]
               | map('float') | sum }}
        availability: >
          {{ not 'unavailable' in 
             [ states('sensor.inverter1qpigs_pv1_charging_power'), 
               states('sensor.inverter2qpigs_pv1_charging_power') ] }}

      - name: "xxxx"
        unit_of_measurement: "W"
        device_class: power
        state: >
          {{ [ states('sensor.xyz'), 
               states('sensor.abc') ]
               | map('float') | sum }}
        availability: >
          {{ not 'unavailable' in 
             [ states('sensor.xyz'), 
               states('sensor.abc') ] }}
3 Likes

Like you read my mind :grin: That was about to be my next question.

Thanks again. There will be more questions for sure.

The format for the template sensor should look like-

template:
  - sensor:
      - name: "Average temperature"
        unit_of_measurement: "°C"
        state: >

      - name: "Average temperature"
        unit_of_measurement: "°C"
        state: >

  - binary_sensor:
      - name: "Has Unavailable States"
        state: "{{ states.sensor }}"

      - name: "Has Unavailable States"
        state: "{{ states.sensor }}"
1 Like

difference between sensor and binary sensor? Where do you use one vs. another?

Thanks

Sensor can have various types of value, while binary_sensor can only be either on or off.

1 Like

Just as a maintenance helper you can create a separate file for the templates in the config directory.

Add the following to configuration.yaml

template: !include "yourtemplatefile".yaml

the only thing different is you don’t put “template:” in the “yourtemplatefile”.yaml as that is defined in configuration.yaml.

Great idea!
You can see here how my template.yaml is split (Frenck’s modular style). It seems a bit of a folder hunting, but I like it to be tidy :grin:

Thank you for the help yesterday. I got the hang of adding up sensor values. Now I have different question.

I have entity sensor.shunt_instantaneous_power which has both positive and negative values. I am trying to create new sensor which will display only positive values or that entity or 0 for negative values. Is that possible?

template:
  - sensor:
      - name: "Shunt Power"
        unit_of_measurement: "W"
        state: >-
          {{ ([0, states('sensor.shunt_instantaneous_power')|int, 1000] |sort) [1] }}

You can check out Taras’ explanation on how it works here-

1 Like

Thank you again. In above template 1000 can be any number I would assume? Well, I guess I will see once the solar system starts producing today. :smile:

Now for the negative values of the shunt measurement. How can that negative value be converted into positive. Reason for my asking is because of the requirement of this card Tesla Style Card

From git hub page:

**templates for missing sensors or for negative sensors

Remember you can create template sensors if you are missing one like solar yield out of solar_consumption and grid_feed_in or if you are missing another one like home_consumption. Some inverters have positive and negative values, here all sensors need to be positive values, so create template sensors like:

battery_consumption:
    value_template: '{% set batter_cons = sensor.powerwall_battery_now | int %}
                    {% if batter_cons > 0 %}
                        {{ batter_cons | int }}
                    {% else %}
                        0
                    {% endif %}'
    device_class: power
    unit_of_measurement: W**

I need to buy you a beer :beer:

Try using abs filter-

{{ states('sensor.powerwall_battery_now') | int | abs }}

2 Likes

Solution to show Power Wall (negative) discharge values of the sensor as positive and showing 0 for all the positive values of the sensor. It works. Thank you again. Would you change anything?

template:
  - sensor:
      - name: "PWdischarge Tesla"
        unit_of_measurement: "W"
        state: <-
          {{ ([-30000, states('sensor.shunt_instantaneous_power')|int, 0] |sort) [1] | abs }}

I have similar question. I would like to have data from multiple sensors and get mean value, but statistics platform does not support multiple entities. Any ideas for this?

 - platform: statistics
    entities:
      - sensor.mijia_1_temperature
      - sensor.temperature_19
    sampling_size: 2
    name: livingroom_mean
    max_age:
      minutes: 30