Help with an if...else automation based on numeric value

I am working my way up the ladder to make more complex automations and am having trouble finding examples of how this might be done. I want to turn a wemo outlet off if my sensor value goes above a set number, and on if it goes below. The following code works, but I would like to learn how to combine them into a single if…else type automation. Whats the best way to go about this?

- id: comed_cost_high_test
  alias: ComEd cost high test
  trigger:
  - above: '3.6'
    entity_id: sensor.comed_5_minute_price
    platform: numeric_state
  condition: []
  action:
  - service: switch.turn_off
    entity_id: switch.wemo_mini_1
    
- id: comed_cost_low_test
  alias: ComEd cost low test
  trigger:
  - below: '3.6'
    entity_id: sensor.comed_5_minute_price
    platform: numeric_state
  condition: []
  action:
  - service: switch.turn_on
    entity_id: switch.wemo_mini_1

YAML automations don’t really handle if/else automations very well. If you want to get into more advance constructs like that, without using two separate triggers like you have now, I’d recommend taking a look at AppDaemon.

Try this:

- id: comed_cost_test
  alias: ComEd cost test
  trigger:
    platform: state
    entity_id: sensor.comed_5_minute_price
  condition: []
  action:
  - service_template: "switch.turn_{{'off' if trigger.to_state.state | float > 3.6 else 'on'}}"
    entity_id: switch.wemo_mini_1

or this (it’s just a condition added)

- id: comed_cost_low_high_test
  alias: 'ComEd cost low/high test'
  trigger:
    platform: state
    entity_id: sensor.comed_5_minute_price
  condition:
    condition: template
    value_template: >-
      {% set target_state = 'off' if trigger.to_state.state == 'unknown' or trigger.to_state.state | round(1) > 3.6 else 'on' %}
      {% set current_state = states('switch.wemo_mini_1') %}
      {{ current_state != 'unknown' and current_state != target_state }}
  action:
    - service_template: switch.turn_{% if trigger.to_state.state == 'unknown' or trigger.to_state.state | round(1) > 3.6 %}off{% else %}on{% endif %}
      entity_id: switch.wemo_mini_1

Thanks, I tested the codes and they both work great. As for AppDaemon, I have no experience with that language so I’ll stick with what I have some understanding of, thank you for the suggestion though.

Glad it works for you.
Think AppDaemon is a bit too much for such a simple job :wink: