Discord server notifications

Hello, I want to have my HA send me a notification through discord whenever the humidity in room exceeds 65 %. This is what I wrote but it doesn’t seem to work.

sensor:
    platform: dht
    sensor: DHT22
    pin: 2
    name: senzor
    monitored_conditions:
        - temperature
        - humidity
        
    
lovelace:
    mode: yaml
homeassistant:
  customize:
    sensor.senzor_temperature:
      friendly_name: Teplota Uvnitř

    sensor.senzor_humidity:
      friendly_name: Vlhkost
      
template:
    - binary_sensor:
       - name: "Vlhkost je vysoká"
         state: "{{ state_attr('sensor.senzor', 'humidity') > 65 }}"
          


notify:
  - platform: discord
    name: discord
    token: ...
    
alert:
  vlhkost:
    name: Vlhkost je vysoká
    entity_id: binary_sensor.Vysoka_vlhkost
    repeat: 30
    skip_first: true
    message: "Vlhkost je {{ state_attr('sensor.senzor', 'humidity')}}"
    done_message: "Vlhkost je opět v normě"
    notifiers: notify.discord

I’m a begginer in this stuff do I’ll be glad for any advice.

Thank you.

The state of your binary sensor will be on, so you’ll need to specify that in alert like this state: "on".

I tried writing the state: “on”, but it still won’t send any notification.

Have you verified that notify.discord works?
Is there anything interesting in your logs?
skip_first: true means that you will have to wait 30 minutes (repeat: 30) before you get the first notification. Have you waited long enough?

Yeah, I think I waited long enough. How can I verify if notify.discord works? I’ll check the logs, but the configuration seems OK.

I would try Developer tools > Services and call the service manually.

Ok, so where could be a problem when the notify.discord doesn’t work?

Documentation: Setting up Discord bot
You seem to be missing the target in your notify.discord service call.

alert:
  vlhkost:
    name: Vlhkost je vysoká
    entity_id: binary_sensor.Vysoka_vlhkost
    state: "on"
    repeat: 30
    skip_first: true
    message: "Vlhkost je {{ state_attr('sensor.senzor', 'humidity')}}"
    done_message: "Vlhkost je opět v normě"
    notifiers: notify.discord
    data:
        target: whatever

I added

data:
   target: "[serverID]" 

but It still won’t work and there is nothing in the logs.

First make sure you can successfully trigger the notify.discord service from Developer tools (do not forget to provide the target).

I don’t think the square brackets should be inside double quotes. Wouldn’t that make it a string instead of a sequence of strings?

$ python3            
Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
+ >>> import yaml
+ >>> test = """
+ ... test1:
+ ...     - a
+ ...     - b
+ ... test2: [ c, d ]
+ ... test3: "[ e, f ]"
+ ... """
+ >>> yaml.safe_load(test)
{'test1': ['a', 'b'], 'test2': ['c', 'd'], 'test3': '[ e, f ]'}

with the square brackets I meant I didn’t write “server ID”, but the actuall ID of the server, I tried it with and without the double quotes, but neither works when I call the service

According to the documentation, the double quotes should be there, but they should be inside the square brackets (i.e. ["something"], not "[something]").
Have you gone through the process of setting up the bot on Discord? If you’ve done everything according to the documentation and it still does not work, I’m afraid I can’t help you anymore. I have no experience with Discord.

notifiers should be a list of services without the notify. prefix:

alert:
  vlhkost:
    name: Vlhkost je vysoká
    entity_id: binary_sensor.Vysoka_vlhkost
    state: "on"
    repeat: 30
    skip_first: true
    message: "Vlhkost je {{ state_attr('sensor.senzor', 'humidity')}}"
    done_message: "Vlhkost je opět v normě"
    notifiers:
      - discord
    data:
      target: ["whatever"]

If this does not work, you can move the target to a notify group

notify:
    - platform: group
      name: discord_whatever
      services:
          - service: discord
            data:
                target: ["whatever"]

and then use

alert:
  vlhkost:
    name: Vlhkost je vysoká
    entity_id: binary_sensor.Vysoka_vlhkost
    state: "on"
    repeat: 30
    skip_first: true
    message: "Vlhkost je {{ state_attr('sensor.senzor', 'humidity')}}"
    done_message: "Vlhkost je opět v normě"
    notifiers:
      - discord_whatever

I tried both, but neither is working. This is what I ended up with:

notify:
  - platform: discord
    name: discord
    token: ...
  - platform: group
    name: discord_home
    services: 
      - service: discord
        data:
          target: ["..."]
    
alert:
  vysoka_vlhkost:
    name: Vlhkost je vysoká
    done_message: Vlhkost je opět v normě
    entity_id: binary_sensor.vlhkost_je_vysoka
    state: 'on'
    repeat:
        - 15
        - 30
        - 60
        - 120
    skip_first: false
    message: "Vlhkost je {{ state_attr('sensor.senzor', 'humidity')}}"
    notifiers: 
        - discord_home

I tried turning it on via developer tools, and still it doesn’t work.

Wow, I completely failed to notice the incorrect entity_id.

What happens if you call notify.discord_home with a message from developer tools?
Try using persistent_notification notifier for testing your alert.

notify.discord_home works perfectly using the developer tools. I tried using persistent_notification as notifier, but the notification won’t appear. I even simplyfied the message to one word and moved the message to discord_home.
Could there be something I should install or enable in order to use the alert integration?

Ok, now I figured out the problem, I was setting the alert state to on and it won’t do anything, but when I set the binary_sensor state to on, it works. Now the only problem is with the state_attr function. It return None instead of the actual value of humidity.

state_attr seemed to work fine when I tested your alert. Check in developer tools if sensor.senzor has the humidity attribute.

Yeah, that was the problem, I changed it to states(sensor.senzor_humidity) and now it works perfectly.
Thank you for all the help.

is there a way to react of messages too?
Example: someone drop the message “door opener” > start automatisation