Car Heater (winter is coming) need coding help!

@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:

I got it working and I also have to use utcnow to get it to turn on at the right time. :slight_smile:

I do have another problem though, and that is that the automations turn off acording to the script, but they never turn on again. If I restart Home assistant or manually activates the automations it works again, but shouldn’t they get activated if I set a new departure time?

Thank you for sharing this code and solution
It worked great first time

One question, for my understanding (and yes, I am a Learner) :slight_smile:

In the mvak group, you state “sensor.heater_start_time_ak”, but I don’t find that referenced anywhere else.

How is that defined, and where?

I have been trying to get this to work for a couple of nights now. I can see everything in the frontend but when I choose a time in the sliders the event wont fire at the time it should. Nothing happens. For example if the temperature outside is 3,5 the vent should fire 30 minutes before departure, right? I have it connected to a switch in the living room now just to try it out.

Automations.yaml:

#Kupevarmare
- 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.koket_utomhus_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.se_ak_mv
    data_template:
       message: 'Motorvärmare AK (-10°) är på i 2h från {{now().strftime("%H:%M:%S")}} Temp ute: {{ states.sensor.netatmo_utomhus_norra_husgaveln_temperature.state}}°C'
  - service: notify.ios_ak
    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.koket_utomhus_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.se_ak_mv
    data_template:
      message: 'Motorvärmare Ak (+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.koket_utomhus_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.se_ak_mv
    data_template:
      message: 'Motorvärmare AK (+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: notify.ios_ak
    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.papperslampan, automation.heater_ak_off
- service: notify.se_ak_mv
  data_template:
    message: 'Motorvärmare AK Stängs nu av {{now().strftime("%H:%M:%S")}} Temp ute: {{ states.sensor.netatmo_utomhus_norra_husgaveln_temperature.state}}°C'

Senors.yaml
#Kupevarmare

- 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

scripts.yaml
#Kupevarmare

heater_ak_on:
  alias: Kupévärmare på
  sequence:
  - service: homeassistant.turn_on
    data:
      entity_id: switch.papperslampan
  - service: automation.turn_on
    entity_id: automation.heater_ak_off

in the configuration.yaml:
#Kupevarmare

  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

Hej.
Du har inte automationen för sensor.heater_start_time_ak?

Hej,

Här är hela “paketet”

https://paste.ubuntu.com/p/HB4pv8ctNC/