I’m pretty sure I’m just missing something simple here, but I can’t see it.
Gradually migrating my existing DSL to Jython and I’m refactoring to move a function into a library. The function takes in a MQTT topic and a message and publishes them accordingly.
When the function is in with the other rules, it works fine, but extracting it to another file results in an error:
NameError: global name 'actions' is not defined
The complete library file: /lib/python/personal/utils.py
""" Send message to MQTT Broker """
def send_message_to_broker(topic, message):
""" Take in topic name & message and send to MQTT broker """
actions.get("mqtt", "mqtt:broker:tower").publishMQTT(str(topic), str(message))
The calling file: /jsr223/python/personal/messaging.py (top part only)
from core.rules import rule
from core.triggers import when
from core.actions import Voice
from java.time import ZonedDateTime
import personal.utils
reload(personal.utils)
from personal.utils import send_message_to_broker
@rule("Handle Item_Notify_Message",
description="MQTT Messaging for normal notifications",
tags=["System", "Messages"])
@when("Item Item_Notify_Message received update")
def notify_message(event):
""" Send normal message to MQTT Broker """
message = str(items["Item_Notify_Message"])
#Log it
notify_message.log.info("***** {}".format(message))
#Send it
send_message_to_broker("home/messages/pushover/normal", message)
send_message_to_broker("home/messages/slack/normal", message)
MQTT v2 is installed and running.
The Jython docs say that I don’t need an import (https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Actions.html?highlight=mqtt), but I’m guessing that is what is missing from the top of the utils.py.
I tried adding:
from core.actions import MQTT
but that just threw:
ImportError: cannot import name MQTT in <script>
Can someone point out the D’oh moment to me please?
[UPDATE]
So it doesn’t throw the error on compile/load, only on execute. If I add the core.actions import to the module, that error gets thrown on compile.