Help create a value_template to return false if any input booleans are on

I’m trying to build a sensor that will check the status of several input booleans. If any of them are on, the sensor should return true.

I’ve never really done any programming or coding before, so please forgive my butchery of this code. I’m trying here to insert my (likely flawed) understanding of the required logic in the context of the value_template… So does the below make sense as to what I’m trying to achieve?

    should_receiver_be_on:
      friendly_name: "Should Reciever Be On?"
      value_template: '{{ states.input_boolean.appletv OR states.input_boolean.nintendo_switch OR states.input_boolean.nintendo_wiiu OR states.input_boolean.nintendo_switch | ARE ON? }}'

and if this does make sense, could anyone please help get the logic & syntax right?

You could just group the input_booleans, then if one is on, the group is on and you could display that group’s state in a card?

If you want the ‘true’ state to trigger something, then you can just create an automation that is triggered by any of those input_booleans turning on.

Otherwise, the value_template could be something like: -

{% if is_state('input_boolean.appletv', 'on') or is_state('input_boolean.nintendowiiu', 'on') or etc. etc. %} true {% else %} false {% endif %} 

If you use the Template editor in Developer Tools, you’ll be able to check if it is evaluating correctly.

1 Like

Wow thanks for the super fast reply Adam. I really appreciate how you provided a couple potentially simpler approaches before giving a specific answer to my problem. I posted this after a long evening trying to get this and several other parts of my system up and running, so I need to take myself to bed now. I’ll have a go at implementing your automation suggestion tomorrow. For now, I just wanted to say thanks for the help.

1 Like

The suggestion to use a group is ideal for your requirements.

However, if you want to do it without employing a group, I suggest this Template Binary Sensor:

    should_receiver_be_on:
      friendly_name: "Should Receiver Be On?"
      value_template: >
        {{ [states.input_boolean.appletv, states.input_boolean.nintendo_switch, 
             states.input_boolean.nintendo_wiiu, states.input_boolean.nintendo_switch]
           | selectattr('state', 'eq', 'on') | list | count > 0 }}

Other than ask question on here, what would I type in a search engine to learn more about examples like this :point_up_2:t2: please i.e. what language is this? I’m not looking to get to the level of understanding the likes of you , Petro etc have, I’m guessing you’ve all studied this for a long time. I’m just interested to expand the art of the possible.

But won’t this say false if all is on?

It’s a templating language called Jinja2.

Jinja is a fast, expressive, extensible templating engine. Special placeholders in the template allow writing code similar to Python syntax. Then the template is passed data to render the final document.

It’s use in Home Assistant is described here:

Thanks. I was just about to delete my question as I noticed it says that right at the top of the Template editor as well! :man_facepalming:t2: Got to get my eyes checked!!

No I don’t think so because the if the first condition is True the the template just skips past the other input_booleans and evaluates to True, it doesn’t even listen to the others. However, if the first is False, it listens to that input_boolean and every subsequent one until it finds a condition that is True.

I love how the Template editor shows you what entities are being listened to now! Very helpful.

no, if anyone of them are on it would be true. If all of them are on jinja would evaluate the first statement and exit before even checking the others.

1 Like

I was thinking of xor by some reason.
My bad.

Thank you all so much, I was just able to get great functionality working using groups of input booleans. There was a little bit of frustration where I’d used capitalisation on “On” and “Off” and I spent way too long figuring out that was my error, but I got there I the end.

Now I have a nice setup where all I need to do is choose an input source and all my other home theatre equipment (projector and receiver) will turn on with the right source selected. This is all done via IR on a Broadlink RM Mini 3. When I choose a different input source in my group, the setup knows to leave the projector and receiver on. Those components only turn off if the whole group is off.

If anyone is interested, here are the automations I ended up with.

The only annoying thing is that when everything is off, hitting the group switch does awful awful things because it turns everything on, triggering a whole lot of nasty. Does anyone know if there’s a way to dictate which input booleans are turned on by default when a group is activated?

Glad its working for you. Scene’s might be a good place to look for your desired behaviour :+1:

Yes, I’d take a completely different approach. Make template switches instead of automations. Seeing as this is like harmony activities, only 1 can be on at a time, make an input_select.

input_select

