Create binary sensor from ups charging sensor

Hi
I’ve not been able to achieve what I need:

Testing the following templates work:
{{states(‘sensor.qnapups_status_data’) == ‘OL’}} returns true if UPS is online
{{states(‘sensor.qnapups_status_data’) != ‘OL’}} returns false if it is online

so far so good. The problem is I need a binary sensor (to add to a groups of ping binary sensors, to trigger an alarm) using this template.
I’ve added this to my templates.yaml:

- binary_sensor:
    - unique_id: UPS_on_battery
      name: "UPS on battery"
      value_template: "{{states('sensor.qnapups_status_data') != 'OL'}}"

but I get an error.
I do have also a binary_sensors.yaml and the following in the configuration.yaml:

binary_sensor: !include binary_sensors.yaml
template: !include templates.yaml

Help much appreciated on how and where to create the binary sensor.
I hav searched the forum but have been able to achieve this.

Hi, I’m newbie, but I think that the device_class is missing: Binary sensor - Home Assistant

Device class is optional. The issue is they have mixed up new and legacy configuration options. It should be:

- binary_sensor:
    - unique_id: UPS_on_battery
      name: "UPS on battery"
      state: "{{states('sensor.qnapups_status_data') != 'OL'}}"

Next time include the full error text in your post. It helps when working out what is wrong. I’m guessing it was something like “value_template is not a valid option”.

The error was about a missing “state”. I had tried with state: instead of value_template but to no avail.
Problem is as you state change in configuration option and the examples I found were mixed.

Your code seems to work. Thanks a lot!

Not in the documentation (the primary source you should be referencing). Forum posts and youtube tutorials go out of date very quickly.

True! But sometimes I don’t understand it all from the docs :wink: Not a coding junky and this yaml stuff is very new to me.

Think of it more like a recipe than coding.

Each integration has a list of ingredients (options). The documentation lists these ingredients.

Quite, but then, only a good baker makes a tasty bread, others buy a japanese bread making machine :smile:
My coding experience (Basic, Pascal, Modula2, assembler) is 40 years ago :flushed: so all bear with me asking dumb questions in the future.

1 Like

Sorry but I need your help again:

The binary sensor is working fine I just need an addition, if the UPS is online and charging it shows “OL CHRG” instead of “OL”
So I need to change the binary sensor with an AND condition checking for both states:

- binary_sensor:
    - unique_id: UPS_on_battery
      name: "UPS on battery"
      state: "{{states('sensor.qnapups_status_data') != 'OL'}}"

something like {{states(‘sensor.qnapups_status_data’) != ‘OL’}} AND {{states(‘sensor.qnapups_status_data’) != ‘OL CHRG’}}

Can’t get it working…

Like this:

- binary_sensor:
    - unique_id: UPS_on_battery
      name: "UPS on battery"
      state: "{{ 'OL' not in states('sensor.qnapups_status_data') }}"
1 Like

Changed it to

{{ 'OL' in states('sensor.qnapups_status_data') }}

You rock! This was quick.

Thank you so much.

1 Like