Car Heater (winter is coming) need coding help!

I need some help with the creation of a car heater function (Living in Sweden) and winter is coming :wink:

I have an outside temp sensor and a switch to connect to the heater in my car.

If the temperature is < 10 i want the heater to start 30 minutes before the actual time to leave.
If the temperature is < 0 i want the heater to start 60 minutes before the actual time to leave.
If the temperature is < -10 i want the heater to start 90 minutes before the actual time to leave.

I also want the heater to run for 30 min after the actual time to leave (if I am late).
It would also be great if it could be started/stopped manually. Departure time could be set trough a google calendar or via an input_number slide.

I have searched trough the forum for this, but with no luck


This is way off my skills, so I trust in you guys :pray:

You can start with the alarm clock package: https://github.com/Danielhiversen/home-assistant_config/blob/master/packages/alarm_clock.yaml

Update the condition so that it will trigger if time less than 90 and temp < -10 or time less than 60 and temp < 0 or 


Use the relative_time() to calculate the relative time left before the sat temperature

1 Like

I am sorry, but it is little to complicated for me (as I don’t have any programming background)

Cut and paste works :innocent:

@Danielhiversen, how to add the “extra time” (ex +90 min) time in condition? Condition for the temp sensor must also be added some where?

Will there be three different automations for the following?

If the temperature is < 10 i want the heater to start 30 minutes before the actual time to leave.
If the temperature is < 0 i want the heater to start 60 minutes before the actual time to leave.
If the temperature is < -10 i want the heater to start 90 minutes before the actual time to leave.

I am sorry for all the noob questions


Hi,

When you refer to extra time, you mean that heater run for 30 min after actual time to leave?

Maybe this you can do it with presence detection of your wifi or geolocation.

First after beginning with automation, have you configured any automation in your HA?

@garvarma

Yes the extra time (30 min) is if I am late, and not leaving in time.

I have other automatons running in HA for light management.

@Danielhiversen

I have tried your code :ok_hand: It works fine. But i need some help with the implementation of the time to run before actual time to leave and the temp sensor condition.

https://pastebin.com/SD5nKsdL

Need some advice
 Will this work?

https://pastebin.com/M0spLRAQ

You can change line 21 to False
If you go to HA_IP:8123/dev-template you can paste in the template to validate it.

It writes out False

For testing you can change the temperature values and time shift, to validate that the output changes to True when expected.

This is my attempt


Not so nice, but it works. All suggestions for better code is welcome :slight_smile:

Thanks to @Danielhiversen for inspiration!

heater

#########################################################################################################
# Car heater package                                                                                    #
#                                                                                                       #
# Source https://github.com/Danielhiversen/home-assistant_config/blob/master/packages/alarm_clock.yaml  #
#                                                                                                       #
#########################################################################################################

# Automation section
automation:
  - alias: "heater ak minus 10"
    initial_state: True
    trigger:
     - platform: time
       minutes: '/5'
       seconds: 2
    condition:
      condition: and
      conditions: 
        - condition: numeric_state
          entity_id: sensor.netatmo_utomhus_norra_husgaveln_temperature
          below: -10
        - condition: template
          value_template: '{{ ((now().strftime("%s") | int + 7200) | timestamp_custom("%H:%M")) == states.sensor.departuretime_ak.state  }}'
    action:
       - service: script.turn_on
         entity_id: script.heater_ak_on
       - service: notify.telegram
         data_template:
           message: 'MotorvÀrmaren (-10°) Àr pÄ i 2h frÄn {{now().strftime("%H:%M:%S")}} Temp ute: {{ states.sensor.netatmo_utomhus_norra_husgaveln_temperature.state}}°C'
       - service: homeassistant.turn_off
         entity_id: automation.heater_ak_minus_10, automation.heater_ak_plus_1, automation.heater_ak_plus_5
  
  - alias: "heater ak plus 1"
    initial_state: True
    trigger:
     - platform: time
       minutes: '/5'
       seconds: 2
    condition:
      condition: and
      conditions:
        - condition: numeric_state
          entity_id: sensor.netatmo_utomhus_norra_husgaveln_temperature
          below: 1.0 
        - condition: template
          value_template: '{{ ((now().strftime("%s") | int + 3600) | timestamp_custom("%H:%M")) == states.sensor.departuretime_ak.state  }}'        
    action:
       - service: script.turn_on
         entity_id: script.heater_ak_on
       - service: notify.telegram
         data_template:
           message: 'MotorvÀrmaren (+1°) Àr pÄ i 1h frÄn {{now().strftime("%H:%M:%S")}} Temp ute: {{ states.sensor.netatmo_utomhus_norra_husgaveln_temperature.state}}°C'
       - service: homeassistant.turn_off
         entity_id: automation.heater_ak_plus_1, automation.heater_ak_plus_5, automation.heater_ak_minus_10
  
  - alias: "heater ak plus 5"
    initial_state: True
    trigger:
     - platform: time
       minutes: '/5'
       seconds: 2
    condition:
      condition: and
      conditions:
        - condition: numeric_state
          entity_id: sensor.netatmo_utomhus_norra_husgaveln_temperature
          below: 5.0 
        - condition: template
          value_template: '{{ ((now().strftime("%s") | int + 1800) | timestamp_custom("%H:%M")) == states.sensor.departuretime_ak.state  }}'        
    action:
       - service: script.turn_on
         entity_id: script.heater_ak_on
       - service: notify.telegram
         data_template:
           message: 'MotorvÀrmaren (+5°) Àr pÄ i 30 min frÄn {{now().strftime("%H:%M:%S")}} Temp ute: {{ states.sensor.netatmo_utomhus_norra_husgaveln_temperature.state}}°C'
       - service: homeassistant.turn_off
         entity_id: automation.heater_ak_plus_5, automation.heater_ak_plus_1, automation.heater_ak_minus_10

  - alias: "heater ak enabled"
    trigger:
      platform: state
      entity_id: input_number.heater_ak_hour, input_number.heater_ak_minutes
    action:
      - service: homeassistant.turn_on
        entity_id:
          - automation.heater_ak_minus_10
          - automation.heater_ak_plus_1
          - automation.heater_ak_plus_5

  - alias: "heater ak off"
    initial_state: False
    trigger:
     - platform: time
       minutes: '/5'
       seconds: 2
    condition:
      - condition: template
        value_template: '{{ now().hour == (states.input_number.heater_ak_hour.state  | round(0))  and  now().minute == (states.input_number.heater_ak_minutes.state  | round(0) )  }}'        
    action:
       - service: homeassistant.turn_off
         entity_id: switch.carheater_ak_plug_switch, automation.heater_ak_off
       - service: notify.telegram
         data_template:
           message: 'MotorvÀrmaren StÀngs nu av {{now().strftime("%H:%M:%S")}} Temp ute: {{ states.sensor.netatmo_utomhus_norra_husgaveln_temperature.state}}°C'


