Shortening my Automations

Hello, so I have just started working with hass.io (2 days ago) and now I started looking into automations, I’m pretty familar with openhab2 rules which is the equivelant of automations in home-assistant, I have wrote 2 automations for turning on and off my relay when the temperature is too high and turn on when it’s too low, I’m curious can You guys help me short it into one automation?

  • alias: ‘Temperature is below preffered,turn on heating’
    hide_entity: true
    trigger:

    • platform: template
      value_template: ‘{{ states.sensor.home_temperature.state <= states.input_number.slider1.state }}’
      action:
    • service: homeassistant.turn_on
      entity_id: switch.home_relay
  • alias: ‘Temperature Is as Preffered, Turn off Heating’
    hide_entity: true
    trigger:

    • platform: template
      value_template: ‘{{ states.sensor.home_temperature.state >= states.input_number.slider1.state }}’
      action:
    • service: homeassistant.turn_off
      entity_id: switch.home_relay’

Any help is highly appreciated. :slight_smile:

service_template would work.

- alias: Adjust heat based on temperature
  hide_entity: true
  trigger:
    - platform: state
      entity_id: sensor.home_temperature, input_number.slider1
  action:
    - service_template: >
        {% if states('sensor.home_temperature') | float >= states('input_number.slider1') | float %}
        homeassistant.turn_off
        {% else %}
        homeassistant.turn_on
        {% endif %}
      entity_id: switch.home_relay

So basically, whenever the state changes for the slider or the home_temperature sensor, the automation will fire.

1 Like

You probably don’t want to turn it both on and off when home_temperature == slider1. Here’s a possible solution:

- alias: Control heating
  hide_entity: true
  trigger:
    platform: template
    value_template: >
      {{ is_state('switch.home_relay', 'off') and states('sensor.home_temperature')|float <
                                                  states('input_number.slider1')|float    or
         is_state('switch.home_relay', 'on')  and states('sensor.home_temperature')|float >=
                                                  states('input_number.slider1')|float }}
  action:
    service: switch.toggle
    entity_id: switch.home_relay

Something to consider - you have not hysteresis. You might want to consider adding some.

1 Like

Thank you both, these 2 actually give me a better understanding of automations in hassio. :smile:

1 Like

3d variant:

create an input_boolean or even a binary template sensor, which is set according to the value_template you provided, and then switch using:

action:
  service_template: >
    switch.turn_{{ states('input_boolean.home_relay') }}
  data:
    entity_id: switch.home_relay

has the added advantage of keeping content and logic apart. If the automation works as desired, you’d only have to change the value_template whenever circumstances need you to do so.

your binary_sensor would suffice, since it has only 2 states, you can ditch 1;-) if it has true and false as values, you can use:

  service_template: >
    switch.turn_{{ 'on' if is_state('binary_sensor.home_relay', 'True') else 'off' }}

binary_sensor:
- platform: template
  sensors:
    home_relay:
      friendly_name: Binary sensor Home relay
  #     device_class: light
      value_template: >
         {{ states.sensor.home_temperature.state <= states.input_number.slider1.state }}