AlexxIT / WebRTC

Home Assistant custom component for real-time viewing of almost any camera stream using WebRTC and other technologies.
https://github.com/AlexxIT/Blog
MIT License
1.48k stars 174 forks source link

passing variable to script in shortcut #561

Open robmwalsh opened 1 year ago

robmwalsh commented 1 year ago

Firstly, thanks for a great project! It's taken my cameras from basically useless using the default HA streams to rock solid!

I'm trying to combine a modified version of the temp link script with a shortcut to generate links directly from the cards.

my script (modified from your example) creates a link based on the camera_id variable

'create_temp_cam_link':
  alias: Create temporary camera link
  sequence:
  - variables:
      link_id: '{% for _ in range(40) %}{{ range(10)|random }}{% endfor %}'
      camera_id:
  - service: webrtc.create_link
    data:
      link_id: '{{ link_id }}'
      url: '{{ camera_id }}'
      open_limit: 1
      time_to_live: 300
  - service: persistent_notification.create
    data:
      message: <a href="/webrtc/embed?url={{ link_id }}" target="_blank">Open Camera
        {{ camera_id }}</a>

here's my card config, once again adapted from the example in the docs - I'm trying to pass the camera_id variable in to the script in service_data.

type: custom:webrtc-camera
ui: true
streams:
  - url: front_north
    name: SD
    mode: webrtc
  - url: front_north_hd
    name: HD
    mode: webrtc
shortcuts:
  - name: share
    icon: mdi:share
    service: script.create_temp_cam_link
    service_data:
      variables:
        camera_id: front_north

The script trace shows that the webrtc.create_link service is called with a null url, suggesting the script camera_id variable isn't set when the script is called. I've tried many variations on the theme using service_data but can't get it to work. providing the variable in the script does work but means that I'd need to create a script per camera which I'd like to avoid if possible.

Executed: September 1, 2023 at 6:56:24 PM
Result:
params:
  domain: webrtc
  service: create_link
  service_data:
    link_id: 6.109331278690482e+39
    url: null
    open_limit: 1
    time_to_live: 300
  target: {}
running_script: false

I'm using go2rtc via frigate and consuming the cameras from the HA integration which is all working very nicely. any hints very much appreciated! Please do let me know if you need any further info

AlexxIT commented 1 year ago

It's hard task because jinja can be calculated only on server side