Template sensor for differnces between sensors

Greetings All,

I am trying to setup a template sensor that will determine if the shower is in use. I plan to use this combined with my Bayesian sensor for measuring if the bathroom is occupied or not.

I have a humidity sensor in the bathroom as well as one in the shower itself. What I plan to do is measure the difference between these two points. If the humidity in the shower is greater by say 10% than the rest of the room, the shower is likely in use.

I think a template sensor is likely the best way to do this but I am not sure how to do it. I havenā€™t done any ā€œfunction-yā€ stuff with template sensors, just reformatting states. Any assistance anyone could provide would be great as I am not even sure how to begin.

Thanks!

Here is what I have so far. Iā€™m just not sure how to format this correctly:

    shower:
  value_template: >-
    {% if states.sensor.temperatureandhumidity_13_0.state >= states.sensor.multisensor_233_2.state + 10 %}
      'In Use'
    {% else %}
      'Not in Use'
    {% endif %}

This is not working for you?

It does not. It shows an unknown state.

Iā€™d say youā€™re using the wrong states, use the template engine to figure them out. Enclose your state call in double curly brackets:

{{states.sensor.temperatureandhumidity_13_0.state}}

Since this is a dual sensor Iā€™m guessing the main state is temperature, not humidity. Double check the units too, you might have to do a type cast to be able to compare values from two different types of sensors. I donā€™t think the % sign should be there in the template sensor either but that depends on the types used.

I figured it out.

Here is a slightly modified version of the code. The issue was I needed to pipe the first sensorā€™s state into a float. I was mixing it with a string.

{% if states('sensor.temperatureandhumidity_13_0') == states('states.sensor.multisensor_233_2')|float +10  %}

ā€˜In Useā€™
{% else %}
ā€˜Not in Useā€™
{% endif %}

This is only true if they are equal, as what I was proposing in my previous deleted post.
ā€œ(sensor 1 = 10 and sensor 2 =10) = trueā€
ā€œ(sensor 1 = 5 and sensor 2 =10) = falseā€
ā€œ(sensor 1 = 10 and sensor 2 =5) = falseā€

The only thing I can come up with and seems to work is creating 2 template sensorā€™s

  1. {{states.sensor.multisensor_233_2.state|float +10 }}
  2. {% if states.sensor.temperatureandhumidity_13_0.state >= states.sensor.new_sensor.state %}
    ā€˜In Useā€™
    {% else %}
    ā€˜Not in Useā€™
    {% endif %}

The sensors (most likely) return float values and you try to add an integer (10) before the comparison. Try adding 10.0 or type cast the sensor value to int before adding.

This works for me in the template engine, I havenā€™t tried adding it to my files.

shower:
  value_template: >-
    {% if states.sensor.temperature_158d0001b92603.state|int >= states.sensor.temperature_158d0001b9152f.state|int + 10 %}
      'In Use'
    {% else %}
      'Not in Use'
    {% endif %}
1 Like

:+1: Yes works great!