Variables: Global variable for automation, being written by IFs

Below is a skeleton of my final code… essentially I need to count the total kW of AC that is running at any point in time. But that will be dependant on various factors.

I have set a variable (KWDemand) at the beginning of my script hoping that this will be accessible to everything.
Then I have an IF statement that is calculating the kW for an AC Headunit, and adding this to the variable.
Finally I am writing this to an Input Number.

I know the IF statement is working correctly; but when I try to write this out the result is always zero.

I assume this is something to do with the scoping, but I cannot figure out how to fix it. Can anyone provide any advice?

Note:

  • Yes, I have tried reading the docs
  • Yes, I have been searching through the forums and Google.
alias: ACKWCapacity Calculator
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.bacnet_startstopstatus_020
conditions: []
actions:
  - variables:
      KWDemand: 0
  - if:
      - condition: state
        entity_id: binary_sensor.bacnet_startstopstatus_020
        state: "on"
    then:
      - variables:
          KWDemand: "{{ KWDemand + 7.1 | float}}"
  - action: input_number.set_value
    target:
      entity_id: input_number.ackwcapacity
    data:
      value: "{{ KWDemand | float}}"
mode: single

There’s a good post about scoping here:

If you want global variables, I think you have to use something like Variables+History:

1 Like

Thanks for that it is a really good example (If only that was the official example!!!)

Although I am still having some issues:

  • I first changed it to the “var_3” example, but this did not seem to have any affect. Also the UI editor keeps moving the variables section to the end which is not intuitively correct from my view.
  • So then I switched to the version in his sample automation by putting it all in a sequence which has a nice side affect of making it more maintainable.

But I am still left with it writing out the contents of 0 (Or whatever I put in the variable instantiation.
I am left wondering if on line 17 I am creating a new variable that is then local to the IF statement, rather than updating the original copy…

alias: ACKWCapacity Calculator
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.bacnet_startstopstatus_020
conditions: []
actions:
  - sequence:
      - variables:
          KWDemand: 0
      - if:
          - condition: state
            entity_id: binary_sensor.bacnet_startstopstatus_020
            state: "on"
        then:
          - variables:
              KWDemand: "{{ KWDemand + 7.1 }}"
      - action: input_number.set_value
        target:
          entity_id: input_number.ackwcapacity
        data:
          value: "{{ KWDemand }}"
mode: single

<<After reading what I wrote, and reading the docs again…>>

Looking at it again just after writing that, I can see what I am doing wrong;. I am defining a new variable when I set 7.1… but I cannot figure out in HA how to just write to the original variable.

Hi Wombo1,

You need a global variable or you need to use namespace in a template.

A helper is a global variable, or this:
Trigger based template sensor to store global variables.

That is really to need to add a helper for a variable that is only within the scope of an automation… I’ve never seen a programming language like this in my life!!!

I’m really shocked!!! :frowning:

Vote here…

1 Like

voted for this one!!! Definitely WTH