Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rank-automation.yaml doesn't work #34

Open
velotelo opened this issue Feb 26, 2024 · 9 comments
Open

rank-automation.yaml doesn't work #34

velotelo opened this issue Feb 26, 2024 · 9 comments

Comments

@velotelo
Copy link

I'm curious whether I'm doing something wrong, but the rank-automation.yaml doesn't work for me at all.

The automation I've created form that blueprint doesn't ever get triggered automatically and when triggered manually always results in else clause being executed.

Having spent a full day on it, I found the root cause: those input_variables are not accessible inside the value_template of the trigger despite the fact they're defined as trigger_variables (which in theory should work fine). I've tried pretty much all the approaches suggested at https://community.home-assistant.io and I just couldn't find a way to make those variables available.

The only solution I've found was moving all the if-else logic into the action section, subsequently changing the trigger to just rank & price state changes and and also changing trigger_variables to variables. That way all the variables are nicely available and automation works as expected:

variables:
  rank: !input rank
  price: !input price
  on_attribute: !input on_attribute

trigger:
  - platform: state
    entity_id:
      - sensor.shf_rank_now
  - platform: state
    entity_id:
      - sensor.shf_electricity_price_now

action:
  - if:
    - condition: template
      value_template: |-
        {% if on_attribute == "Price OK" %}
          {{ states('sensor.shf_electricity_price_now') | float <= price | float }}
        {% elif on_attribute == "Rank OK" %}
          {{ states('sensor.shf_rank_now')|float <= rank | float }}
        {% elif on_attribute == "Price or Rank OK" %}
          {{ states('sensor.shf_electricity_price_now') | float <= price | float or states('sensor.shf_rank_now')|float <= rank | float }}
        {% elif on_attribute == "Price and Rank OK" %}
          {{ states('sensor.shf_electricity_price_now') | float <= price and states('sensor.shf_rank_now')|float <= rank | float }}
        {% endif %}
    then: !input action_on
    else: !input action_off
@zuiker1974
Copy link

I noticed same thing. Always goes to else.

@velotelo
Copy link
Author

velotelo commented Mar 3, 2024

I've been running the code I've suggested above for a week now – works like a charm.

@zuiker1974
Copy link

Even with these changes get always if condition false.

@velotelo
Copy link
Author

velotelo commented Mar 3, 2024

There should be no magic that it works for me flawlessly and doesn't work for you at all... Here's the full updated file I'm using, you can try it out:
rank-automation.yaml.zip

@zuiker1974
Copy link

Only rank works with this, but not price. Needs some debugging.

@velotelo
Copy link
Author

Both rank and price work for me, but there's another issue with this.
Whenever HA gets rebooted, all scripts based on rank-automation.yaml get executed minimum 5 times within 2 seconds because of following events:

  1. Rank changes from unavailable to unavailable
  2. Price changes from unavailable to unavailable
  3. Rank changes from unavailable to value
  4. Price changes from unavailable to value
  5. Price changes from value to same value

"Run" # 1 gets executed and naturally goes to the else clause. "Runs" # 2-5 get terminated, because #1 is being executed.

One fast solution which works with added limitation is setting the trigger to ignore state change to unavailable:

trigger:
  - platform: state
    entity_id:
      - sensor.shf_rank_now
      - sensor.shf_electricity_price_now
    not_to: unavailable

With this fix in place only events 3 and 4 from above result in script triggering, with # 3 going through all conditions and performing as expected and # 4 getting terminated. Interestingly, event 5 also disappeared with this fix.
However, if entity state becomes unavailable for some legit reason (i.e. not because of just executed reboot), that remains not handled by the script, that's the limitation.

Other option I see is modifying the else-clause as so (pseudo code):

    else: |-
      {% if trigger.from_state.state != "unavailable" and trigger.to_state.state != "unavailable" %}
        !input action_off
      {% endif %}

This particular code doesn't work and I'm not sure what am I missing here.

@DR00ME
Copy link

DR00ME commented Jun 7, 2024

Why this is not still fixed? I fought with this problem many hours and now I found this thread.

@DR00ME
Copy link

DR00ME commented Jun 24, 2024

Only rank works with this, but not price. Needs some debugging.

Remember the price is in euros. So for example 4.4c is 0.044 €

@osro
Copy link

osro commented Jul 31, 2024

For me the automation works, but it only when I trigger it manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants