Home Assistant If else block

Hi everyone,

Im trying to automate the charge power based on the excess solar production at home. So far I have attempted to achieve this via the UI, however while inspecting the yaml output does this look fine? Thanks for any pointers in making it cleaner.

alias: charge power
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.dsmr_reading_electricity_currently_returned
condition:
  - condition: device
    device_id: 101a76a54dbacf224d0dc5c162c6b282
    domain: device_tracker
    entity_id: c243994ef91d5e1bb1db9a238cb642d2
    type: is_home
    enabled: true
  - type: is_charging
    condition: device
    device_id: 101a76a54dbacf224d0dc5c162c6b282
    entity_id: 8e906bbbff159a56ff8e19560f1b6fba
    domain: binary_sensor
    enabled: true
action:
  - if:
      - condition: numeric_state
        entity_id: sensor.dsmr_reading_electricity_currently_returned
        above: 7
    then:
      - device_id: 101a76a54dbacf224d0dc5c162c6b282
        domain: number
        entity_id: 219001a6b1d453519e627d6d7d044a3b
        type: set_value
        value: 10
    else:
      - if:
          - condition: numeric_state
            entity_id: sensor.dsmr_reading_electricity_currently_returned
            above: 6
            below: 7
        then:
          - device_id: 101a76a54dbacf224d0dc5c162c6b282
            domain: number
            entity_id: 219001a6b1d453519e627d6d7d044a3b
            type: set_value
            value: 9
      - if:
          - condition: numeric_state
            entity_id: sensor.dsmr_reading_electricity_currently_returned
            above: 5
            below: 6
        then:
          - device_id: 101a76a54dbacf224d0dc5c162c6b282
            domain: number
            entity_id: 219001a6b1d453519e627d6d7d044a3b
            type: set_value
            value: 7
      - if:
          - condition: numeric_state
            entity_id: sensor.dsmr_reading_electricity_currently_returned
            above: 4
            below: 5
        then:
          - device_id: 101a76a54dbacf224d0dc5c162c6b282
            domain: number
            entity_id: 219001a6b1d453519e627d6d7d044a3b
            type: set_value
            value: 6
      - if:
          - condition: numeric_state
            entity_id: sensor.dsmr_reading_electricity_currently_returned
            above: 3
            below: 4
        then:
          - device_id: 101a76a54dbacf224d0dc5c162c6b282
            domain: number
            entity_id: 219001a6b1d453519e627d6d7d044a3b
            type: set_value
            value: 5
        else:
          - device_id: 101a76a54dbacf224d0dc5c162c6b282
            domain: number
            entity_id: 219001a6b1d453519e627d6d7d044a3b
            type: set_value
            value: 0
mode: single

In the first If what I’m doing is that if the solar production is more than 7kW then adjust the charge amps to 10, the second if block is if the solar production is between 6-7kW adjust the charge amps to 9amps and I have the same until the ‘else’ where it set the amps to 0.

Since they are mutually-exclusive, instead of using multiple If/Then actions you should use a Choose action. In addition to the if/then/else allowed by the If/Then action, the Choose action allows else-if.

alias: charge power
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.dsmr_reading_electricity_currently_returned
condition:
  - condition: device
    device_id: 101a76a54dbacf224d0dc5c162c6b282
    domain: device_tracker
    entity_id: c243994ef91d5e1bb1db9a238cb642d2
    type: is_home
    enabled: true
  - type: is_charging
    condition: device
    device_id: 101a76a54dbacf224d0dc5c162c6b282
    entity_id: 8e906bbbff159a56ff8e19560f1b6fba
    domain: binary_sensor
    enabled: true
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.dsmr_reading_electricity_currently_returned
            above: 7
        sequence:
          - device_id: 101a76a54dbacf224d0dc5c162c6b282
            domain: number
            entity_id: 219001a6b1d453519e627d6d7d044a3b
            type: set_value
            value: 10
      - conditions:
          - condition: numeric_state
            entity_id: sensor.dsmr_reading_electricity_currently_returned
            above: 6
            below: 7
        sequence:
          - device_id: 101a76a54dbacf224d0dc5c162c6b282
            domain: number
            entity_id: 219001a6b1d453519e627d6d7d044a3b
            type: set_value
            value: 9
      - conditions:
          - condition: numeric_state
            entity_id: sensor.dsmr_reading_electricity_currently_returned
            above: 5
            below: 6
        sequence:
          - device_id: 101a76a54dbacf224d0dc5c162c6b282
            domain: number
            entity_id: 219001a6b1d453519e627d6d7d044a3b
            type: set_value
            value: 7
      - conditions:
          - condition: numeric_state
            entity_id: sensor.dsmr_reading_electricity_currently_returned
            above: 4
            below: 5
        sequence:
          - device_id: 101a76a54dbacf224d0dc5c162c6b282
            domain: number
            entity_id: 219001a6b1d453519e627d6d7d044a3b
            type: set_value
            value: 6
      - conditions:
          - condition: numeric_state
            entity_id: sensor.dsmr_reading_electricity_currently_returned
            above: 3
            below: 4
        sequence:
          - device_id: 101a76a54dbacf224d0dc5c162c6b282
            domain: number
            entity_id: 219001a6b1d453519e627d6d7d044a3b
            type: set_value
            value: 5
      default:
        - device_id: 101a76a54dbacf224d0dc5c162c6b282
          domain: number
          entity_id: 219001a6b1d453519e627d6d7d044a3b
          type: set_value
          value: 0
mode: single

FWIW, there are multiple ways to make an automation like this more compact using some templates.

Templated example
alias: charge power
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.dsmr_reading_electricity_currently_returned
condition:
  - condition: device
    device_id: 101a76a54dbacf224d0dc5c162c6b282
    domain: device_tracker
    entity_id: c243994ef91d5e1bb1db9a238cb642d2
    type: is_home
    enabled: true
  - type: is_charging
    condition: device
    device_id: 101a76a54dbacf224d0dc5c162c6b282
    entity_id: 8e906bbbff159a56ff8e19560f1b6fba
    domain: binary_sensor
    enabled: true
action:
  - variables:
      value: |
        {% set x = trigger.to_state.state | int(0) %}
        {% set d = {6: 9, 5: 7, 4: 6, 3: 5} %}
        {% if x >= 7 %} 
          10
        {% elif x < 3 %} 
          0
        {% else %}  
          {{ d.get(x) }}
        {% endif %}
  - service: number.set_value
    entity_id: 219001a6b1d453519e627d6d7d044a3b
    value: "{{ value }}"
mode: single
1 Like

@Didgeridrew, Thank you.