Play audio file based on sensor value

I am using the following component to read a value from a file.

Every time the value changes I would like the following to happen.

If the value is below 100,000 do nothing
If the value is between 100,000 and 199,999 play audio file “alarm1.mp3”
If the value is between 200,000 and 299,000 play audio file “alarm2.mp3
If the value is above 300,000 paly audio file “alarm3.mp3”

Any help on how to script this behavior in the automations.yaml file would be most appreciated.

- alias: Play audio
  trigger:
    platform: state
    entity_id: sensor.CMD_LINE
  condition:
    condition: numeric_state
    entity_id: sensor.CMD_LINE
    above: 99999
  action:
    service: media_player.play_media
    entity_id: media_player.PLAYER
    data_template:
      media_content_type: 'audio/mp4'
      media_content_id: >
        {% set val = trigger.to_state.state|int %}
        {% if val < 200000 %}
          alarm1.mp3
        {% elif val < 300000 %}
          alarm2.mp3
        {% else %}
          alarm3.mp3
        {% endif %}

I may not have gotten all the details right since I really don’t use media_players, but hopefully this points you in the right direction.

EDIT: Updated per mf_social’s feedback.

service: media_player.play_media

And

media_content_type: 'audio/mp4'
1 Like

Which is it? The documentation says media_play in one place and play_media in another. When I checked my log I see that the registered service was media_play:

2018-11-01 10:07:24 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: service=media_play, domain=media_player>

play_media is like saying ‘play the following song’

media_play is like as in play / pause /stop

Ok. I just noticed both were registered:

2018-11-01 10:07:24 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: service=play_media, domain=media_player>
1 Like

Thanks for the support everyone!

Below is the script that I am using and it is working well with Google Home.
The only thing to note is that the IP adress of the audio files has to be public in order for Google Home to be able to play the audio file.

alias: Alarm
trigger:
entity_id: sensor.file_value
platform: state
condition:
condition: numeric_state
entity_id: sensor.file_value
above: 99999
action:
service: media_player.play_media
entity_id: media_player.kontor
data_template:
media_content_type: ‘audio/mp3’
media_content_id: >
{% set val = trigger.to_state.state|int %}
{% if val < 200000 %}
alarm1.mp3
{% elif val < 300000 %}
alarm2.mp3
{% else %}
alarm3.mp3
{% endif %}