Ideally the automation can be eliminated, but for now, I’m good. One could probably pack the rules for the offset into the binary sensor too, however when the outside temp is close to the inside temp on certain days, that will require the “5 AM fix”
ok, so I think I’ve finally got it. Still have to observer though.
The logic is this:
You have three phases:
- below lower limit
- between lower limit and upper limit
- above upper limit.
Phase 1. and 3. are easy.
Phase two has two cases:
2.1. value crosses the border from outside to inside
2.2. value changes within the interval
In case 2.1. the state should toggle, no matter if it is coming from above (OFF to ON) or below (ON to OFF)
In Case 2.2 the state should stay the same it was after entering the interval, until it leaves the interval again, in which case we would be in Phase 1. or 3. again.
To achieve this we need to know, weather the previous value was inside the interval or not.
So I introduced an additional state attribute “isin” that stores exactly that.
This is what it looks like:
state: |
{% set current_state = this.state %}
{% set base_entity = states('sensor.environment_temperature')|float(0) %}
{% set limit_upper = states('sensor.home_temperature')|float(0) + states('input_number.home_temperature_warm_diff')|float(0) %}
{% set limit_lower = states('sensor.home_temperature')|float(0) - states('input_number.home_temperature_warm_diff')|float(0) %}
{% if base_entity > limit_upper %}
on
{% elif base_entity < limit_lower %}
off
{% else %}
{% if state_attr(current_state,'isin') == 'on' %}
{{ this.state }}
{% else %}
{{ not this.state }}
{% endif %}
{% endif %}
attributes:
isin: |
{% set base_entity = states('sensor.environment_temperature')|float(0) %}
{% set limit_upper = states('sensor.home_temperature')|float(0) + states('input_number.home_temperature_warm_diff')|float(0) %}
{% set limit_lower = states('sensor.home_temperature')|float(0) - states('input_number.home_temperature_warm_diff')|float(0) %}
{{ limit_lower <= base_entity <= limit_upper }}
[EDIT 20220818] Removed Quotes from “on” and “off”
that looks great too, let me try it out, will observe it for a while and report back
It works, but without hysteresis it might be flapping
the hysteresis in your code above is 'input_number.home_temperature_warm_diff'
if I got it correctly?
There is no hysteresis in the classical sense.
This still needs to be added.
In fact, there need to be two now, for both limits.
So ‘input_number.home_temperature_warm_diff’ is an offset only?
I also need a threshold binary sensor and use the helper for this. In my case I need a variable lower limit with a fix threshold. First, I can not enter the variable lower direct into the helper GUI, its red and does not work.
Then I tried following in my templates.yaml file:
sensor:
- name: "Pool IST Temp"
state: >
{{ states('input_number.poolactualtemp') }}
binary_sensor:
- platform: threshold
name: "Pool Need Heat"
entity_id: sensor.pool_ist_temp
lower: {{ states(input_number.poolnominaltemp1) }}
hysteresis: 0.5
Also doesn’t work.
And when I set a number for “lower”, quick restart is possible but after that with following error messages:
sensor:
- name: "Pool IST Temp"
state: >
{{ states('input_number.poolactualtemp') }}
binary_sensor:
- platform: threshold
name: "Pool Need Heat"
entity_id: sensor.pool_ist_temp
lower: 30
hysteresis: 0.5
Hopefully somebody can help
Thanks
You can see a working example above already in my post Use template for binary_sensor threshold? - #21 by fair_dinkum
The syntax changed slightly, so that since HA 2022.05 or 2022.06 this sensor must now be declared as a template sensor, see below.
My example uses an input helper for the threshold, so I can set a threshold directly in the UI - to be honest, I never changed it so far, but in case I would need to, there is an easy way to do so I have been using this sensor for almost 2 years and it has been working great.
template:
- binary_sensor:
- name: Inside vs outside temp
icon: mdi:thermometer-low
availability: |
{{ not false in
[states('sensor.outside_temp),
states('input_number.threshold'),
states('sensor.inside_temp')]|map('is_number')
}}
state: |
{% set hysteresis=states('input_number.threshold')|float(0) %}
{% set entity=states('sensor.outside_temp')|float(0) + states('input_number.offset')|float(0) %}
{% set limit=states('sensor.inside_temp')|float(0) + (hysteresis if this.state=='on' else -hysteresis) %}
{{ entity < limit }}
Great, thanks a lot. This is a selfmade hysteresis binary sensor, which is necessary because the built-in integration can not work with templates as limits.
I skipped the “availability”, don’t know what is good for.
This is my binary sensor in templates.yaml:
- name: "Pool Wärmebedarf"
unique_id: "poolneedheat"
state: |
{% set hysteresis=0.3 %}
{% set actualtemp=states('sensor.poolactualtemp')|float(0) %}
{% set settemp=states('input_number.poolsettemp')|float(0) + (hysteresis if this.state=='on' else -hysteresis) %}
{{ actualtemp < settemp }}
The only think, unique id is always ignored, it sets a unique id derivated from the name, in my case pool_warmebedarf…funny!