# Input section
input_number:
  heater_ak_hour:
    name: Timmar
    icon: mdi:timer
    initial: 07
    min: 0
    max: 23
    step: 1
  
  heater_ak_minutes:
    name: Minuter
    icon: mdi:timer
    initial: 30
    min: 0
    max: 55
    step: 5

# Sensor section
sensor:
  - platform: template
    sensors:
      departuretime_ak:
        friendly_name: 'Avresetid AK'
        value_template: '{% if states.input_number.heater_ak_hour.state|round(0)|string|length == 1 %}0{% endif %}{{ states.input_number.heater_ak_hour.state|round(0)|string }}:{% if states.input_number.heater_ak_minutes.state|round(0)|string|length == 1 %}0{% endif %}{{ states.input_number.heater_ak_minutes.state|round(0)|string }}'
        entity_id: 
         - input_number.heater_ak_minutes
         - input_number.heater_ak_hour

# Group section
group:
  heaterak:
    name: Heater AK
    icon: mdi:alarm
    view: yes
    entities:
      - group.mvak
      
  mvak:
    name: "MotorvÀrmare AK"
    entities:
      - sensor.departuretime_ak
      - sensor.heater_start_time_ak
      - input_number.heater_ak_hour
      - input_number.heater_ak_minutes
      - sensor.netatmo_utomhus_norra_husgaveln_temperature
      - switch.carheater_ak_plug_switch

# Script section
script:

  heater_ak_on:
    sequence:
    - service: homeassistant.turn_on
      #data: 
      entity_id: switch.carheater_ak_plug_switch
    - service: automation.turn_on
      entity_id: automation.heater_ak_off
3 Likes

Hello fellow Swed. Copied your code and it works very good! Thanks for sharing! :slight_smile:

1 Like

I tried to create a nearly copy of your project. But I just have one tiny problem left, my code is not work. :wink:

I have tried in the dev-tool to check my template (the current time now is 14:17), if I run this:

‘{{ ((now().strftime("%s") | int) | timestamp_custom("%H:%M")) }}’
‘{{ (now().strftime("%s") | int -3600) | timestamp_custom("%H:%M") }}’

I got output 15:17 for the first line, and 14:17 on the second one. So it seems that my time is 1hour off. But ‘date’ in cli gives the correct time (14:17).

Any ideas?

I answer myself. My code work. But it seems that everything is use the correct time beside the sensors. They use utc-time. So my heater is start 1h earlier then I set. I can’t find a setting to change this. I use “Europe/Stockholm” as time_zone in configuration.yaml.

Strange, It shows correct time for me
 (Also Europe/Stockholm.

Do you use hassio/docker?

I use hass.io on a raspberry Pi.
Do you have:

  • platform: time_date
    display_options:
    • ‘time’
    • ‘time_utc’
      in your configuration.yaml? Do they show the same time?
      Mine show (current time 17:28)
      Time: 17:28
      Time (UTC): 16:28

Yes I use the time sensor. And utc is one hour behind.

I also run HA in a virtual environment. I know there was a guy in the Swedish Facebook group SHG that hade similar problems.

It was fixed by using utcnow() in the template-string.

Nice, good to know!

I have been trying to get this to work for a couple of days now, but I can’t get it going.

HASS gives me the following error Error loading /config/configuration.yaml: expected '<document start>', but found '<block mapping start>' in "/config/packages/motorv.yaml", line 99, column 1

I’m using Atom as my editor and it outputs the another error for line 98 end of the stream or a document separator is expected

atom

As far as I can tell the indentation is correct.

Please help! :slight_smile: