So how does camera.play_stream really work?

Maybe, you can find my example usefull.

Thanks, it has a good flow. I like the logic. It’s not quite what I was looking for. In hind sight, I feel like I should have started this off differently. I was really looking for people to share thier automations (if they had any yet) that were similiar to the youtube video in the .90 go live post. I should have just started with this basic automation.

- alias: 'Garage Stream with Motion'
  trigger:
  - platform: state
    entity_id: binary_sensor.garage_motion
    to: 'on'
  condition:
  - condition: state
    entity_id: input_select.house_mode
    state: Home
  action:
  - service: camera.play_stream
    data:
      entity_id: camera.garage_camera
      media_player: media_player.living_room_tv

This will stream to my tv with motion in the garage. What it wont do, and what I was hoping that people who have used casting and HA might have some insight on is how to end a stream. Since I have not messed with casting much, I dont know what the capabilites are. I do know how to send a pause to my AV unit via harmony, I do know how to send a play command to my tv via harmony, I know how to turn on/off input booleans if needed. But with this being so new, I was hoping not to have to reivent the wheel. I’ve seen other people who cast to kodi, or plex, etc. maybe switching between inputs, etc. Are there any lessons learned, things that are hard, things that are easy and just work, etc.

Thanks everyone so far.

1 Like

Ok, so if I understood correctly, you are looking for a way to pause the current stream you are playing, stream you camera for a defined time and then resume the previous stream you where playing right?

1 Like

In this example, he is not pausing / resuming the stream, he is playing a film on the Xbox, and just switching the TV Source from XBOX HDMI 2 to CromeCast HDMI 1. As soon as the CromeCast stop the streaming, the TV shitch back to HDMI 2 automatically. So there is not magic behind on that regard.

1 Like

Oh, nice. That was not clear to me, even rewatching the video, but it makes sense. Well, boo, that wont work for me and my TV. I have a sony TV with build in cast capabilities. So in my case, watching apple TV on HDMI 2, then cast takes over.

Thanks for the insight though, since I have not casted much, I’ve never thought about the actual process. Hmm, maybe I can switch back to HDMI 2 after a bit and it will replace the cast. Makes, sense.

Thanks, that is exactly what I was trying to work through in my head

Thanks again for the info, here’s a decent automation. It’s untested, but probably pretty close. So answering my own question. Hope someone else finds this useful

- alias: 'Garage Stream with Motion'
  trigger:
  - platform: state
    entity_id: binary_sensor.garage_motion
    to: 'on'
    for:
      minutes: 30
  condition:
  - condition: state
    entity_id: input_select.house_mode
    state: Home
  action:
  - service: remote.send_command
    data:
      entity_id: remote.basement_remote 
      command:
        - Pause
      device: 17712304
  - service: camera.play_stream
    data:
      entity_id: camera.garage_camera
      media_player: media_player.living_room_tv
  - delay:
      seconds: 30
  - service: media_player.select_source
    data:
      entity_id: media_player.living_room_tv
      source: 'HDMI 2'
  - service: remote.send_command
    data:
      entity_id: remote.basement_remote 
      command:
        - Play
      device: 17712304

Details: I have the sony TV custom component for setting the HDMI (I could prob use harmony for that too, just have not checked yet). Also I prob need to do a template for the device to cover different sources. But the concept should be the same

2 Likes

something I just cooked,so totally untested

- alias: 'Garage Stream with Motion'
  trigger:
  - platform: state
    entity_id: binary_sensor.garage_motion
    to: 'on'
  condition:
  - condition: state
    entity_id: input_select.house_mode
    state: Home
  action:
# save current TV state
  - service: input_text.set_value
    data_template:
      entity_id: input_text.tv_source
      value: "{{ state_attr('media_player.tv','source') }}"
  - service: input_text.set_value
    data_template:
      entity_id: input_text.tv_media_content_id
      value: "{{ state_attr('media_player.tv','media_content_id') }}"
  - service: input_text.set_value
    data_template:
      entity_id: input_text.tv_media_content_type
      value: "{{ state_attr('media_player.tv','media_content_id') }}"
  - service: input_text.set_value
    data_template:
      entity_id: input_text.tv_media_content_position
      value: "{{ state_attr('media_player.tv','media_position') }}"
# add more of these as needed based on your setup / what you're watching
  - service: camera.play_stream
    data:
      entity_id: camera.garage_camera
      media_player: media_player.tv
# Wait for end of motion or 30 sec, whichever comes first
  - wait_template: "{{ is_state('binary_sensor.garage_motion', 'off') }}"
    timeout: '00:00:30'
    continue_on_timeout: 'true'
# Restore TV State
  - service: media_player.select_source
    data_template:
      entity_id: media_player.tv
      source: '{{states("input_text.tv_source")}}
  - service: media_player.play_media
    data_template:
      entity_id: media_player.tv
      media_content_id: '{{states("input_text.tv_media_content_id")}}
      media_content_type: '{{states("input_text.tv_media_content_type")}}'
  - service: media_player.media_seek
    data_template:
      entity_id: media_player.tv
      seek_position: '{{states("input_text.media_position")}}
5 Likes

I have the sequence, now just need to either template this out or create some scripts,but the flow is
Trigger = motion
action
a template based on this

{{ states.remote.living_room_harmony_hub.attributes.current_activity }} 

The activities I use are “watch tivo”, “apple tv”, or “Watch Smart TV”

