Custom Component: SkyQ Media Player

Thanks… i’ll correct both my errors and see if it will work.
As i said, i must improve my templating…

Changed my configs accordingly to yours, restarted HA and got this:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 420, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1933, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 2, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1600, in forgiving_as_timestamp
    raise_no_default("as_timestamp", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1402, in raise_no_default
    raise ValueError(
ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{% if not is_state_attr('sensor.skyq_schedule', 'skyq_scheduled_start', none) %}
  {{((state_attr('sensor.skyq_schedule', 'skyq_scheduled_start')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
{% else %}
  9999
{% endif %}' but no default was specified

Really don’t understand why it doesn’t work!

EDIT: Restarted again and got:

2022-11-28 11:56:02.400 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{% if not is_state_attr('sensor.skyq_schedule', 'skyq_scheduled_start', none) %}
  {{((state_attr('sensor.skyq_schedule', 'skyq_scheduled_start')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
{% else %}
  9999
{% endif %}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1597, in forgiving_as_timestamp
    return dt_util.as_timestamp(value)
  File "/usr/src/homeassistant/homeassistant/util/dt.py", line 132, in as_timestamp
    raise ValueError("not a valid date/time.")
ValueError: not a valid date/time.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 420, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1933, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 2, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1600, in forgiving_as_timestamp
    raise_no_default("as_timestamp", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1402, in raise_no_default
    raise ValueError(
ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{% if not is_state_attr('sensor.skyq_schedule', 'skyq_scheduled_start', none) %}
  {{((state_attr('sensor.skyq_schedule', 'skyq_scheduled_start')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
{% else %}
  9999
{% endif %}' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 536, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 422, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{% if not is_state_attr('sensor.skyq_schedule', 'skyq_scheduled_start', none) %}
  {{((state_attr('sensor.skyq_schedule', 'skyq_scheduled_start')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
{% else %}
  9999
{% endif %}' but no default was specified
2022-11-28 11:56:02.409 ERROR (MainThread) [homeassistant.helpers.template_entity] TemplateError('ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{% if not is_state_attr('sensor.skyq_schedule', 'skyq_scheduled_start', none) %}
  {{((state_attr('sensor.skyq_schedule', 'skyq_scheduled_start')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
{% else %}
  9999
{% endif %}' but no default was specified') while processing template 'Template("{% if not is_state_attr('sensor.skyq_schedule', 'skyq_scheduled_start', none) %}
  {{((state_attr('sensor.skyq_schedule', 'skyq_scheduled_start')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
{% else %}
  9999
{% endif %}")' for attribute '_attr_native_value' in entity 'sensor.skyq_minutes_to_next_recording'

I changed the template in this way and now i get the right result from the template:

  - sensor:
      - name: SkyQ minutes to next recording
        unique_id: sky_q_recording_minutes
        unit_of_measurement: "min"
        state: >-
          {% if state_attr('sensor.skyq_schedule', 'skyq_scheduled_start') is not none %}
            {{((state_attr('sensor.skyq_schedule', 'skyq_scheduled_start')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
          {% else %}
            9999
          {% endif %}

Now i get 9999 if i have no scheduled recording, but the markdown card is always empty…
If i disable the ‘Next Recordings’ section i get the markdown card showing only the first two sections ‘Recording Now’ and ‘Not Recording’ while leaving enabled also the ‘Next Recording’ section i have no card.

Sorry this is going beyond my knowledge of HA I’m afraid as it looks like some underlying issue with how it’s handling the code, I assume your HA is fully up to date is it, along with your Sky Q Integration?

Yes, all is at the last release…

Not sure then. Just to double check you do know the entities in the above snippets may have different names on your system, I assume you’ve checked all the entities in the code actually exist with the same names on your HA?

Maybe try removing all the as_timestamp and custom_timestamp formatting from the end of the markdown code and see what happens, it looks from your error log that its somehow not recognising the time you’re getting as a valid time.

Finally i found a solution.

This is what i use now:

                  content: >-
                    {% set recordings = state_attr('sensor.sky_q_schedule', 'skyq_recordings') %}
                    {% if recordings is sequence and recordings|length > 0 and recordings[0].skyq_recording_title is defined %}
                    **Recording Now:** {{ recordings[0].skyq_recording_title }}

                    {{as_timestamp (recordings[0].skyq_recording_start) | timestamp_custom('**Started:** %H:%M') }}

                    {{as_timestamp (recordings[0].skyq_recording_end) | timestamp_custom('**Ends:** %H:%M') }}
                    {% else %}

                    **Actually Not Recording**
                    {% endif %}

                    {% if state_attr('sensor.sky_q_schedule', 'skyq_scheduled_title') is not none %}
                                     
                    **Next Recording:** {{state_attr ('sensor.skyq_schedule', 'skyq_scheduled_title')}}

                    {{ as_timestamp(states.sensor.sky_q_schedule.attributes.skyq_scheduled_start) | timestamp_custom('Starts: %A %dth %H:%M') }} (in {{states ('sensor.minutes_next_sky_recording')}} minutes )
                    
                    {{ as_timestamp(states.sensor.sky_q_schedule.attributes.skyq_scheduled_end) | timestamp_custom('Ends: %A %dth %H:%M') }}
                    {% else %}
                    **Actually No Scheduled**
                    {% endif %}

Just a question: which is the meaning of ‘%A %dth’ ? Actually i get the hours of start and end one hour before the local hours. How to set the input to have the real local hours?

%A displays the day, %d is the date. As for the “th”, in my defence I did just copy/paste this from someone else’s code but the “th” appears to just put “th” on the end of the date which obviously works great for days in the month 4 and above, but I’ve never noticed it not be correct on the 1st, 2nd or 3rd :rofl:

Therefore either accept it will be grammatically incorrect on the first 3 days of each month or delete the “th” :+1:

As to your time being an hour out, I can only guess its something to do with the time being recorded in UTC time and I assume you’re not in the UK (although I’ll probably need to change it for BST in the summer).

Have a search on here for various ways of formatting to local time and have a play, but something like this might work, adding local=true to the end

timestamp_custom('Starts: %A %d %H:%M', local=True)

I am unable to get the buttons displayed to work. I press them & they do not do anything.
Am I missing code somewhere?

type: custom:mini-media-player
entity: media_player.sky_q
name: null
icon: phu:sky-q-logo
more_info: false
artwork: none
hide:
  volume: true
  source: true
  power_state: true
  play_pause: true
  prev: true
  next: true
  power: true
  info: true
  name: true
  icon: true
shortcuts:
  columns: 4
  hide_when_off: true
  buttons:
    - icon: mdi:home
      type: source
      id: Home
    - icon: mdi:arrow-up-thick
      type: source
      id: Up
    - icon: null
      name: Guide
      type: source
      id: TVGuide
    - icon: mdi:chevron-up
      type: source
      id: CHup
    - icon: mdi:arrow-left-thick
      type: source
      id: Left
    - icon: null
      name: Select
      type: source
    - icon: mdi:arrow-right-thick
      type: source
      id: Right
    - icon: mdi:chevron-down
      type: source
      id: CHdn
    - icon: mdi:record-rec
      type: source
      id: Record
    - icon: mdi:arrow-down-thick
      type: source
      id: Down
    - icon: null
      name: Sky
      type: source
      id: Sky
    - icon: mdi:menu-open
      type: source
      id: Sidebar
    - icon: phu:netflix
      type: js
      id: Netflix
      media_content_id: >-
        home,down,down,down,down,down,down,down,down,down,down,right,right,select
    - icon: phu:disney-plus
      type: js
      id: Disney Plus
    - icon: phu:prime-video
      type: js
      id: Prime Vidoe
    - icon: phu:youtube-tv
      type: js
      id: YouTube
volume_stateless: true
group: true
info: short
toggle_power: true
source: full

For that config to work you would need to configure each of those commands as custom sources, which I guess you haven’t done. Since you are using mini media player you can use the structure shown here - Buttons | Sky Q Home Assistant

Thanks - I thought I had tried that

example of my code for netflix - am I doing something stupid? I thought I might be missing code to put elsewhere.
The other buttons have started to work.

 - icon: phu:netflix
      type: js
      id: Netflix
      media_content_id: >-
        home,down,down,down,down,down,down,down,down,down,down,right,right,select

Is there a reason you are using type js, presumably for javascript? The id field is for the button commands you will send, Netflix isn’t a command, so you can’t use that, and you are mixing up a service call with the structure for mini media player.

    - icon: 'phu:netflix'
      id: home,down,down,down,down,down,down,down,down,down,down,right,right,select
      type: skyq

I can’t remember if id field needs to be quoted or not with multiple button presses. No idea if your icon works, but I assume it does.

Thanks for the pointer. Not 100% sure what I am doing as I am relatively new to HA.
I understand what you are saying about a service & js is being used because that is what I cut & pasted from elsewhere :smiley:

Are there any possibilities of being able to watch downloaded media from the Sky Q box directly via Home Assistant?

No, the box does not support streaming to anything other than the Sky Go app.

1 Like

I use Fully kiosk on my tablet wall display, as that can launch an android app direct from a button on the dash, I have it starting sky go, kind of a work around if thats why you want it.

Hi everyone,
I noticed that the media browser no longer works, both from a PC and from a smartphone. I use chrome browser and the latest version of HA. Do you also have the same problem?

Logger: homeassistant.components.websocket_api.http.connection
Source: custom_components/skyq/classes/mediabrowser.py:85
Integration: Home Assistant WebSocket API

Error handling message: Unknown error (unknown_error)

Thank you.

Please raise this as an issue on my Github and I’ll investigate.