openhab / openhab-core

Core framework of openHAB
https://www.openhab.org/
Eclipse Public License 2.0
932 stars 429 forks source link

sendNotification not working inside DSL script in a rule #4142

Open Thinqer77 opened 8 months ago

Thinqer77 commented 8 months ago

I’m struggling to get sendNotification to work inside a DSL script in a rule built using the UI. I’ve created a very simple rule that fires every minute and have added two actions: the first executes a one line DSL script containing the sendNotification command and the second action uses the built-in UI ‘send a notification’ option. Both target the same email address (I’ve double checked this!) I get the message from the UI ‘send a notification’ but not the one from inside the DSL script.

Issue more fully outlined and explored here:

https://community.openhab.org/t/sendnotification-not-working-inside-dsl-script-in-a-rule/153811/11

Expected Behavior

I should receive the notification on my mobile device from the sendNotification inside the DSL rule.

Current Behavior

Here is the rule definition:

Screenshot 2024-02-11 012255

Inside the script is the following (email address changed but matched the one I sign in to myOpenHAB with):

sendNotification("myemail@address.com", "Hello world!")

When the rule triggers I get the notification from the UI defined send notification, but not the one from the Rule DSL script. If I remove the UI send notification item then I still don't get a notification.

I then further tested by setting up the following:

  1. Text file rule in a file called ‘TestNotification.rules’ with the following rule in it:

    rule "TestSendNotification"
    when
        Time cron "0 * * * * ? *"
    then
        logInfo("Alerts", "About to send my notification 2")
        sendNotification("myemail@address.com", "Hello World 2!")
        logInfo("Alerts", "My notification 2 sent!")
    end
  2. Rule defined in UI as above but with the UI notification set up as well as the DSL script notification as follows:

logInfo("Alerts", "About to send my notification")
sendNotification("myemail@address.com", "Hello world!")
logInfo("Alerts", "My notification sent!")

Here are the log entries I get with the above two rules set up: ` 2024-02-12 16:43:00.206 [DEBUG] [e.automation.internal.RuleEngineImpl] - The trigger '0' of rule 'TestNotification-1' is triggered. 2024-02-12 16:43:00.207 [TRACE] [ore.internal.scheduler.SchedulerImpl] - Scheduled a task to run in 59 seconds. 2024-02-12 16:43:00.208 [DEBUG] [ipt.internal.ScriptEngineManagerImpl] - Added ScriptEngine for language 'application/vnd.openhab.dsl.rule' with identifier: 331718c7-5873-48aa-9306-77fafe330dc3 2024-02-12 16:43:00.209 [TRACE] [internal.events.ThreadedEventHandler] - inspect event: org.osgi.service.event.Event [topic=openhab] {topic=openhab/rules/TestNotification-1/state, source=RuleEngineImpl, type=RuleStatusInfoEvent, payload={"status":"RUNNING","statusDetail":"NONE"}, timestamp=1707756180208} 2024-02-12 16:43:00.209 [DEBUG] [time.internal.engine.DSLScriptEngine] - Script uses context 'TestNotification-1'. 2024-02-12 16:43:00.210 [TRACE] [automation.internal.RuleEventFactory] - creating ruleEvent of type: RuleStatusInfoEvent 2024-02-12 16:43:00.210 [TRACE] [ab.core.internal.events.EventHandler] - Delegate event to subscriber (class org.openhab.core.io.monitor.internal.metrics.RuleMetric). 2024-02-12 16:43:00.211 [DEBUG] [.monitor.internal.metrics.RuleMetric] - Rule TestNotification-1 RUNNING - updating metric. 2024-02-12 16:43:00.213 [TRACE] [ab.core.internal.events.EventHandler] - Delegate event to subscriber (class org.openhab.core.io.monitor.internal.EventLogger). 2024-02-12 16:43:00.213 [TRACE] [ab.core.internal.events.EventHandler] - Delegate event to subscriber (class org.openhab.core.io.rest.sse.internal.listeners.SseEventSubscriber). 2024-02-12 16:43:00.214 [TRACE] [.openhab.core.io.rest.SseBroadcaster] - broadcast to potential 2 sinks 2024-02-12 16:43:00.214 [TRACE] [internal.events.ThreadedEventHandler] - wait for event 2024-02-12 16:43:00.222 [INFO ] [org.openhab.core.model.script.Alerts] - About to send my notification 2 2024-02-12 16:43:00.223 [DEBUG] [b.io.openhabcloud.NotificationAction] - sending notification 'Hello World 2!' to user myemail@address.com 2024-02-12 16:43:00.223 [DEBUG] [o.openhabcloud.internal.CloudService] - Sending message 'Hello World 2!' to user id myemail@address.com 2024-02-12 16:43:00.224 [DEBUG] [io.openhabcloud.internal.CloudClient] - No connection, notification is not sent 2024-02-12 16:43:00.224 [INFO ] [org.openhab.core.model.script.Alerts] - My notification 2 sent! 2024-02-12 16:43:00.225 [DEBUG] [e.automation.internal.RuleEngineImpl] - The rule 'TestNotification-1' is executed. 2024-02-12 16:43:00.225 [TRACE] [internal.events.ThreadedEventHandler] - inspect event: org.osgi.service.event.Event [topic=openhab] {topic=openhab/rules/TestNotification-1/state, source=RuleEngineImpl, type=RuleStatusInfoEvent, payload={"status":"IDLE","statusDetail":"NONE"}, timestamp=1707756180225}

2024-02-12 16:43:00.636 [DEBUG] [e.automation.internal.RuleEngineImpl] - The trigger '1' of rule 'afd0b389b6' is triggered. 2024-02-12 16:43:00.636 [TRACE] [ore.internal.scheduler.SchedulerImpl] - Scheduled a task to run in 59 seconds. 2024-02-12 16:43:00.636 [INFO ] [org.openhab.core.model.script.Alerts] - About to send my notification 2024-02-12 16:43:00.637 [TRACE] [internal.events.ThreadedEventHandler] - inspect event: org.osgi.service.event.Event [topic=openhab] {topic=openhab/rules/afd0b389b6/state, source=RuleEngineImpl, type=RuleStatusInfoEvent, payload={"status":"RUNNING","statusDetail":"NONE"}, timestamp=1707756180636} 2024-02-12 16:43:00.637 [DEBUG] [b.io.openhabcloud.NotificationAction] - sending notification 'Hello world!' to user myemail@address.com 2024-02-12 16:43:00.637 [DEBUG] [o.openhabcloud.internal.CloudService] - Sending message 'Hello world!' to user id myemail@address.com 2024-02-12 16:43:00.637 [TRACE] [automation.internal.RuleEventFactory] - creating ruleEvent of type: RuleStatusInfoEvent 2024-02-12 16:43:00.637 [DEBUG] [io.openhabcloud.internal.CloudClient] - No connection, notification is not sent 2024-02-12 16:43:00.637 [TRACE] [ab.core.internal.events.EventHandler] - Delegate event to subscriber (class org.openhab.core.io.monitor.internal.metrics.RuleMetric). 2024-02-12 16:43:00.638 [TRACE] [ab.core.internal.events.EventHandler] - Delegate event to subscriber (class org.openhab.core.io.monitor.internal.EventLogger). 2024-02-12 16:43:00.638 [DEBUG] [.monitor.internal.metrics.RuleMetric] - Rule afd0b389b6 RUNNING - updating metric. 2024-02-12 16:43:00.638 [INFO ] [org.openhab.core.model.script.Alerts] - My notification sent! 2024-02-12 16:43:00.638 [TRACE] [ab.core.internal.events.EventHandler] - Delegate event to subscriber (class org.openhab.core.io.rest.sse.internal.listeners.SseEventSubscriber). 2024-02-12 16:43:00.638 [TRACE] [internal.events.ThreadedEventHandler] - wait for event 2024-02-12 16:43:00.638 [TRACE] [.openhab.core.io.rest.SseBroadcaster] - broadcast to potential 2 sinks 2024-02-12 16:43:00.638 [DEBUG] [o.openhabcloud.internal.CloudService] - Sending message 'This is a notification from the UI action' to user id myemail@address.com 2024-02-12 16:43:00.639 [DEBUG] [e.automation.internal.RuleEngineImpl] - The rule 'afd0b389b6' is executed. 2024-02-12 16:43:00.641 [TRACE] [internal.events.ThreadedEventHandler] - inspect event: org.osgi.service.event.Eve `