So based on the current harmony activity

  • Send the pause command to the correct device
  • Send the play stream command to the tv (cast device)
  • delay for X seconds or motion off (would use a wait for this)
  • Then send the input command to the appropriate input (HMDI2, HDMI3, or “home”)
  • then send the play command to the correct device

I don’t think I need any input texts but that is definitely a way to go depending on your AV setup, in my case the state of the harmony never changes, so it’s just a matter of pulling the attribute.

I’ve done some testing from the services menu and it all works. I just did not know much about the casting when I started, now I realize it’s really just an “input” on the TV. so it would not be any different if someone changed the input to HDMI1 when I was watching hdmi2.

Hi all,

I have multiple cast devices set up as below but the stream only plays on last 2 devices. Anyone has the similar setup and how to resolve this?

    - service: camera.play_stream
      data:
         entity_id: camera.front
         media_player: 
          - media_player.tv_living
          - media_player.tv_bedroom
          - media_player.mibox3

Could this be a limitation of the device running the HA?
How much does a stream take on a device like Rpi3?

I don’t know if there’s a limit of streams. I run hassio on a Intel NUC with Virtual Machine because it’s faster and more reliable than rpi3

Is there a limitation to which media players support a stream? There is no documentation of that. The only thing relevant is Paulus’ tweet that states, “Stream any camera on any device.” So should I be able to call camera.play_stream on any media player? Because I’ve tried both my fire tv and roku and neither seems to work.

I totally agree… there’s no documentation about the supported media_playeres. I hope that will work for at least kodi. in future relese? For the moment NONE of my media Players work. I don’t have android TV or Chromecast. I use Kodi

When you stream to a Chromecast by using the: camera.play_stream. When you want the stream to stop, should you then just turn off the chromecast or is there a command like stop_stream or so?

Since you’re going to stream to a media_player device, you can simply turn off or stop the media_player indeed.

I do use the turn off, but nothing happens.
The following code doesn’t seem to work.

- alias: '[Frontyard] Cube Shake - Stream Camera'
  trigger:
  - event_data:
      action_type: shake_air
      entity_id: binary_sensor.cube_xxxxxxxxxxxx
    event_type: xiaomi_aqara.cube_action
    platform: event
  action:
    - service: media_player.turn_on
      entity_id: media_player.chromecast_kitchen
    - service: camera.play_stream
      data:
         entity_id: camera.drivein
         media_player: 
          - media_player.chromecast_kitchen
    - delay:
        seconds: 30
    - service: media_player.turn_off
      entity_id: media_player.chromecast_kitchen

It does turn on the Chromecast and then the screen shows the cast icon, but that’s about it.
No stream and it certainly doesn’t do the turn off command. I can afterwards manually in my lovelace click turn off and then follow my command.

I see this error though in my log:

Error while executing automation automation.frontyard_cube_shake_stream_camera. Unknown error for call_service at pos 2:
Traceback (most recent call last):
File “/usr/local/lib/python3.7/site-packages/homeassistant/components/automation/init.py”, line 380, in action
await script_obj.async_run(variables, context)
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py”, line 131, in async_run
await self._handle_action(action, variables, context)
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py”, line 210, in _handle_action
action, variables, context)
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py”, line 299, in _async_call_service
context=context
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/service.py”, line 88, in async_call_from_config
domain, service_name, service_data, blocking=blocking, context=context)
File “/usr/local/lib/python3.7/site-packages/homeassistant/core.py”, line 1138, in async_call
self._execute_service(handler, service_call))
File “/usr/local/lib/python3.7/site-packages/homeassistant/core.py”, line 1160, in _execute_service
await handler.func(service_call)
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity_component.py”, line 188, in handle_service
self._platforms.values(), func, call, service_name
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/service.py”, line 314, in entity_service_call
future.result() # pop exception if have
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/service.py”, line 330, in _handle_service_platform_call
await func(entity, data)
File “/usr/local/lib/python3.7/site-packages/homeassistant/components/camera/init.py”, line 644, in async_handle_play_stream_service
keepalive=camera_prefs.preload_stream)
File “/usr/local/lib/python3.7/site-packages/homeassistant/components/stream/init.py”, line 56, in request_stream
raise HomeAssistantError(“Stream component is not set up.”)
homeassistant.exceptions.HomeAssistantError: Stream component is not set up.

But, since I added the Ubiquiti Protect Camera as a Generic as described in the guide, I shouldn’t use the stream component as far as I could read?

What have I messed up?

I get the same error as above - homeassistant.exceptions.HomeAssistantError: Stream component is not set up when trying to launch via automation.
I can display the still image in the web UI in a picture card and when I click it I get a pop up with the streaming video, so I know that the video itself does work.
Camera is a Hikvision set up as generic; tried H264 and mjpeg.

Have anyone had luck turning your TV off with the Chromecast. Currently when I use media_player.turn_off it just stops whatever that is playing on the Chromecast and then it goes back to the Screensaver. But if I say the command directly to google “Hey Google, Turn off Chromecast XXX” then it turns off completely?

Is that not possible through HA?

1 Like

Trying to send the stream to Kodi gets me this:

Error calling async_play_media on entity media_player.kodi: TransportError("Error calling method 'Player.Open': Transport Error", TimeoutError())

So when I ask Google to stream to my Android TV one of my cameras this is what I’m getting any suggestions!