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

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)?

Hello ,
can You please add with a yaml format for an automation that would have to fullfill the task according to the schematics here attached ?
Thank You in advance for your Support.

group:
  everyone:
    entities:
      - TRACKER-1
      - TRACKER-2

automation:
  alias: switch based on temperature when home
  trigger:
    platform: state
    entity_id: group.everyone
  action:
    service: >
      {% if trigger.to_state.state == 'home' %}
        {% if states('REAL_FEEL_SENSOR')| int < 10 %}
          switch.turn_on
        {% else %} switch.turn_off {% endif %}
      {% else %}
        {% if states('REAL_FEEL_SENSOR')| int < 4 %}
          switch.turn_on
        {% else %} switch.turn_off {% endif %}
      {% endif %}
    entity_id: SWITCH

(replace everything in capitals with the correct entity_ids)

I have to ask though, are you sure you want to trigger on the presence detection, rather than the temperature?

Hello mf_social,

the trigger for the switch is the temperature. however the value of the below limit temperature is set by the presence or absence of one of the trackers at the location.
is this what Your syntax is taking into account please?

Best regards,

That’s not the logic you put in your flow chart.

This would be more suitable…

group:
  everyone:
    entities:
      - TRACKER-1
      - TRACKER-2

automation:
  alias: switch based on temperature when home
  trigger:
    - platform: numeric_state
      entity_id: REAL_FEEL_SENSOR
      below: 10
    - platform: numeric_state
      entity_id: REAL_FEEL_SENSOR
      below: 4
  action:
    service: >
      {% if ( is_state('group.adults', 'home') and
        states('REAL_FEEL_SENSOR')|int < 10 ) or
        ( is_state('group.adults', 'not_home') and
        states('REAL_FEEL_SENSOR')|int < 4 ) %}
          switch.turn_on
      {% else %} switch.turn_off {% endif %}
    entity_id: SWITCH
1 Like

mf_social,
yes this makes sense :slight_smile:

yes, my logic schematics should have had an “and” between the trackers and the below temperatures.

thank You for Your remark. i will try the automation you provided.
Thankk You for it.
Best regards,

1 Like