Trying to make a virtual sensor with logging

Hello,
I am stuck with trying to make a sensor to log its changing data, in the Dev tools i can see my entity of a media player and under State attributes there are the following:
volume_level: 0.08
is_volume_muted: false
media_content_id: 58178139
media_content_type: video
media_position: 3324
media_position_updated_at: ‘2021-05-08T11:55:24.073509+00:00’
media_title: Football Focus

I am trying to strip out the media_title to place in a sensor and would be very grateful for some hints or suggestions

You can extract any of those attributes into a sensor. I just posted something for someone else that demonstrates that in this thread.

Thanks for your reply CO_4X4,
I have looked at your example and the template section in HA and believe I should be using the following:
Media_Title:
unique_id: Media_Title
friendly_name: “Media Title”
value_template: “{{ state_attr(‘media_player.sittingroom’, ‘media_title’) }}”

However I cannot see where to insert it, I have tried in a test dashboard and get errors, I have looked to try and make a new entity and do not see how to.

Sorry but any further assistance would be great.

You create the sensor as a template device as outlined here.

Thanks again,
I really wish to keep positive but just burning time and keep facing errors, looking at the link it appears all is for binary or numerical sensors, I tried adding as a text sensor which my yaml does not like.

Invalid config for [template]: [text_sensor] is an invalid option for [template]. Check: template->text_sensor. (See /config/configuration.yaml, line 13).
21:25:15 – (ERROR) config.py

It sounds like your YAML is incorrect. Please post your YAML code and use the preformatted text button here so it posts exactly like you have it.

Hi CO_4X4,
so frustrating, so much i need to do but this is taking over me at present.
firstly if I use the dev tools and insert {{ state_attr(‘media_player.sittingroomosmc’, ‘media_title’) }} I get the correct output of ‘ITV Nightscreen’

I have added a sensor.yaml file and also added ‘sensor: !include sensor.yaml’ in my /config/configuration.yaml whilst there is no text in sensor.yaml all is ok.

If I than add to the sensor.yaml

  • platform: template
    sensors:

strip media title

media_title:
friendly_name: 'Media Title'
value_template: "{{ state_attr('media_player.sittingroomosmc', 'media_title') }}"    

it gives a green tick but a restart gives the following error
2021-05-09 15:10:48 ERROR (MainThread) [homeassistant.components.hassio] Invalid config for [sensor.template]: expected dictionary for dictionary value @ data[‘sensors’]. Got None

extra keys not allowed @ data[‘friendly_name’]. Got ‘Media Title’

extra keys not allowed @ data[‘media_title’]. Got None

extra keys not allowed @ data[‘value_template’]. Got “{{ state_attr(‘media_player.sittingroomosmc’, ‘media_title’) }}”. (See ?, line ?).

I then remove the above from sensor.yaml and add this instead

  • platform: template
    sensors:

strip media title

media_title:
friendly_name: 'Media Title'
value_template: '{{ states.media_player.sittingroomosmc.attributes.media_title }}'

I then get this error
2021-05-09 15:15:20 ERROR (MainThread) [homeassistant.components.hassio] Invalid config for [sensor.template]: expected dictionary for dictionary value @ data[‘sensors’]. Got None

extra keys not allowed @ data[‘friendly_name’]. Got ‘Media Title’

extra keys not allowed @ data[‘media_title’]. Got None

extra keys not allowed @ data[‘value_template’]. Got ‘{{ states.media_player.sittingroomosmc.attributes.media_title }}’. (See ?, line ?).

Your indentions look incorrect. YAML is very indent needy.

Your post:

media_title:
friendly_name: 'Media Title'
value_template: "{{ state_attr('media_player.sittingroomosmc', 'media_title') }}"    

Corrected:

- platform: template
  sensors:
    media_title:
      friendly_name: 'Media Title'
      value_template: "{{ state_attr('media_player.sittingroomosmc', 'media_title') }}"    

Hi CO_4X4,
WOW, so close but yet so far, I would like to think I will not forget this learning curve.

Thank you very very much for your assistance and identifying my error, It works exactly as I need it to.

Thanks again and best regards

1 Like