Writing first If/Then Statement

Hi All,

I have been doing quite a bit of reading as I get deeper into learning YAML. I am attempting to write my first if/then statement and need a little help (so I can reverse engineer and understand the syntax).

Use Case: When someone ring my doorbell I want to display the camera on my family room TV (if the TV is turned on) and kitchen display. If the family room TV is not turned on then I just want to display on the kitchen display.

First, here is the code to properly display the camera to the kitchen display:

#------------------------------------------------------------------------------
# STREAM DOORBIRD CAMERA TO DISPLAY WHEN DOORBELL RINGS
#------------------------------------------------------------------------------
- alias: 'Doorbell'
#    hide_entity: true
  trigger:
    - platform: event
      event_type: doorbird_front_gate_somebody_pressed_the_button
  action:
    - service: camera.play_stream
      data:
        entity_id: camera.front_gate_live
        media_player: media_player.kitchen_display
    - delay: '00:00:10'
    - service: media_player.turn_off
      data:
        entity_id: media_player.kitchen_display

Next, here is where I am, so far, in trying to figure out the logic with it:

- alias: 'Doorbell'
#    hide_entity: true
  trigger:
    - platform: event
      event_type: doorbird_front_gate_somebody_pressed_the_button
  action:
    - service: camera.play_stream
      data_template:
        entity_id: camera.front_gate_live
        media_player: >
		    {% if is_state("media_player.family_room_tv","on") %}
              media_player.family_room_tv
			  media_player.kitchen_display
			{% else %}
   		      media_player.kitchen_display
			{% endif %}
    - delay: '00:00:10'
    - service: media_player.turn_off
      data_template:
	    entity_id: >
		    {% if is_state("media_player.family_room_tv","on") %}
			  media_player.family_room_tv
			  media_player.kitchen_display
			{% else %}
   		      media_player.kitchen_display
			{% endif %}

I realize I cannot just throw in two different media players for the if statement… I need some help here :slight_smile:

Thanks!

I think you would be better off using a condition for the family room tv and maybe moving it to two scripts. I would think you would keep the TV on if it was on to begin with, no?


  action:
    - service: script.camera_feed_on
    - delay: '00:00:10'
    - service: script.camera_feed_off
script:
  camera_feed_on:
    sequence:
      - service: camera.play_stream  
        data:
          entity_id: camera.front_gate_live
          media_player: media_player.kitchen_display
      - condition: 
          condition: state
          entity_id: media_player.family_room_tv
          state: 'on'
      - service: camera.play_stream  
        data:
          entity_id: camera.front_gate_live
          media_player: media_player.family_room_tv
script:
  camera_feed_off:
    sequence:
      - service: media_player.turn_off  
        data:
          entity_id: media_player.kitchen_display
      - condition: 
          condition: state
          entity_id: media_player.family_room_tv
          state: 'on'
      - service: media_player.turn_off  
        data:
          entity_id: media_player.family_room_tv
  action:
    - service: camera.play_stream
      data_template:
        entity_id: camera.front_gate_live
        media_player: >
          {% if is_state("media_player.family_room_tv","on") %}
            media_player.family_room_tv, media_player.kitchen_display
          {% else %}
            media_player.kitchen_display
          {% endif %}
    - delay: '00:00:10'
#... same format for the other multiple entity_id

Also, do not use TAB for spacing, use only spaces.

Awesome! Thanks for the quick reply… this gives me 2 great examples of how to enact this. Let me mess around and see what’s going to work best :slight_smile:

Not sure what is going on here, but whenever I add these scripts my system errors out:

Logger: aiohttp.server
Source: helpers/config_validation.py:732
First occurred: 11:36:56 AM (2 occurrences)
Last logged: 11:42:08 AM

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
    resp = await task
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/real_ip.py", line 39, in real_ip_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 72, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 127, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 123, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/config/core.py", line 29, in post
    errors = await async_check_ha_config_file(request.app["hass"])
  File "/usr/src/homeassistant/homeassistant/config.py", line 821, in async_check_ha_config_file
    res = await check_config.async_check_ha_config_file(hass)
  File "/usr/src/homeassistant/homeassistant/helpers/check_config.py", line 129, in async_check_ha_config_file
    config = config_schema(config)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 386, in validate_mapping
    cval = cvalue(key_path, value)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 443, in verify
    return cast(Dict, schema(value))
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 386, in validate_mapping
    cval = cvalue(key_path, value)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 386, in validate_mapping
    cval = cvalue(key_path, value)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 205, in _run
    return self._exec(self._compiled, value, path)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 285, in _exec
    v = func(path, v)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 635, in validate_sequence
    cval = validate(index_path, value)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 1026, in script_action
    return ACTION_TYPE_SCHEMAS[determine_script_action(value)](value)
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 732, in key_value_validator
    if key_value not in value_schemas:
TypeError: unhashable type: 'collections.OrderedDict'

UPDATE: It looks like something to do with the condition statements. Config won’t pass unless I take those out. Must be a formatting issue?

It’s possible it could be formatting. I looked at the examples in the docs and it should work as is, however the spacing will need to be corrected to match your config.

I unfortunately have not been able to get either of these suggestions to work. The first, using if/then:

- alias: 'Doorbell'
#    hide_entity: true
  trigger:
    - platform: event
      event_type: doorbird_front_gate_somebody_pressed_the_button
  action:
    - service: camera.play_stream
      data_template:
        entity_id: camera.front_gate_live
        media_player: >
          {% if is_state("media_player.family_room_tv","on") %}
            media_player.family_room_tv, media_player.kitchen_display
          {% else %}
            media_player.kitchen_display
          {% endif %}
    - delay: '00:00:10'
    - service: media_player.turn_off
      data_template:
        entity_id: >
          {% if is_state("media_player.family_room_tv","on") %}
            media_player.family_room_tv, media_player.kitchen_display
          {% else %}
            media_player.kitchen_display
          {% endif %}

If I execute the automation, it works but it otherwise doesn’t trigger from the actual doorbell press.

The second, using scripts. I tried to reformat it a bit to get it to at least load, and got to the following:

camera_feed_on:
  sequence:
  - data:
      media_player: media_player.kitchen_display
    entity_id: camera.front_gate_live
    service: camera.play_stream
  - condition: state
    entity_id: media_player.family_room_tv
    state: 'On'
  - data:
      media_player: media_player.family_room_tv
    entity_id: camera.front_gate_live
    service: camera.play_stream
camera_feed_off:
  sequence:
  - data: {}
    entity_id: media_player.kitchen_display
    service: media_player.turn_off
  - condition: state
    entity_id: media_player.family_room_tv
    state: 'on'
  - data: {}
    entity_id: media_player.family_room_tv
    service: media_player.turn_off
trigger: 
  - platform: event 
    event_type: doorbird_front_gate_somebody_pressed_the_button

Is this correct? I mean it may be, but I haven’t seen it before.

Yeah, positive this is fine. I was using it in my old automation that just displayed to 1 display:

- alias: 'Doorbell'
#    hide_entity: true
  trigger:
    - platform: event
      event_type: doorbird_front_gate_somebody_pressed_the_button
  action:
    - service: camera.play_stream
      data:
        entity_id: camera.front_gate_live
        media_player: media_player.kitchen_display
    - delay: '00:00:10'
    - service: media_player.turn_off
      data:
        entity_id: media_player.kitchen_display

If the first works from manually triggering (which bypasses the trigger), then the action section is working and the trigger is not.

On the second, the only thing that jumps out to me is the condition state needs to be lowercase. I’m assuming this is being programmed through the UI?