Calculated sensor

Beginner question:
I still have a lot to learn…
I’ve been looking for the solution all day.

I have two sensors A and B.
However, I still want a third sensor .
Sensor C = sensor A - sensor B.

How do I do that?

Do I have to enter the calculation
Sensor: make ?
Or should/can I do the calculation in Utility Meter:

I do want to keep the calculated value C in the database

I have below code.

sensor`:

  - platform: rest 
    name: Meter_A
    json_attributes: 
        - waterquantity 
    resource: http://192.168.0.101/water.html

  - platform: rest 
    name: Meter_B
    json_attributes:
        - waterquantity
    resource: http://192.168.0.100/water.html

utility_meter:

  hourly_Meter_A:
    source: Meter_A_quantity
    cycle: hourly
   
  hourly_Meter_B:
    source: Meter_B_quantity
    cycle: hourly
       
  *hourly_Meter_C:*
*     source: Meter_C_quantity*
*     cycle: hourly*

Use a template sensor.

template:
  - sensor:
      - name: "Your Sensor Name Here"
        unit_of_measurement: "your_unit_here"
        state: "{{ states('sensor.your_sensor_a_here')|float(0) - states('sensor.your_sensor_b_here')|float(0) }}"

Thank you for your help.

I had tried this solution, but it doesn’t seem to work with the utility meter_C.
If meter_A and meter_B count then Utility meter_C has the same value as utility meter_A
meter_B does not seem to be subtracted from meter_A.

Utility meter_A and B indicate the correct value.

sensor`:

  - platform: rest 
    name: Meter_A
    json_attributes: 
        - waterquantity 
    resource: http://192.168.0.101/water.html

  - platform: rest 
    name: Meter_B
    json_attributes:
        - waterquantity
    resource: http://192.168.0.100/water.html

template:

  - sensor:
      name: meter_C 
      state: '{{(states.sensor.meter_A.attributes["waterquantity"] | float | round(0))- (states.sensor.meter_B.attributes["waterquantity"] | float) | round(0)}}'
      state_class: "measurement" 
      unit_of_measurement: "l" 
      icon: "mdi:water"

 utility_meter:

  hourly_Meter_A:
    source: Meter_A_quantity
    cycle: hourly
   
  hourly_Meter_B:
    source: Meter_B_quantity
    cycle: hourly
       
  hourly_Meter_C:
    source: Meter_C_quantity
    cycle: hourly

Entity ids only contain lower case letters. Also use this format to prevent errors if the sensor is not initialised:


template:
  - sensor:
      name: Meter_C 
      state: "{{ ( state_attr('sensor.meter_a', 'waterquantity')|float(0) - state_attr('sensor.meter_b', 'waterquantity')|float(0) )|round(0) }}"
      state_class: "measurement" 
      unit_of_measurement: "l" 
      icon: "mdi:water"

https://www.home-assistant.io/docs/configuration/templating/#states

Also your source entity ids are not correct for your utility metres:

 utility_meter:

  hourly_Meter_A:
    source: Meter_A_quantity   # What is this?

You need to provide a valid entity id there.

I still have a lot to read and learn!
Below is my original code.

The quantity sensors from meter A,B and C are correct (1870 - 626 = 1244 ).

For now only the quarter-hourly and hourly sensors are representative.
The others sensors are not running long enough yet.
The (utility) meters from Meter C does not do what I expect (meter_a - meter_b = meter_c).
.

sensor:

  - platform: rest 
    name: meter_a 
    json_attributes: 
    - waterquantity 
    resource: http://192.168.0.101/water.html

  - platform: rest 
    name: meter_b
    json_attributes:
    - waterquantity
    resource: http://192.168.0.100/water.html
    
template:

  - sensor: 
      name: "meter_a quantity" 
      state: "{{ ( state_attr('sensor.meter_a','waterquantity')|float(0)|round(0)) }}" 
      state_class: "measurement" 
      unit_of_measurement: "l" 
      icon: "mdi:water"
      
  - sensor: 
      name: "meter_b quantity" 
      state: "{{ ( state_attr('sensor.meter_b', 'waterquantity')|float(0)|round(0)) }}"  
      state_class: "measurement" 
      unit_of_measurement: "l" 
      icon: "mdi:water"

  - sensor:
      name: "meter_c quantity" 
      state: "{{ ( state_attr('sensor.meter_a', 'waterquantity')|float(0) - state_attr('sensor.meter_b', 'waterquantity')|float(0) )|round(0) }}"
      state_class: "measurement" 
      unit_of_measurement: "l" 
      icon: "mdi:water"

utility_meter:

  quarter_meter_a:
     source: sensor.meter_a_quantity
     cycle: quarter-hourly
     name: quarter meter_a

  hourly_meter_a:
    source: sensor.meter_a_quantity
    cycle: hourly
    name: hourly meter_a

  quarter_meter_b:
    source: sensor.meter_b_quantity
    cycle: quarter-hourly
    name: quarter meter_b

  hourly_meter_b:
    source: sensor.meter_b_quantity
    cycle: hourly
    name: hourly meter_b

  quarter_meter_c:
    source: sensor.meter_c_quantity
    cycle: quarter-hourly
    name: quarter meter_c

  hourly_meter_c:
     source: sensor.meter_c_quantity
     cycle: hourly
     name: hourly meter_c  
      
  daily_meter_a:
    source: sensor.meter_a_quantity
    cycle: daily
    name: daily meter_a
    
  daily_meter_b:
    source: sensor.meter_b_quantity
    cycle: daily
    name: daily meter_b
    
  daily_meter_c:
     source: sensor.meter_c_quantity
     cycle: daily
     name: daily meter_c   
    
  monthly_meter_a:
    source: sensor.meter_a_quantity
    cycle: monthly
    name: monthly meter_a
    
  monthly_meter_b:
    source: sensor.meter_b_quantity
    cycle: monthly
    name: monthly meter_b
    
  monthly_meter_c:
    source: sensor.meter_c_quantity
    cycle: monthly
    name: monthly meter_c

You can’t use the utility meter for C. That gives a running total. All you want is the difference between Utility meters A and B. Use template sensors to do this.

Thank you very much Tom,

I get what you mean and I have it working now :flushed:.
Not easy for a 60+ to start a new hobby.

1 Like