Calc in templates or automations

Hello,

I have 2 sensors to state my shift start and shift end. I can use them as trigger to switch between “on” and “energy_save” heater.

# google calendar sensor start time
# state - to <time> bevore event
- platform: template
  sensors:
    next_shift_start:
      friendly_name: Schichtbeginn
      #entity_id: sensor.time
      value_template: >-
        {% if states.calendar.sap_schichtplan.attributes.start_time %}
          {{((as_timestamp(states.calendar.sap_schichtplan.attributes.start_time) - as_timestamp(now())) /60) | default(99) | int}}
        {%- else -%}
          0
        {%- endif %}
# google calendar sensor start time
# state - to <time> bevore event
- platform: template
  sensors:
    next_schift_end:
      friendly_name: Schichtende
      #entity_id: sensor.time
      value_template: >-
        {% if states.calendar.sap_schichtplan.attributes.end_time %}
          {{((as_timestamp(states.calendar.sap_schichtplan.attributes.end_time) - as_timestamp(now())) /60) | default(99) | int}}
        {%- else -%}
          0
        {%- endif %}

If I open the the window, the heater has to be off. After close the window again, I want to reset the heater, to a state depending on my shift if need to heat or energy save.
I guess a template which contains heat or energy_save is the way. But how can I calculate here? Below dies not work.

- platform: template
  sensors:
    schift_heat:
      friendly_name: Schicht - Büro Heizen?
      value_template: >-
        {% if (next_schift_end > 30) and (next_shift_start < 60 ) %}
          heat
        {%- else -%}
          energy_save
        {%- endif %}

Can ayone Help pleased?
Many thanks in advance

You need to reference the sensor entities, schift_heat does not have a definition for next_schift_end or next_schift_start.

- platform: template
  sensors:
    schift_heat:
      friendly_name: Schicht - Büro Heizen?
      value_template: >-
        {% if (states('sensor.next_schift_end') | int > 30) 
        and (states('sensor.next_schift_start') | int < 60 ) %}
          heat
        {%- else -%}
          energy_save
        {%- endif %}

Manny Thanks, this works perfect. Many thanks to your explanation.

Now I discovered an other possibility. in Automation - Action the ‘if-then’ action is also possible.

if:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.next_shift_start
        below: '60'
      - condition: numeric_state
        entity_id: sensor.next_schift_end
        above: '30'
then:
  - device_id: <id>
    domain: climate
    entity_id: climate.heizung_buro
    type: set_hvac_mode
    hvac_mode: heat
else:
  - device_id: <id>
    domain: climate
    entity_id: climate.heizung_buro
    type: set_preset_mode
    preset_mode: Energy heat

This also works and to me looks as an better solution, as the test will not be executed every minute but only if the automation is triggered (I guess 2-4 times a day).

Sorry I’m new with home assistant and need to discover the different options. :slight_smile: