Change boolean on device check

I now have a macro fuction that turns on multiple devices with a boolean_switch

input_boolean:
  watch_movie:
    name: Mediacenter
    initial: off
    icon: mdi:movie

As it is initial off, it is possible that the macro was called (all devices are on) but the switch is off.
Is it possible to toogle the switch state depending on the state of the corresponding devices?

e.g.:

if (media_player.a == 'on' && media_player.b == 'on' && media_player.a.source == 'TV' ...)
  homeassistant.turn_on
    entity_id: input_boolean.mediacenter
else
  homeassistant.turn_off
    entity_id: input_boolean.mediacenter

I would presume its in the automation part. Something like that?

  - alias: 'Change to ON Mode'
    trigger:
      platform: state
      entity_id: media_player.xy
      # not "from 'off' to 'on'" but "is 'on'"
      #where to add an if else?#
    action:
        service: input_boolean.turn_on
        entity_id: input_boolean.watch_movie

and vise versa

You can specify multiple triggers. The automation will run if any of these triggers fire.
You’d then need to add a condition section that uses and with a list of conditions.

Sebastian

Ah, okay, thanks for the link!
From what I’ve read I changed my code to:

- alias: 'Change to ON Mode'
    trigger:
      condition:
        - condition: state
          entity_id: media_player.a, media_player.b
          state: 'on'
        - condition: state
          entity_id: media_player.a.source ?????????
          state: 'htpc'       ??????????
    action:
        service: input_boolean.turn_on
        entity_id: input_boolean.watch_movie

I still dont know how to get the ??? Parts :slight_smile:

I am not 100% sure what you want to do here but the configuration you just posted should not work the condition block should be on the same level as trigger and action. Could you describe what you are trying to achieve here?

~Cheers

It should basically work like this:

trigger:
  - platform: state
    entity_id: media_player.a
	state: on
  - platform: state
	entity_id: media_player.b
	state: on
condition:
  - condition: and
	conditions:
	  - condition: state
	    entity_id:  media_player.a
		state: on
	  - condition: state
	    entity_id:  media_player.b
		state: on
	  - condition: template
		entity_id: media_player.a
		value_template: '{{ states.media_player.a.attributes.source == "TV"}}'
action:
  [...]

You probably can drop the “state: on” check for media player “a” from the conditions, because if it has a valid source (i.e. “TV”) this probably implies that it’s on.

Use the dev-tools section to check if the template returns the expected values.

Sebastian

1 Like

Thanks for all teh input! :smiley:

Well, what I actually try to do is. I have kodi on a htpc connected to an AVR.

I have conigured an input_boolean
see above

where an automation reacts to:

automation:
  - alias: Start Makro Mediacenter
    hide_entity: True
    trigger:
      platform: state
      entity_id: input_boolean.watch_movie
      from: 'off'
      to: 'on'
    action:
      service: script.turn_on
      entity_id: script.mediacenter_on

  - alias: Stop Makro Mediacenter
    hide_entity: True
    trigger:
      platform: state
      entity_id: input_boolean.watch_movie
      from: 'on'
      to: 'off'
    action:
      service: script.turn_on
      entity_id: script.mediacenter_off

This automation turns all the neccessary components on and off:

script:
  mediacenter_on:
    alias: mediacenter on
    sequence:
     - service: media_player.turn_on
       entity_id: 
        - media_player.avr_wohnzimmer
     - service: switch.turn_on
       entity_id:
        - switch.htpc
     - service: media_player.select_source
       data:
         entity_id: media_player.avr_wohnzimmer
         source: HTPC

  mediacenter_off:
    alias: mediacenter off
    sequence:
     - service: media_player.turn_off
       entity_id: 
        - media_player.avr_wohnzimmer
     - service: media_player.turn_off
       entity_id: 
        - media_player.kodi

Now I want to read the state of all controlled components to put the input_boolean switch in the correct state.
Sometimes if on component doesnt turn on or if I restart hass the boolean switch is in the wrong state and I cant be sure the state of the devices is the same as the switch. :slight_smile:

Well then sebk-666 solution should do the trick!

~Cheers

Thanks! I gave it a try :slight_smile:
But I get an error.

17-01-27 22:24:04 homeassistant.bootstrap: Invalid config for [automation]: expected str for dictionary value @ data['trigger'][0]['state']. Got None
extra keys not allowed @ data['condition'][0]['conditions'][0]['state']. Got None
not a valid value for dictionary value @ data['condition'][0]['conditions'][0]['condition']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 85). Please check the docs at https://home-assistant.io/components/automation/

My code:

