Power::Outlet::MQTT - Control and query an outlet or relay via MQTT
Tasmota defaults
my $outlet = Power::Outlet::MQTT->new( host => "mqtt", name => "my_device", relay => "POWER1", );
or topic defaults
my $outlet = Power::Outlet::MQTT->new( host => "mqtt", publish_topic => "cmnd/my_device/POWER1", subscribe_topic => "stat/my_device/POWER1", );
or explicit definitions with no defaults
my $outlet = Power::Outlet::MQTT->new( host => "mqtt", publish_on => "cmnd/my_device/POWER1+ON", #plus sign delimited topic and message publish_off => "cmnd/my_device/POWER1+OFF", publish_switch => "cmnd/my_device/POWER1+TOGGLE", publish_query => "cmnd/my_device/POWER1+", subscribe_topic => "stat/my_device/POWER1", subscribe_value_on => 'ON' #or qr/\A(?:ON|1)\Z/i, subscribe_value_off => 'OFF, #or qr/\A(?:OFF|0)\Z/i, ); print $outlet->query, "\n"; print $outlet->on, "\n"; print $outlet->off, "\n";
Power::Outlet::MQTT is a package for controlling and querying an outlet or relay via MQTT
Examples:
$ mosquitto_pub -h mqtt -t "cmnd/my_device/POWER1" -m ON $ mosquitto_pub -h mqtt -t "cmnd/my_device/POWER1" -m OFF $ mosquitto_sub -h mqtt -t "stat/my_device/POWER1" -v
use Power::Outlet::MQTT; my $outlet = Power::Outlet::MQTT->new(host=>"mqtt", name=>"my_device"); print $outlet->on, "\n";
my $outlet = Power::Outlet->new(type=>"MQTT", host=>"mqtt"); my $outlet = Power::Outlet::MQTT->new(host=>"mqtt");
Sets and returns the host name of the MQTT broker.
Default: mqtt
Sets and returns the port number of the MQTT broker.
Default: 1883
Sets and returns a boolean property to use secure MQTT protocol or not.
Default: if port=8883 then 1 else 0
Sets and returns the device name of the MQTT topic.
Note: Only used when topics are autogenerated for devices that support the Tasmota MQTT topic conventions.
Sets and returns the relay of the device. Only used when name is used to define default publish and subscribe topics.
Default: POWER1
MQTT topic to publish to control the relay
Default: "cmnd/$device/$relay"
MQTT topic and message payload to publish to turn the relay on (plus sign delimited)
Default: "cmnd/$device/$relay+ON"
MQTT topic and message payload to turn the relay off (plus sign delimited)
Default: "cmnd/$device/$relay+OFF"
MQTT topic and message payload to toggle the relay (plus sign delimited)
Default: "cmnd/$device/$relay+TOGGLE"
MQTT topic and message payload to request the turn the current state of the relay (plus sign delimited)
Default: "cmnd/$device/$relay+"
MQTT topic which indicates the current state of the relay
Default: "stat/$device/$relay+"
MQTT message payload to indicate the current state of the relay as on
Default: "ON" or 1
MQTT message payload to indicate the current state of the relay as off
Default: "OFF" or 0
Sets and returns the authentication user for the MQTT broker.
Default: undef
Sets and returns the password used for authentication with the MQTT broker
Default: ""
Sets and returns a user friendly name of this device relay.
Sends an HTTP message to the device to query the current state
Sends a message to the device to Turn Power ON
Sends a message to the device to Turn Power OFF
Returns a cached connected Net::MQTT::Simple or Net::MQTT::Simple::SSL object.
Please log on GitHub
Michael R. Davis
Copyright (c) 2023 Michael R. Davis
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
To install Power::Outlet, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Power::Outlet
CPAN shell
perl -MCPAN -e shell install Power::Outlet
For more information on module installation, please visit the detailed CPAN module installation guide.