Calculated value, difference between two sensors

Hi

I would like to compare two temperatures measured with dallas sensors, and I guess that some kind of template sensor with a lambda would be the way to go. But I cannot find out how to do it.

difference = (float value 1) - (float value 2)

What would the code be?

Can you give us your YAML config so far, or at least the part for two Dallas sensors?

The sensors work with this, but the template part is still just guessing.

dallas:
  - pin: GPIO14
    
sensor:
  - platform: dallas
    address: 0x1E0300A279559928
    name: "Temp 1"
    id: temp1
  - platform: dallas
    address: 0x690300A279A23D28
    name: "Temp 2"
    id: temp2
  - platform: template
    name: Hot or not
    id: hotornot
    lambda: return temp2 - temp2; #???

Try

  - platform: template
    id: hotornot
    unit_of_measurement: °C
    update_interval: 10s
    lambda: 'return id(temp2).state - id(temp1).state;'

I’m not sure how you’re going to use hotornot, but if you don’t need it to be published to Home Assistant, then you may not need to create a new template sensor. You could just use the same lambda statement where ever else you use the value.

2 Likes

That works like a charm. Thank you.
Good point on not needing the template sensor, it’s supposed to start a fan so I will use it in an automation instead.

1 Like

Hi there,

If you would like to publish the hotornot value to HA, what do you need to add to the ESPHome config? I tried several options among on_value but at compling I get a error. Any suggestions?

Give it a name: :point_down:

If you get a error message you should post it and not keep it - that’s self :fish:

1 Like

Yes! that did the trick, thank you -:wink:

Working code:

sensor:
  - platform: dallas
    address: 0x4c3c01f095637899
    name: "Supply Temperature - Ta"
    id: Ta
  - platform: dallas
    address: 0xf83c01f095c53a00
    name: "Return Temperature - Tr"
    id: Tr
  - platform: template
    name: "Delta T"
    id: Td
    unit_of_measurement: °C
    update_interval: 10s
    lambda: |-
      return (id(Ta).state - (id(Tr).state - 0.68750));
3 Likes