Simple "if...then...else"

Hi,

I have created this little automation to compate two temperatures and based on that a boolean input gets set to ‘on’.

- id: '1596650713006'
  alias: Check Außen vs Innentemperatur
  description: ''
  trigger:
  - platform: time_pattern
    minutes: '/5'
  condition:
  - condition: template
    value_template: '{{ states.sensor.netatmo_jt_aussen_temperature.state > states.sensor.netatmo_innen_temperature.state
      }}'
  action:
  - data: {}
    entity_id: input_boolean.status_temperatur_ahai
    service: input_boolean.turn_on
  mode: single

Is there a way to add an “else” part to this script that would set the boolean input to ‘no’? Or if the condition is not fulfilled it gets set to ‘no’? Or do I have to create a separate automation for that?

Johannes

Rather than using an automation and an input boolean you could use a template binary sensor.

value_template: "{{ states('sensor.netatmo_jt_aussen_temperature')|float > states('sensor.netatmo_innen_temperature')|float }}"

When the template is true the sensor will be on, when the template evaluates as false the sensor will be off.

You can even apply a cold or heat device class for fancy changing icons.

2 Likes

Ok, that’s really cool :slight_smile: I think I need to overthink some things compared to my previous home automation system.

While this solves it, coming back to my initial question, just to learn something, don’t get me wrong here. :wink: Just wanted to know if an if.then.else construct is possible with HA or does it stop at the then? :slight_smile:

Johannes

If else, elif (else-if) are all valid in jinja templating.

E.g.

1 Like

Thanks, will check it out and try :slight_smile:

Hello everyone. i am trying to figure out how to do an automation with the trigger accuweather real feel sensor . if the outside temperature drops bellow 3 deg celsius, then sonoff relay on, else sonoff relay off.

Can You please help me with this? thgank You in advance.

trigger:
  - platform: numeric_state
    entity_id: YOUR TEMPERATURE ENTITY_ID 
    below: 3
  - platform: numeric_state
    entity_id: YOUR TEMPERATURE ENTITY_ID
    above: 3
action:
  service: "switch.turn_{{ 'on' if states('YOUR TEMPERATURE ENTITY_ID')|float < 3 else 'off' }}"
  entity_id: YOUR RELAY ENTITY_ID 
2 Likes

it works flawlessly. Thank You so much for Your help mf_social.

1 Like

to go further with my automations:
if a phone enters a zone(detected by phone location) turn on a device, if the device leaves the zone, turn off the device. thank You in advance.

trigger:
  - platform: zone
    entity_id: YOUR DEVICE ENTITY_ID 
    zone: YOUR ZONE ENTITY_ID
    event: enter
  - platform: zone
    entity_id: YOUR DEVICE ENTITY_ID 
    zone: YOUR ZONE ENTITY_ID
    event: leave
action:
  service: "switch.turn_{{ 'on' if trigger.event == 'enter' else 'off' }}"
  entity_id: YOUR SWITCH ENTITY_ID

for some reason it does not seem to work. this is what i have in the automation

alias: Pornire Boiler
description: ‘’
trigger:

  • platform: zone
    entity_id: device_tracker.sm_g935f
    zone: zone.home
    event: enter
  • platform: zone
    entity_id: device_tracker.sm_g935f
    zone: zone.home
    event: leave
    condition: []
    action:
  • service: ‘switch.turn_{{ ‘‘on’’ if trigger.event == ‘‘enter’’ else ‘‘off’’ }}’
    entity_id: switch.boiler
    mode: single

is there something wrong with the sytax?

I can’t tell you if you don’t show me it correctly formatted.

alias: Pornire Boiler
description: ''
trigger:
  - platform: zone
    entity_id: device_tracker.sm_g935f
    zone: zone.home
    event: enter
  - platform: zone
    entity_id: device_tracker.sm_g935f
    zone: zone.home
    event: leave
condition: []
action:
  - service: 'switch.turn_{{ ''on'' if trigger.event == ''enter'' else ''off'' }}'
    entity_id: switch.boiler
mode: single

sorry about that, newbie here :slight_smile:

That looks fine, except for the messed up quotes in the template, but if it’s just one device measuring whether one person is home or not then it’s even simpler…

alias: Pornire Boiler
trigger:
  platform: state
  entity_id: device_tracker.sm_g935f
action:
  service: "switch.turn_{{ 'on' if trigger.to_state.state == 'home' else 'off' }}" 
  entity_id: switch.boiler
1 Like

MF_social,
thank You for the reply.
i agree, it would be more logical to detect if a person is home and this is what i would actually want. but as my original request was n a device, then i kept this logic.

so, if we were to retake, how would it look like if there was a person detected by the device that he is home or not?

thank You in advance for Your help.

Because the device state will only be ‘home’ or not, so the template will turn the switch on or off based on the state.

thats perfect. thank You. do you know if the refresh rate on an automation can be set? or if there is a setting on the devices position refresh rate? the automation works but it is laggy. the detection of the gps positon is made by the phone, and the switch is a tplink plug.

The automation will trigger the moment the state of the device tracker updates, so no need for a refresh rate there.

As for the device tracker, you’ll have to look at the settings for whatever software you’re using.

Or consider using a proximity based tracker for the automation, like ping or Bluetooth.

i see.
thank You very much for Your support.

2 Likes

Hello. I added to my automation a secondary action that informs me about the outcome of te action taken. Ow on each execution of the automation i receive the notification. Is there a way to receive the notification only when the outcome changes( when passing from if to else)?