Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Need help with @mqtt_trigger and multiple instantiations using the YAML configuration #749

Answered by andisa-8
andisa-8 asked this question in Q&A
Discussion options

I need some help.
I’m trying to change my pyscript so that it doesn’t use the click sensors that was removed in Zigbee2MQTT 2.0.0.
With "legacy_action_sensor: true" enabled in Zigbee2MQTT everything it’s working as intended.

So now I'm trying to use the @mqtt_trigger and I can get it to work for a single switch/lamp combination but I can’t figure out what the correct f-string should look like when using the YAML configuration to start several instantiations.

So,

@mqtt_trigger('zigbee2mqtt/Home Office Wall Switch', "payload_obj['action'] == 'right_press'")`
@mqtt_trigger('zigbee2mqtt/Home Office Wall Switch', "payload_obj['action'] == 'right_press_release'")
@mqtt_trigger('zigbee2mqtt/Home Office Wall Switch', "payload_obj['action'] == 'right_hold'")
@mqtt_trigger('zigbee2mqtt/Home Office Wall Switch', "payload_obj['action'] == 'right_hold_release'")
def handle_mqtt_message(**kwargs):
 log.info(f"zigbee2mqtt/Home Office Wall Switch right button. kwargs == {kwargs}")

works.

But how do I get

import asyncio
registered_triggers = []
def make_wall_switch_action(config):
 action_q = asyncio.Queue(10)
 # handles the switch action and adds it to the queue.
 @mqtt_trigger(f"'zigbee2mqtt/{config['switch_id']}', \"payload_obj['action'] == '{config['button']}_press'\"")
 @mqtt_trigger(f"'zigbee2mqtt/{config['switch_id']}', \"payload_obj['action'] == '{config['button']}_press_release'\"")
 @mqtt_trigger(f"'zigbee2mqtt/{config['switch_id']}', \"payload_obj['action'] == '{config['button']}_hold'\"")
 @mqtt_trigger(f"'zigbee2mqtt/{config['switch_id']}', \"payload_obj['action'] == '{config['button']}_hold_release'\"")
 def wall_switch_action(**kwargs):
 log.info(f"zigbee2mqtt/{config['switch_id']} {config['button']} button. kwargs == {kwargs}")
@time_trigger("startup")
def wall_switch_action_startup():
 log.info("wall switch action startup.")
 for app in pyscript.app_config:
 log.info(f"wall switch action startup. app = {app}")
 make_wall_switch_action(app)

to play ball?
My f-string must be wrong but I haven't been able to figure out the correct way.

configurations.yaml

pyscript: 
 apps:
 wall_switch_action:
 - switch_id: Home Office Wall Switch
 button: left
 - switch_id: Home Office Wall Switch
 button: right
You must be logged in to vote

And I’m an idiot....

When I reduced the scope of the function then I forgot to add the trigger

 # register it in the global scope so pyscript sees it.
 registered_triggers.append(wall_switch_action)

And the correct (or at least working) configuration is:

 @mqtt_trigger(f"zigbee2mqtt/{config['switch_id']}", f"payload_obj['action'] == '{config['button']}_press'")
 @mqtt_trigger(f"zigbee2mqtt/{config['switch_id']}", f"payload_obj['action'] == '{config['button']}_press_release'")
 @mqtt_trigger(f"zigbee2mqtt/{config['switch_id']}", f"payload_obj['action'] == '{config['button']}_hold'")
 @mqtt_trigger(f"zigbee2mqtt/{config['switch_id']}", f"payload_obj['action'] == '{config['bu...

Replies: 1 comment

Comment options

And I’m an idiot....

When I reduced the scope of the function then I forgot to add the trigger

 # register it in the global scope so pyscript sees it.
 registered_triggers.append(wall_switch_action)

And the correct (or at least working) configuration is:

 @mqtt_trigger(f"zigbee2mqtt/{config['switch_id']}", f"payload_obj['action'] == '{config['button']}_press'")
 @mqtt_trigger(f"zigbee2mqtt/{config['switch_id']}", f"payload_obj['action'] == '{config['button']}_press_release'")
 @mqtt_trigger(f"zigbee2mqtt/{config['switch_id']}", f"payload_obj['action'] == '{config['button']}_hold'")
 @mqtt_trigger(f"zigbee2mqtt/{config['switch_id']}", f"payload_obj['action'] == '{config['button']}_hold_release'")
You must be logged in to vote
0 replies
Answer selected by andisa-8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /