Help with a simple script to catch my UPS status

Hello everyone,
This is my first post and my first script.
I made a simple script to transfer my UPS status to my Polisy unit.
On the Polisy I created a state variable that I can write to from the HA script. Simple.
Here is the script I have so far:

alias: UPS Status
sequence:
  - if:
      - condition: state
        entity_id: sensor.cp1350avr_status
        state: Online
    then:
      - service: isy994.set_variable
        data:
          name: sUPSStatus
          value: 1
    else:
      - service: isy994.set_variable
        data:
          name: sUPSStatus
          value: 4
mode: single

The problem I have is the value of sensor.cp1350avr_status lists all active states in the same string.
For example, I can have:
ā€œOnlineā€ or ā€œOnline Battery Chargingā€ or ā€œOnline Battery Charging Low Batteryā€ or ā€œOn Battery Battery Dischargingā€, etc.
Iā€™d like to be able to separate each state.
For example, is there a way to find ā€œOnlineā€ in the state string?

Could you maybe use the choose option instead of an ā€˜ifā€™

That way you could list out each individual state and have differing actions based on the state?

you can use a template condition:

alias: UPS Status
sequence:
  - if:
      - condition: template
        value_template: "{{ 'Online' in states('sensor.cp1350avr_status') }}"
    then:
      - service: isy994.set_variable
        data:
          name: sUPSStatus
          value: 1
    else:
      - service: isy994.set_variable
        data:
          name: sUPSStatus
          value: 4
mode: single

If the word ā€œOnlineā€ (case sensitive) appears anywhere in the state then it will return true.

Thank you finity, I looked at the choose option, but the template fits better my need. Iā€™ll have to play a bit with to get what I want in the end.

1 Like

@finity Thanks again. The template condition works. Iā€™d like to go a bit further and combine two UPS status condition into one ISY variable. For example, Iā€™d like to do something like:

{{ 'Battery Charging' or 'Battery Discharging' in states('sensor.cp1350avr_status') }}

I tried it and it doesnā€™t work. I always get a false result.
How should I modify the value_template?

Itā€™s looking for that exact string contained within the state string.

So for example you had a sentence (a string):

ā€œthe state of the entity canā€™t be both Battery Charging and Battery Discharging at the same timeā€

to construct a template to check for either of those phrases you would do this:

{% set string = "the state of the entity can't be both Battery Charging and Battery Discharging at the same time" %}

{{ 'Battery Charging'  in string or 'Battery Discharging' in string }}

in this case it would return true (finds both phrases).

but if you changed it to this:

{% set string = "the state of the entity can't be both Battery is Charging and Battery is Discharging at the same time" %}

{{ 'Battery Charging'  in string or 'Battery Discharging' in string }}

the template returns false (it finds neither phrase).

or:

{% set string = "the state of the entity can't be both Battery Charging and Battery is Discharging at the same time" %}

{{ 'Battery Charging'  in string or 'Battery Discharging' in string }}

would be true (it finds ā€œBattery Chargingā€ but not ā€œBattery Dischargingā€).

you can play around with it in the dev tools template editor to find the right combinations.

Thanks @finity, before reading your post I tried a few things that didnā€™t work because I didnā€™t have the proper syntax. I was editing directly in YAML. I tried the scriptā€™s web interface and realized that the proper syntax is like this:

  - if:
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ ''High Battery'' in states(''sensor.cp1350avr_status'') }}'
          - condition: template
            value_template: >-
              {{ 'Battery Needs Replaced' in states('sensor.cp1350avr_status')
              }}
          - condition: template
            value_template: '{{ ''Bypass Active'' in states(''sensor.cp1350avr_status'') }}'
          - condition: template
            value_template: '{{ ''Runtime Calibration'' in states(''sensor.cp1350avr_status'') }}'
          - condition: template
            value_template: '{{ ''Offline'' in states(''sensor.cp1350avr_status'') }}'
          - condition: template
            value_template: '{{ ''Overloaded'' in states(''sensor.cp1350avr_status'') }}'
          - condition: template
            value_template: '{{ ''Trimming Voltage'' in states(''sensor.cp1350avr_status'') }}'
          - condition: template
            value_template: '{{ ''Boosting Voltage'' in states(''sensor.cp1350avr_status'') }}'
          - condition: template
            value_template: '{{ ''Alarm'' in states(''sensor.cp1350avr_status'') }}'
    then:
      - service: isy994.set_variable
        data:
          name: sUPSProblem
          value: 1
    else:
      - service: isy994.set_variable
        data:
          name: sUPSProblem
          value: 0

Thanks again for your help.

1 Like