automation:
  - alias: Start Makro Mediacenter
    hide_entity: True
    trigger:
      platform: state
      entity_id: input_boolean.watch_movie
      from: 'off'
      to: 'on'
    action:
      service: script.turn_on
      entity_id: script.mediacenter_on
  - alias: Stop Makro Mediacenter
    hide_entity: True
    trigger:
      platform: state
      entity_id: input_boolean.watch_movie
      from: 'on'
      to: 'off'
    action:
      service: script.turn_on
      entity_id: script.mediacenter_off

  - alias: Stop Makro Mediacenter
    hide_entity: True
    trigger:
      - platform: state
        entity_id: media_player.avr_wohnzimmer
        state: on
      - platform: state
        entity_id: media_player.kodi
        state: on
    condition:
      - condition: and
        conditions:
          - condition: state
            entity_id:  media_player.avr_wohnzimmer
            state: on
          - condition: state
            entity_id:  media_player.kodi
            state: on
          - condition: template
            entity_id: media_player.avr_wohnzimmer
            value_template: '{{ states.media_player.avr_wohnzimmer.attributes.source == "htpc"}}'
      action:
        service: input_boolean.turn_on
        entity_id:
         - input_boolean.watch_movie

This part of the code doesnt work:

    - condition: template
      entity_id: media_player.avr_wohnzimmer
      value_template: '{{state.media_player.avr_wohnzimmer.attributes.source == "htpc"}}'

I get error:

17-01-30 12:03:55 homeassistant.bootstrap: Invalid config for [automation]: not a valid value for dictionary value @ data['condition'][0]['conditions'][2]['condition']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 98). Please check the docs at https://home-assistant.io/components/automation/

It’s really hard to help you if you keep messing up the preformatted text. Try to copy paste your config and look at the preview befor submitting so we can see what you have exactly.

~Cheers

Im sorry for that but its nearly impossible for me to understand the behaviour of this damn editor :angry:

It doesnt matter if I click on the performatted text button or not besides changed indent.

EDIT : I forgot to make a screenshot of the preview. But it doesnt change and showed just normal text instead of code…

EDIT :
Okay I guess I got it. Seems there has to be a blank line before and after the code…

Sorry for all that try and error

I just use the triple top quotes to surround my preformatted text

```yaml
[preformatted text here]
```

works for me. First thing I noticed is you have a “entity_id” in a template condition which I do not know if this is possible ^^

~Cheers

Thank you very mouch! Works like a charm and makes things lot easier :smiley:

The entity_id was suggested in the post of @sebk-666 from above.

The complete part is:

  - alias: 'Change Mode on'
    trigger:
      - platform: state
        entity_id: media_player.avr_wohnzimmer
        state: 'on'
      - platform: state
        entity_id: media_player.kodi
        state: 'on'
      - platform: event
        event_type: homeassistant_start
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id:  media_player.avr_wohnzimmer
          state: 'on'
        - condition: state
          entity_id:  switch.htpc
          state: 'on'
       # - condition: template
         # entity_id: media_player.avr_wohnzimmer
         # value_template: '{{state.media_player.avr_wohnzimmer.attributes.source == "htpc"}}'
    action:
      service: input_boolean.turn_on
      entity_id:
        - input_boolean.watch_movie

But better would be to eliminate redundant conditions through source states:

  - alias: 'Change Mode on'
    trigger:
      - platform: state
        entity_id: media_player.avr_wohnzimmer
        state: 'on'
      - platform: state
        entity_id: switch.htpc
        state: 'on'
      - platform: event
        event_type: homeassistant_start
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id:  switch.htpc
          state: 'on'
        - condition: template
          entity_id: media_player.avr_wohnzimmer
          value_template: '{{state.media_player.avr_wohnzimmer.attributes.source == "htpc"}}' #implies AVR is ON
    action:
      service: input_boolean.turn_on
      entity_id:
        - input_boolean.watch_movie

Okay…so does it work now? Sorry a bit unclear right now ^^

~Cheers

Well, not really :sweat_smile:

First: The trigger doesnt seem to work. If I mannually trigger it the conditions work. Thanks.
But after a restart (the devices are on as for the conditions necessary) or when I change a device state nothing happens.
These automations only purpose is to show the correct “state” of my scene ("are all devices in the right state -or- “has the macro act correctly”)
-> The input_boolean.watch_movie triggers an automation which execute a script turning on different devices.

To dig deeper into it.
Maybe its the wrong way I try to control my scene?

The Problem I have or what I try to do:

  • I have various scenes/macros I want to control:
    – For instance: HTPC + AVR + AVR Souce + TV = Mediacenter aka. “whatch movies”
    – or: Dreambox (Enigma2) + AVR + AVR Source + TV = TV aka. “watch TV2”

What Im trying to do now is for one thing:

  • Turn all components on (and switch sources) via one button or switch
    – check if they are all on
    otherwise:
  • When one scene is active and another is activated - switch and turn off unnecessary devices
    – e.g.: from Mediacenter -> TV: HTPC:OFF, AVR:SOURCE:TV, Dreambox:ON (AVR and TV unchanged)

This is a bit more complex and I presume I havent the necessary phantasy to get a reliable solution :smiley: