Using and testing Automation Trigger Variables

I’m attempting to use Automation Trigger Variables in an automation to detect the from_state and use it as part of a condition in the action session. It’s basically an automation triggered on octopus energy price detecting it’s in a certain price band but I want to know whether the price has decreased into the band or increased into it.

My yaml seems to always return false

1 - I’m sure I’ve errored the syntax somewhere, can anyone point out where?
2 - because I’m using trigger variables testing this/debugging seems impossible as the trigger is invalid when called from devtools service or templates. Is there a trick to debugging I’m missing?

Code segment, condition always seems to resolve to false, I am attempting to do a check for less than or greater than the last value

if:
- condition: template
    value_template: "{{ (trigger.from_state.state |int) - (trigger.to_state.state|int) > 0 }}"
then:
  - service: notify.notify
    data:
      message: >
        PowerCost reducing just changed from {{ trigger.from_state.state }} to
        {{ trigger.to_state.state }}
else:
  - service: notify.notify
    data:
      message: >
        PowerCost increasing just changed from {{ trigger.from_state.state }} to
        {{ trigger.to_state.state }}

Thanks in advance for any assistance/pointers

Looks ok except that you are truncating the prices to integers. Try floating point numbers. Also your indentation is not correct. Should be:

if:
  - condition: template
    value_template: "{{ trigger.from_state.state|float(0) > trigger.to_state.state|float(0) }}"

Simulate the trigger variable by adding this to the Template Editor. Adjust the state values to meet your testing requirements.

{% set trigger = {
  "from_state": {"state": "11.1"},
  "to_state": {"state": "12.3"}
  }
%}
1 Like

Thankyou so much for pointing out what was staring me in the face but I didn’t see. Using INT not FLOAT meant values rounded to nearest integer which meant in nearly all cases the values were then the same and therefore “>” was always false.

I was so busy looking for syntactical errors I missed the obvious!

Thanks, I’ll give this a go