IR Heater stays on during movement, then turns off

Just thought to share this code.

I have an infrared heater and infrared motion sensor. When I go outside, I want that heater stays on, and then automatically turn off when I leave. Of course I don’t want the heater to turn on every time I go outside, so I will have a separate push button that turns the heating on.

Here’s the logical code for this.

input_boolean:
  yard_heater_push_button:
    name: Start yard heater
    initial: off

  yard_heater:
    name: Yard heater
    initial: off

  yard_heater_motion_sensor:
    name: Yard heater Motion Sensor
    initial: off

automation:
  - alias: yard_heater_button_on
    trigger:
      - platform: state
        entity_id: input_boolean.yard_heater_push_button
        to: 'on'
    action:
      service: input_boolean.turn_off
      entity_id: input_boolean.yard_heater_push_button

  - alias: yard_heater_button_off
    trigger:
      - platform: state
        entity_id: input_boolean.yard_heater_push_button
        to: 'off'
    action:
      service: homeassistant.turn_on
      entity_id: script.heater_on

  - alias: yard_heater_movement_on
    trigger:
      - platform: state
        entity_id: input_boolean.yard_heater_motion_sensor
        to: 'on'
    condition:
      condition: state
      entity_id: input_boolean.yard_heater
      state: 'on' 
    action:
      service: homeassistant.turn_off
      entity_id: script.heater_timer_off

  - alias: yard_heater_movement_off
    trigger:
      - platform: state
        entity_id: input_boolean.yard_heater_motion_sensor
        to: 'off'
    condition:
      condition: state
      entity_id: input_boolean.yard_heater
      state: 'on' 
    action:
      service: homeassistant.turn_on
      entity_id: script.heater_on

script:
  heater_on:
    alias: "Set heater on"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
           entity_id: script.heater_timer_off
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.yard_heater
      - service: script.turn_on
        data:
           entity_id: script.heater_timer_off
  
  heater_timer_off:
    alias: "Turn off heater after 10 seconds"
    sequence:
      - delay:
          seconds: 10
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.yard_heater

There’s some extra code for push button to turn itself off. The motion sensor you can set when motion starts and ends.

You should enclose the code in a “Preformatted text” block when pasting it here into the forum. It is hard for people to see the proper layout and spacing you have, and it makes it easier for them to copy and paste it directly into their configuration. If you don’t know how to do that, it is the icon on the top of the post editing field that looks like this:

</>

Just highlight your code and click that button

Thanks, the “Preformatted Text” was not as intuitive as “Code” would have been.

Yeah, I totally agree… Should be a different name, especially on a forum that is all about code :smiley:

Totally looks better now though :thumbsup:

Here’s the same script with added functionality to turn off the heater when the button is pressed again:

input_boolean:
  yard_heater_push_button:
    name: Start yard heater
    initial: off

  yard_heater:
    name: Yard heater
    initial: off

  yard_heater_motion_sensor:
    name: Yard heater Motion Sensor
    initial: off

automation:
  - alias: yard_heater_button_on
    trigger:
      - platform: state
        entity_id: input_boolean.yard_heater_push_button
        to: 'on'
    action:
      service: input_boolean.turn_off
      entity_id: input_boolean.yard_heater_push_button

  - alias: yard_heater_button_off
    trigger:
      - platform: state
        entity_id: input_boolean.yard_heater_push_button
        to: 'off'
    condition:
      condition: state
      entity_id: input_boolean.yard_heater
      state: 'off' 
    action:
      service: homeassistant.turn_on
      entity_id: script.heater_on

  - alias: yard_heater_button_off
    trigger:
      - platform: state
        entity_id: input_boolean.yard_heater_push_button
        to: 'off'
    condition:
      condition: state
      entity_id: input_boolean.yard_heater
      state: 'on' 
    action:
      service: homeassistant.turn_on
      entity_id: script.heater_off

  - alias: yard_heater_movement_on
    trigger:
      - platform: state
        entity_id: input_boolean.yard_heater_motion_sensor
        to: 'on'
    condition:
      condition: state
      entity_id: input_boolean.yard_heater
      state: 'on' 
    action:
      service: homeassistant.turn_off
      entity_id: script.heater_timer_off

  - alias: yard_heater_movement_off
    trigger:
      - platform: state
        entity_id: input_boolean.yard_heater_motion_sensor
        to: 'off'
    condition:
      condition: state
      entity_id: input_boolean.yard_heater
      state: 'on' 
    action:
      service: homeassistant.turn_on
      entity_id: script.heater_on

script:
  heater_on:
    alias: "Set heater on"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
           entity_id: script.heater_timer_off
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.yard_heater
      - service: script.turn_on
        data:
           entity_id: script.heater_timer_off
  
  heater_off:
    alias: "Set heater off"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
           entity_id: script.heater_timer_off
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.yard_heater

  heater_timer_off:
    alias: "Turn off heater after 10 seconds"
    sequence:
      - delay:
          seconds: 10
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.yard_heater