Avoiding Redundant Commands in Home Assistant Automations

Here is a refined version of your message for the forum:


Title: How to Avoid Redundant Commands in Automations When the State is Already Set?

Hi everyone,

I’m working on an automation that controls my inverter via RS485, and I’m encountering an issue where unnecessary commands are sent, even if the desired state is already achieved. Since the inverter is sensitive to receiving too many commands, I’d like the automation to only send a command if the current state or value is different from what it’s supposed to be.

What I want to achieve:

  1. Check the current state or value of an entity (e.g., select or number entities).
  2. Only send the command if the current state is different from the desired one. For instance, if the select entity is already set to Allow Export, I don’t want to send that command again.
  3. This behavior should be applied to multiple entities in sequence, but I need the automation to continue running even if one check fails. I don’t want the entire sequence to stop just because one condition is met.

Here’s an example of the current sequence I’m using:

sequence:
  - data:
      option: Allow Export
    action: select.select_option
    target:
      entity_id: select.ss_load_limit

  - target:
      entity_id:
        - select.ss_prog1_charge
        - select.ss_prog2_charge
        - select.ss_prog3_charge
        - select.ss_prog4_charge
        - select.ss_prog5_charge
        - select.ss_prog6_charge
    data:
      option: No Grid or Gen
    action: select.select_option

  - target:
      entity_id:
        - number.ss_prog1_capacity
        - number.ss_prog2_capacity
        - number.ss_prog3_capacity
        - number.ss_prog4_capacity
        - number.ss_prog5_capacity
        - number.ss_prog6_capacity
    data:
      value: "10"
    action: number.set_value

  - target:
      entity_id:
        - number.ss_prog1_power
        - number.ss_prog2_power
        - number.ss_prog3_power
        - number.ss_prog4_power
        - number.ss_prog5_power
        - number.ss_prog6_power
    data:
      value: "{{ (states('sensor.p_batt_forecast')|float(0)|abs / 0.95) }}"
    action: number.set_value

  - target:
      entity_id:
        - select.ss_grid_peak_shaving
    data:
      option: Disable
    action: select.select_option

  - target:
      entity_id: number.ss_battery_max_charge_current
    data:
      value: "0"
    action: number.set_value

Current Solutions I’ve Tried:

  • Using choose blocks for each action: This works, but it makes the automation complicated with repeated blocks of code.
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ states('select.ss_load_limit') != 'Allow Export' }}"
        sequence:
          - service: select.select_option
            target:
              entity_id: select.ss_load_limit
            data:
              option: Allow Export
    - condition: template
      value_template:>-
         {{ 
             states('select.ss_prog1_charge') != 'No Grid or Gen' or
             states('select.ss_prog2_charge') != 'No Grid or Gen' or
             states('select.ss_prog3_charge') != 'No Grid or Gen' or
             states('select.ss_prog4_charge') != 'No Grid or Gen' or
             states('select.ss_prog5_charge') != 'No Grid or Gen' or
            states('select.ss_prog6_charge') != 'No Grid or Gen'
         }}
    - service: select.select_option
      target:
        entity_id:
          - select.ss_prog1_charge
          - select.ss_prog2_charge
          - select.ss_prog3_charge
          - select.ss_prog4_charge
          - select.ss_prog5_charge
          - select.ss_prog6_charge
    data:
      option: No Grid or Gen
  • Using condition blocks: This isn’t ideal because the sequence stops when one condition isn’t met.
sequence: 
  - condition: template
    value_template: "{{ state_attr('select.ss_load_limit', 'option') != 'Allow Export' }}"
  - service: select.select_option
    target:
      entity_id: select.ss_load_limit
    data:
      option: Allow Export

  - condition: template
    value_template: "{{ state_attr('select.ss_prog1_charge', 'option') != 'No Grid or Gen' }}"
  - service: select.select_option
    target:
      entity_id:
        - select.ss_prog1_charge
        - select.ss_prog2_charge
        - select.ss_prog3_charge
        - select.ss_prog4_charge
        - select.ss_prog5_charge
        - select.ss_prog6_charge
    data:
      option: No Grid or Gen

My Ideal Solution:

It would be great to have an automation-wide option, like “run action if the value is not the same,” that applies to all actions, or something similar.

Has anyone else faced this problem? Do you have any suggestions on how to simplify this process and avoid sending redundant commands?

Thanks!


This version provides clear details about the issue, the solutions you’ve tried, and what you’re ideally looking for, making it easier for forum members to provide helpful insights.

What is the issue with sending a redundant command?

Create a script the takes the entity id and the value and sets it only if the requested value is different. Then call this script from your automations,

When sending too many commands inverter drops some and starting acting wierd.

Anyways I made the automation in nodered with checks of previous states and it’s working nicely.