Help with some error - Invalid data for call_service at pos 1: required key not provided @ data['message']

i got this error message like below.

Error while executing automation automation.dust_airpurifier_onoff. Invalid data for call_service at pos 1: required key not provided @ data[‘message’]

But it works. and it seems that this is from some incorrect indemtation. is it correct? and plz let me know correct script.


        
- alias: Dust_airpurifier_ONOFF
  initial_state: True
  trigger:
    - platform: template
      value_template: "{% if states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20 %} true {% endif %}" 
    - platform: template
      value_template: "{% if states.sensor.pm10_1.state | float < 10 and states.sensor.pm10_2.state | float < 10 and states.sensor.pm25_1.state | float < 10 and states.sensor.pm25_2.state | float < 10 %} true {% endif %}" 
  action:
    - service: notify.telegram
      data_template:
        message: >
          {% if states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20 %}
            {% if is_state('binary_sensor.door_window_sensor_158d000248cb4b', 'on') %}
              ' high dust, though opened window. air purifier is not working.'
            {% elif is_state('switch.sonoff1_airpurifier1', 'off') and is_state('switch.sonoff2_airpurifier2', 'off') %}
              'Air purifier On'  
            {% endif %} 
          {% endif %} 
          {% if states.sensor.pm10_1.state | float < 10 and states.sensor.pm10_2.state | float < 10 and states.sensor.pm25_1.state | float < 10 and states.sensor.pm25_2.state | float < 10 and is_state('switch.sonoff1_airpurifier1', 'on') and is_state('switch.sonoff2_airpurifier2', 'on') %}       
            'Air purifier Off'
          {% endif %}
    - delay:
        seconds: 3
    - service_template: >
        {% if (states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20) and is_state('binary_sensor.door_window_sensor_158d000248cb4b', 'off') %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}
      entity_id: 
        - switch.sonoff1_airpurifier1
        - switch.sonoff2_airpurifier2

Look at the way your if statements are nested.

message: >
{ if A }
  { if B }
  { else C }
  { endif B }
{ endif A}
{ if D}
{endif D}
- delay

It is quite possible that no message is defined when if A and if D are not true.

i.e. what happens if states.sensor.pm10_1.state == 15?

You need to provide an else statement for this.

Thank you for your comment. It seems that i need to put the else statement in the case such as one of the sensors ==15. But, i dont want to notify any information in this situation. So, how ca i make it with more details?

Not just == 15 but when the values are between 10 and 20.

Send an “everything is just peachy” message?

No, probably not the best idea.

You need to rethink your automation’s logic.

I’d help but right now I’m struggling with an annoying HA problem of my own.

If you don’t make any progress update this post later and I’ll try to help.