Test conditions continually and switch a sensor based on the outcome

Hi I created an automation to switch my Inverter to Mode : On when the total home load is above 7000W. Once this mode is selected I need HA to monitor the total load and when the load stays below 7000W for 15 min, I need HA to switch to Mode : Inverter. This automation needs to run continually to select the Mode based on the load conditions. (>7000W Mode : ON, <7000W for 15min Mode : Inverter)

Below my code, but there is a problem, the automation switch to Mode : ON, but not back to Mode : Inverter every time once 15min lapsed.

Assistance would be greatly appreciated


alias: Select Victron Mode based on load
description: ""
trigger:
  - type: power
    platform: device
    device_id: da23db8f10cee165941854f092113eee
    entity_id: e1a93ca9544af90d2b7a64036268623b
    domain: sensor
    above: 7000
  - type: power
    platform: device
    device_id: da23db8f10cee165941854f092113eee
    entity_id: e1a93ca9544af90d2b7a64036268623b
    domain: sensor
    below: 7000
    for:
      hours: 0
      minutes: 15
      seconds: 0
    enabled: true
condition: []
action:
  - choose:
      - conditions:
          - type: is_power
            condition: device
            device_id: da23db8f10cee165941854f092113eee
            entity_id: e1a93ca9544af90d2b7a64036268623b
            domain: sensor
            above: 7000
          - condition: and
            conditions:
              - condition: device
                device_id: da23db8f10cee165941854f092113eee
                domain: select
                entity_id: 830da40ad97a44dcf2a8db9714558083
                type: selected_option
                option: INVERTER
        sequence:
          - device_id: da23db8f10cee165941854f092113eee
            domain: select
            entity_id: 830da40ad97a44dcf2a8db9714558083
            type: select_option
            option: "ON"
      - conditions:
          - type: is_power
            condition: device
            device_id: da23db8f10cee165941854f092113eee
            entity_id: e1a93ca9544af90d2b7a64036268623b
            domain: sensor
            below: 7000
          - condition: and
            conditions:
              - condition: device
                device_id: da23db8f10cee165941854f092113eee
                domain: select
                entity_id: 830da40ad97a44dcf2a8db9714558083
                type: selected_option
                option: "ON"
        sequence:
          - device_id: da23db8f10cee165941854f092113eee
            domain: select
            entity_id: 830da40ad97a44dcf2a8db9714558083
            type: select_option
            option: INVERTER
mode: single

please see this info on how to format the code so that it’s readable for us to be able to help.

1 Like

Apologies, I missed the format

did you construct this in yaml? or did you use the ui to construct? curious because of the “and” constructs…

you should avoid device id’s:

which not only will help you, but it also helps us edit your code (because home assistant front end will nuke device id’s that don’t exist in our system).

i definitely see a number of issues in your code. the “and” condition isn’t correct
the format is here:

note how the “and” comes before all the actual conditions… as a result you’re not getting an “and”… you’re effectively getting an “or” in your conditions.

by the way, you shoudln’t need to check the power sensor state again. you can use trigger id’s to know what triggered it (whether it was triggered because you were above or below the threshold).

if you post up an entity_id code version instead of device id, it’d be easier for me to edit it for you if you’d like.

Here’s another way to compose your automation.

Replace the following with the entity_id of your actual sensor and select entities.

  • sensor.your_power_sensor
  • select.your_mode_selector
alias: Select Victron Mode based on load
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.your_power_sensor
    above: 7000
    variables:
      is_true: "{{ is_state('select.your_mode_selector', 'INVERTER') }}"
      cmd: 'ON'
  - id: 'ON'
    platform: numeric_state
    entity_id: sensor.your_power_sensor
    below: 7000
    for:
      minutes: 15
    variables:
      is_true: "{{ is_state('select.your_mode_selector', 'ON') }}"
      cmd: 'INVERTER'
condition:
  - condition: template
    value_template: "{{ is_true }}"
action:
  - service: select.select_option
    target:
      entity_id: select.your_mode_selector
    data:
      option: "{{ cmd }}"
mode: single

Thank you so much, works 100%. Much apreciated!

I Used UI, not a programmer by any means, Taras code worked, I used the actual sensor and selector.

Just to confirm, below is my (your) code, is this correct?

alias: Select Victron Mode based on load 2.0
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.victron_vebus_out_l1_power_227
    above: 7000
    variables:
      is_true: "{{ is_state('select.victron_vebus_mode_227', 'INVERTER') }}"
      cmd: "ON"
  - id: "ON"
    platform: numeric_state
    entity_id: sensor.victron_vebus_out_l1_power_227
    below: 7000
    for:
      minutes: 15
    variables:
      is_true: "{{ is_state('select.victron_vebus_mode_227', 'ON') }}"
      cmd: INVERTER
condition:
  - condition: template
    value_template: "{{ is_true }}"
action:
  - service: select.select_option
    target:
      entity_id: select.victron_vebus_mode_227
    data:
      option: "{{ cmd }}"
mode: single

Yes, you have substituted the entity_id values correctly.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.