# 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â€¦

``````{% 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