The first block of the log shows the text file based rule running. You can see there are two lines in there, one saying ‘sending notification’ and one saying ‘Sending message’, which seems a bit odd. In any case, the next line says no connection (then my logInfo line gets inserted).

The second block is for the UI defined rule and it also has the two ‘sending Notification’ and ‘Sending message’ lines and the ‘no connection’ message (all of which I assume relate to the script based action, which is triggered first). Then a few lines later the UI defined message gets sent with a log entry of ‘Sending message’ but not followed with a ‘no connection’ error.

I don’t get any of the notifications from the two script based rules but I do get the one from the UI defined one. I need to use the script based one as I want to include variable text in the message depending on the state of certain items which isn’t possible using the UI defined notification.

I also created a Blockly based rule and it delivers the notification without issue.

Possible Solution

Steps to Reproduce (for Bugs)

See above for setup

Context

I'm trying to send a variable text string (based on the values of multiple items, including and excluding some depending on other items) and am trying to build it in Rule DSL scripting because the UI rule definition doesn't allow me to send variables or compose strings.

Your Environment

openhab-bot commented 8 months ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/sendnotification-not-working-inside-dsl-script-in-a-rule/153811/19

lolodomo commented 4 months ago

Version used: OpenHAB 3.3.0 Environment name and version (e.g. Chrome 111, Java 17, Node.js 18.15, ...): Docker container on a Synology (DSM 6.2.4-25556)

Very old version of openHAB run with an unrecommended JRE version (openHAB 3.x should be run with Java 11). And docker stuff so could be also something specific to your docker setup.

Are you using the public myopenhab cloud service or your own instance ?

I am using sendNotification in DSL rule everyday (RPI without docker and public cloud instance) and it works well.

Can you please try something very simple. No concurrent calls, just one basic DSL rule that calls sendNotification like you did in your example TestSendNotification but replacing the cron trigger by for example an item change trigger. If this case is still not working, the problem is then specific to your system setup.

I would like to understand if the problem is due to concurrent calls to sendNotification or if if a simple sendNotification is failing in your setup.

I agree with that your log "No connection, notification is not sent" is not normal and is clearly the reason of the notification not being sent.. It is as if your OH server is not connected to the cloud instance. Are you able to control openHAB from the cloud ?