For this solution
I combined the two existing soutions:
(curtesy @Mutt)
and
(curtesy @nylund)
I used the linear temperature fomula from second thread and the general automation of the first:
- id: '1601582133'
alias: Motorvärmare Flagga Start (master)
trigger:
- platform: time_pattern
minutes: /1
condition:
- condition: template
value_template: '{{ states(''sensor.balkong_norr_temperature'') | float <= 10
}}'
- condition: or
conditions:
- condition: state
entity_id: input_boolean.mv_flagga_automat_igang
state: 'on'
- condition: state
entity_id: input_boolean.mv_flagga_manuell_tid
state: 'on'
action:
- service_template: >
{# This is a jinja comment and will be ignored by the interpreter #}
{# ALL the below is indented more than it needs to be to show the comments more clearly #}
{# This next line (all comments will be as this) gets the time RIGHT NOW and stores it as a
{# HUGE number that has both date and time #}
{% set timenow = as_timestamp(now()) %}
{# this gets your input number hour (input_datetime is much easier) and stores as a number in hr #}
{% set dept_hr = states('input_number.mv_flagga_timme') | int %}
{# this gets your input number minute and stores as a number in mn #}
{% set dept_mn = states('input_number.mv_flagga_minuter') | int %}
{# this sets your 'settime' as a huge number and stores in settime #}
{% set settime = as_timestamp(now().strftime("%Y-%m-%d " ~ '{0:0=2d}:{1:0=2d}'.format(dept_hr, dept_mn) ~ ':00')) %}
{# as we've already done most of the work we just subtract 3 hours from the
set time to start our evaluation #}
{% set starttime = settime - (2*60*60) %}
{# this tests to see if we are 'in' the 'slot' #}
{% set slot = (starttime <= timenow < settime) %}
{# this gets the temperature (as a float, as that will aid in resolution) #}
{% set tempdeg = states('sensor.balkong_norr_temperature') | float %}
{# this gets the minute offset from the start based on the temperature #}
{% set offset_mins = ((-4 * tempdeg) + 40) | int %}
{# given the 'offset' we now have a new 'start time' calculated, based on temperature #}
{% set heatstart = settime - (offset_mins * 60) %}
{# this tests that we are (now) in the 'slot' and 'that switch on time' is in the past #}
{% set heat = slot and timenow >= heatstart %}
{# this uses the last evaluation to determine if we switch 'on' or 'off' #}
{% if heat %}
switch.turn_on
{# switch.turn_off is managed by separate automation that monitors the entity_id being ON for x:y hrs #}
{% endif %}
entity_id: switch.motorvarmare_flagga
initial_state: true
mode: queued
max: 10
Then, rather then sending a switch_off emeddiately at the the departure time I have an action triggered automation that monitors the switch_on of the switch.motorvarmare_flagga, delays for 2h and then sends the off signal. This will ensure a warm car even if departure is delayed, and if I do leave on time, what is the problem with the power still being on?
- id: '1602700151078'
alias: Motorvärmare Flagga Stopp (master)
description: Slå av efter 2h
trigger:
- platform: state
entity_id: switch.motorvarmare_flagga
to: 'on'
for: '2:10:00'
condition: []
action:
- service: switch.turn_off
data: {}
entity_id: switch.motorvarmare_flagga
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.mv_flagga_manuell_tid
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.mv_flagga_automat_igang
The input_booleans are to differntiate between manual heating on, and heating provided by the schedule automations (this basically from stopping the main automation to pump order every minute).
The glance_card has the input_number values as a temporary workaround of HA not coping with narrow columns, most noticable in the native Android app where the number value is dropped from display.
The sceduling is then managed by separate automations that is time-triggered just 2h before planned departure, setting the input_values as well as the input_boolean.
And, yes, I did block the main atomation from flooding the loogbook by adding an exclude rule in configuration.yaml.