input_select:
  rm_mini:
    name: Current Activity
    initial: 'Off'
    options:
    - Apple TV
    - Wii U
    - PlayStation
    - Nintendo Switch
    - 'Off'

input_boolean

input_boolean:
  home_theatre:

example script for apple tv and home theatre

script:
  turn_on_home_theatre:
    sequence:
      - condition: state
        entity_id: switch.home_theatre
        state: 'off'

      ... Add your home theatre turn on here, couldn't find in current automations...
  turn_off_home_theatre:
    sequence:
      - action: input_boolean.turn_off
        entity_id: input_boolean.home_theatre
      - service: remote.send_command
        data:
          entity_id: remote.rm_mini_3_lounge_room_remote
          command: b64:JgBQAAABKJMTExITEhQRFBEUERMSFRAUEhMTEhIUERMTOBE5ExISExITEzcSFBEUEhMSFBISEhQROREUEjgSOBM3EjgTNxI4EgAF9gABJ0oTAA0FAAAAAAAAAAA=
      - delay: 00:00:02
      - service: remote.send_command
        data:
          entity_id: remote.rm_mini_3_lounge_room_remote
          command: b64:JgBQAAABKJMTExITEhQRFBEUERMSFRAUEhMTEhIUERMTOBE5ExISExITEzcSFBEUEhMSFBISEhQROREUEjgSOBM3EjgTNxI4EgAF9gABJ0oTAA0FAAAAAAAAAAA=
      - delay: 00:00:01
      - service: remote.send_command
        data:
          entity_id: remote.rm_mini_3_lounge_room_remote
          command: b64:JgBgAAABJpQSExI4EjgSOBI3EjgUNhITEjgSExITEhMSExITEhMSOBITEjgSExI4ERQSNxITEhMSOBITEjgUERI4EhMSOBI4EgAFFwABJksSAAxUAAElSxIADFQAASZKEgANBQAAAAAAAAAA=

  turn_on_apple_tv:
    sequence:
      - action: input_select.select_option
        data:
          entity_id: input_select.rm_mini
          option: Apple TV
      - action: switch.turn_on
        entity_id: switch.home_theatre
      - action: remote.send_command
        data:
          entity_id: remote.rm_mini_3_lounge_room_remote
          command: b64:JgBYAAABJpUSExE4ExISOBI4EjgSOBITEjgSExI4EhMRFBETEhQROBM3EjgSExITEhMSExITEhMSExITEjgSOBI4ETgSOBITEgAFPQABJkoSAAxUAAEmSxIADQU=

  turn_off_apple_tv:
    sequence:
      - action: input_select.select_option
        data:
          entity_id: input_select.rm_mini
          option: 'Off'
      - action: switch.turn_off
        entity_id: switch.home_theatre

example template switch for apple tv and home theatre

switch:
- platform: template
  switches:
    home_theatre:
      value_template: "{{ is_state('input_boolean.home_theatre', 'on') }}"
      turn_on:
        action: script.turn_on_home_theatre
      turn_off:
        action: script.turn_off_home_theatre

    apple_tv:
      value_template: "{{ is_state('input_select.rm_mini', 'Apple TV') }}"
      turn_on:
        action: script.turn_off_apple_tvb64:JgBYAAABJpUSExE4ExISOBI4EjgSOBITEjgSExI4EhMRFBETEhQROBM3EjgSExITEhMSExITEhMSExITEjgSOBI4ETgSOBITEgAFPQABJkoSAAxUAAEmSxIADQU=
      turn_off:
      - action: input_select.select_option
        data:
          entity_id: input_select.rm_mini
          option: 'Off'
      - action: switch.turn_off
        entity_id: switch.home_theatre

So basically, you’ll end up with switches that you use for the UI. The value_template for the switch will determine if the switch is on or off. So you don’t need to keep the other switches in sync by turning them off. They’ll turn off based on the value_template returning true or false, which is updated when the input_select changes.

1 Like

Oh wow, this sounds cool petro, I’ll take a crack at fully understanding and implementing your solution tomorrow. Thanks for taking a bit of a deeper dive into this with me. I’ll let you know how I go. Also, thanks again Adam for your help too :slight_smile:

Hey Petro, I just wanted to extend a huge thank you for your ideas here. This is really helpful stuff, and turns out to be perfect for my situation, so again, Thanks :smiley: