Tvh and kodi - TV zapper according to voice command topic

Hi there!

I’m having a lot of fun with Homeassistant media-wise and wanted to share this small project that:

  • Takes a voice command: e.g. who currently boadcasts: “Topic”
  • Searches throug the epg for the Topic
  • zappes throug the tv channels containing the Topic in title or description or …

My setup:

  • HA + TVheadend running on central server in different docker-container
  • No TVHeadined rec integration installed (HACS)
  • Kodi running on a raspi, connected to a projector

Setup this sensor that queries the epg from tvh:

### TVH
  - platform: rest
    name: Tvh_EPG
    #this only gets the currently running epg entries
    resource_template: "http://192.168.178.59:9981/api/epg/events/grid?mode=now"
    method: GET
    headers:
      Content-Type: "application/json"
    #the scan interval is set to very high since the sensor is triggered every time the script runs
    scan_interval: 360000

Then I have the following script to find the channels + zapp through them

alias: Zappe durch Sender
description: Zappt durch Kanäle, auf denen das Thema aktuell läuft
mode: queued
fields:
  suchwort:
    description: Suchbegriff
    required: true
    example: Sport
sequence:
  - action: homeassistant.update_entity
    metadata: {}
    data:
      entity_id:
        - sensor.tvh_epg
  - variables:
       #this is mapping from the returned channel names from the script to the content_id for the media.player 
       mapping:
        PHOENIX HD: 1
        WDR HD Köln: 3
        TOGGO plus: 4
        ntv: 6
        SAT.1: 7
        NDR FS NDS HD: 8
        zdf_neo HD: 12
        SWR BW HD: 13
        VOX: 15
        ORF2E: 17
        arte HD: 18
        RTLup: 19
        DMAX HD: 20
        N24 Doku: 21
        TELE 5: 23
        SAT.1 Gold: 24
        SPORT1: 25
        WELT: 26
        Bibel TV HD: 27
        Channel21: 28
        Comedy Central: 29
        sixx: 30
        DF1 HD: 32
        rbb Berlin HD: 33
        Nick: 34
        BR Süd HD: 36
        MDR Sachsen HD: 37
        RTL: 38
        DELUXE MUSIC: 39
        ProSieben MAXX: 41
        KiKA HD: 42
        3sat HD: 46
        hr-fernsehen HD: 48
        SR Fernsehen HD: 52
        MTV: 53
        ARD alpha HD: 54
        DMF: 56
        Super RTL: 57
        RTLZWEI: 49
        ZDF HD: 55
        VOXup: 2
        NITRO: 10
        ProSieben: 47
        Das Erste HD: 5
        ZDFinfo HD: 9
        kabel eins Doku: 14
        tagesschau24 HD: 16
        ONE HD: 43
        DOKUSAT: 44
        kabel eins: 45
        Eurosport 1: 50
      eintraege: "{{ state_attr('sensor.tvh_epg', 'entries') }}"
      jetzt: "{{ now().timestamp() | int }}"
      sender_ids: >-
        {% set sw = suchwort | lower %} {% set ns = namespace(ids=[]) %} {% for
        e in eintraege %}
          {% set start = e.start %}
          {% set stop = e.stop %}
          {% if start <= jetzt <= stop %}
            {% set title = (e.title | string | lower) %}
            {% set subtitle = (e.subtitle | string | lower if 'subtitle' in e else '') %}
            {% set description = (e.description | string | lower if 'description' in e else '') %}
            {% if sw in title or sw in subtitle or sw in description %}
              {% set cid = mapping.get(e.channelName) %}
              {% if cid is not none %}
                {% set ns.ids = ns.ids + [cid] %}
              {% endif %}
            {% endif %}
          {% endif %}
        {% endfor %} {{ ns.ids }}
  - repeat:
      for_each: "{{ sender_ids }}"
      sequence:
        - target:
            entity_id: media_player.libreelec
          data:
            media_content_type: channel
            media_content_id: "{{ repeat.item }}"
          action: media_player.play_media
        - delay:
            #here you can set the pause between zapps
            seconds: 10

And last but not least the automation for triggering the whole thing via voice. Do not forget to enter your script-name:

alias: Voice - Wo läuft gerade (TV)
description: ""
triggers:
  - command: Wo läuft gerade {thema}
    trigger: conversation
actions:
  - metadata: {}
    data:
      suchwort: "{{ trigger.slots.thema }}"
    action: script.<name-of-your-script>
mode: single

Hope it works for you, I use it very often, it puts the ‘fun’ back into linear tv :slight_smile:

1 Like