Script to say quick summary based on sensor number

So i’ve tried to get this to work, but i’m having issues with using IF statement in my data template.

So i want a summary of the air quality sensor i poll, so if it’s < 50 “air quality is good” < 100 “air quality is ok” ETC.
I’m not sure if i’m suppose to double {{ the sensor or if that just to pass it tts.
script below.

  air:
    alias: 'Air Quality'
    sequence:
      - service: tts.google_say
        data_template:
          entity_id: media_player.living_room
          message: >
            {% if {{states.sensor.waqi_sutton__beddington_lane_north_united_kingdom.state}}|int < 50 %}
            Air Quality is good.
            {% elif {{states.sensor.waqi_sutton__beddington_lane_north_united_kingdom.state}}|int < 100 %}
            Air Quality is moderate.
            {% elif {{states.sensor.waqi_sutton__beddington_lane_north_united_kingdom.state}}|int < 150 %}
            Air Quality is bad.
            {% elif {{states.sensor.waqi_sutton__beddington_lane_north_united_kingdom.state}}|int > 149 %}
            Your all going to die down here.
            {% else %}
            Not quite sure what happened here.
            {% endif %}

Any pointers ?

The could just be an automation instead of a script, as there is only one action - service: tts.google_say and no sequence.

Are you using Sonos as the media_player?

This should do it. You didn’t need the double brackets before and after the sensors.

    {% if states.sensor.waqi_sutton__beddington_lane_north_united_kingdom.state | int < 50 %}
      Air Quality is good.
    {% elif states.sensor.waqi_sutton__beddington_lane_north_united_kingdom.state | int < 100 %}
      Air Quality is moderate.
    {% elif states.sensor.waqi_sutton__beddington_lane_north_united_kingdom.state | int < 150 %}
      Air Quality is bad.
    {% elif states.sensor.waqi_sutton__beddington_lane_north_united_kingdom.state | int >= 150 %}
      Your all going to die down here.
    {% else %}
      Not quite sure what happened here.
    {% endif %}

Try it in the dev tools template tool - just copy it in :slight_smile:

I was trying it in the dev tools, but it still wasn’t working how i expected.

I think that i was getting confused as the example i was copying had them as double, and as they are being passed to TTS.

It could, would that be best ?
I’ve got one that says time to day greeting, then weather, drive to work time if mon-fri, i suspect i could move that to an automation.

I just click the button in HA to run that script at the moment (moving soon, so will make it do other things)

Yes, using my SONOS as a speaker, but if i every get round to it will setup my MyCroft AI, and see if i can use that to trigger and pipe out the audio

This worked perfectly, well no errors in HA, but can’t hear my SONOS from work :joy: