Create a custom sensor whichs value is calculated from two other sensors

I have two humidity sensors:

On the balcony, named Balcony humidity
id: sensor.viking_0203502038_87_00_humidity

In the shower, simply named Shower humidity
id: sensor.viking_0203502038_87_00_humidity

What I want to accomplish is to determine if someone is taking a shower (in which case the light time out will be longer and the blinds will be closed in the bedroom). I have done this with a helper that turns on when Shower Humidity is over a specific value, but since the outdoor humidity changes over the seasons a low value for Shower humidity will give a lot of false positives half the year, and a high value will take too long for the helper to switch on.

I would like to try something like this:
[pseudo code]

IF (shower humidity) > ( (shower humidity) + ( 100 - (balcony humidity) ) )
THEN (shower switch) = 1

IF (shower humidity) =< ( (shower humidity) + ( 100 - (balcony humidity) ) )
THEN (shower switch) = 0

100 is for the maximum humidity
But I don’t know how the proper code would look like or where to put it.

I have looked in to templates under Developer tools, and googled, but I can’t get a grip of how to do it.
Is there a templates for dummies that I’ve missed?
Or maybe some one whips this into a couple of lines… :pray:

replace with your own

{% if (states('sensor.badkamer_humidity') | float(0)) > ( (states('sensor.badkamer_humidity') | float(0)) + ( 100 - ((states('sensor.outside_humidity_humidity') | float(0) )))) %} true
{%else%}
false
{% endif %}
1 Like

You don’t need the if/else. Just do

{{ the condition being tested }}

That logic reduces to “if the balcony humidity is over 100%”. You might want to re-think.

1 Like

Yes of course, it’s supposed to be
IF (shower humidity) > ( (BALCONY humidity) + ( 100 - (balcony humidity) ) )

not so much of a typ-o as a think-o…

That’s always 100…

Yes of course… again. Forgot to divide by 10

( (BALCONY humidity) + ( ( 100 - (balcony humidity) /10 ) ) )

The entity IDs in your first post are identical.

If you’re convinced about that latest logic (I’m not…), put the correct sensor IDs in here and drop it into the template binary sensor helper state template as in the screenshot above:

{% set sh = states('sensor.shower_humidity')|float(0) %}
{% set bh = states('sensor.balcony_humidity')|float(0) %}
{{ sh > bh + (100 - bh / 10) }}

Note arithmetic rules of order (I learned them as BODMAS: brackets, order (powers), division, multiplication, addition, subtraction) — that’s “bh plus 100 minus one-tenth of bh”. You may want to move some brackets around, as the test value for comparison with sh ranges from 100% to 190%…

Drop this in Developer Tools / Template:

{% for bh in range(0,101,10) -%}
bh: {{ bh }}; test: {{ bh + (100 - bh / 10) }}
{% endfor %}
1 Like

Good catch on the entity IDs

I’m open for suggestions for another logic or another method (that doesn’t involve significant costs…)

Thanks for the short and simple code.

Precedence of mathematical operators is a pita when it comes do computers and phones… I tend to sprinkle with brackets. I’m not familiar with the BODMAS acronym (being a non native english speaker) but the way you wrote it would not be correct as I learned in school, as for what I want.

I bracketed it like this, and so far the template sensor shows of, which is the same as reality.

{% set sh = states('sensor.viking_0203502038_d7_00_humidity')|float(0) %}
{% set bh = states('sensor.viking_0203502038_87_00_humidity')|float(0) %}
{{ sh > (bh + ((100 - bh) / 10)) }}
1 Like

I edited my previous post as I noticed my mathematical mistake, it now contains my updated calculation.

I ran the test in dev tools, with the updated calculation, and the outcome looks as I imagined it to be

Perhaps my intended logic is clearer now.

1 Like

I’m glad you got it sorted out. This is equivalent and a little bit shorter:

{{ sh > 10 + (bh * 0.9) }}

Brackets are not strictly necessary but help to read it.

1 Like

I am using derivative helper for humidity as condition in one of my automations. So if water flow is constantly above XY, I check if derivative is below 0 - meaning someone is likely taking shower.

1 Like