MQTT \ Home Assistant - Resinchem/YouTube-to-HomeAssistant GitHub Wiki

MQTT and Home Assistant

MQTT

The following is a list of the current MQTT topics and valid payloads supported by the YouTube API and YouTube-to-HomeAssistant bridge. The values MQTT_TOPIC_SUB and MQTT_TOPIC_PUB will be substituted with the topics you define in the settings file. See the Credentials and Settings page for more information on defining your own MQTT topics.

Command Topics - issued TO the bridge to issue commands to YouTube

None - future versions may support this feature

State Topics - sent FROM the bridge to MQTT broker

TOPIC PAYLOAD NOTES
MQTT_TOPIC_PUB/subs numeric Current number of subscribers to the channel
MQTT_TOPIC_PUB/views numeric Returns the total number of views for all published videos of the channel
MQTT_TOPIC_PUB/videos numeric Returns the total number of publicly available videos on the channel
MQTT_TOPIC_PUB/door1/status 'API_OK' or 'API_FAILED' Status of the last API call.

Home Assistant

MQTT Discovery

Beginning with release v1.10, if Home Assistant MQTT Discovery is enabled in your settings (ha_discovery = true;), the discovery process will create all available sensors in Home Assistant without the need to manually define the sensors in YAML. When enabled and the bridge is rebooted, the follow entities will appear in Home Assistant:

Sensors and States

Entity_id State values
sensor.youtube_api_status API_OK or API_FAILED
sensor.youtube_subscribers Count of current subscribers
sensor.youtube_total_views Total channel views
sensor.youtube_videos Count of published videos

If you do not want all of these entities (for example, you only want to return subscribers), you can either disable the other entities in Home Assistant, or you can disable MQTT Discovery and define your own sensors via YAML.

If you wish to disable MQTT Discover after the entities have already been created, just set ha_discovery = false; in the Settings.h file and upload the sketch to the bridge again. Upon boot, the entities will be removed from Home Assistant. If you used these entities in any scripts, automations or Lovelace cards, you will need to remove or update those items as well.

Manually creating sensors

You do not need to create sensors for every possible MQTT topic. For example, if you are only interested in total subscriber count, you do not need a sensor for total views. See the sensors.yaml file in the \homeassistant folder of this repo for example sensors. But the basic structure for each sensor will be:

sensor:
  - platform: mqtt
    name: Total Subscriber Count
    state_topic: "stat/youtube/subs"  #update if you used a different topic in the Settings.h file

Limiting the number of notifications

If you are an established channel that receives a large number of views and/or new subscribers with each new video, you may not wish to receive a notification for each. You can set a limit on how often, or what thresholds will be used, to notify you within your Home Assistant notification. To do this, you need to create an input_number entity for each stat where you want to set this threshold. You will then update this number by your desired threshold amount as the last step in your automation. For example, if you only want to receive a notification for each 100 new views of you latest video (instead of every view), create an input_number.youtube_last_count_max. Then in your automation that handles updates to the last viewed count, add the following service:

    - service: input_number.set_value
      target:
        entity_id: input_number.youtube_last_count_max
      data:
        value: "{{ (((trigger.to_state.state) | int) + 99) }}"  # will notify for each 100 views

Simply add your threshold value - 1 to the value statement above. Now a notification will not be generated until the count exceeds this new max value. This can be done for last views, total views and/or subscriber counts. Note that due to the nature of the API, how often it refreshes and how quickly views and/or subscribers are generated, this won't necessarily always translate to exact values (e.g. 100, 200, 300). For example, if you have 198 views and the next update reports 202, the threshold value will be increased by 99 in this example, and the next threshold will now be 301, meaning the next notification will occur with view 302. If desired, you can always reset the input_number manually if desired, but this isn't a requirement. The primary goal is to limit your notifications to an acceptable level.

See the \homeassistant folder for more examples, including sample automations on how to use the bridge values.