NAME

Power::Outlet::MQTT - Control and query an outlet or relay via MQTT

SYNOPSIS

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";

DESCRIPTION

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

USAGE

use Power::Outlet::MQTT;
my $outlet = Power::Outlet::MQTT->new(host=>"mqtt", name=>"my_device");
print $outlet->on, "\n";

CONSTRUCTOR

new

my $outlet = Power::Outlet->new(type=>"MQTT", host=>"mqtt");
my $outlet = Power::Outlet::MQTT->new(host=>"mqtt");

PROPERTIES

host

Sets and returns the host name of the MQTT broker.

Default: mqtt

port

Sets and returns the port number of the MQTT broker.

Default: 1883

secure

Sets and returns a boolean property to use secure MQTT protocol or not.

Default: if port=8883 then 1 else 0

device

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.

relay

Sets and returns the relay of the device. Only used when name is used to define default publish and subscribe topics.

Default: POWER1

publish_topic

MQTT topic to publish to control the relay

Default: "cmnd/$device/$relay"

publish_on

MQTT topic and message payload to publish to turn the relay on (plus sign delimited)

Default: "cmnd/$device/$relay+ON"

publish_off

MQTT topic and message payload to turn the relay off (plus sign delimited)

Default: "cmnd/$device/$relay+OFF"

publish_switch

MQTT topic and message payload to toggle the relay (plus sign delimited)

Default: "cmnd/$device/$relay+TOGGLE"

publish_query

MQTT topic and message payload to request the turn the current state of the relay (plus sign delimited)

Default: "cmnd/$device/$relay+"

subscribe_topic

MQTT topic which indicates the current state of the relay

Default: "stat/$device/$relay+"

subscribe_value_on

MQTT message payload to indicate the current state of the relay as on

Default: "ON" or 1

subscribe_value_off

MQTT message payload to indicate the current state of the relay as off

Default: "OFF" or 0

user

Sets and returns the authentication user for the MQTT broker.

Default: undef

password

Sets and returns the password used for authentication with the MQTT broker

Default: ""

METHODS

name

Sets and returns a user friendly name of this device relay.

query

Sends an HTTP message to the device to query the current state

on

Sends a message to the device to Turn Power ON

off

Sends a message to the device to Turn Power OFF

switch

cycle

ACCESSORS

mqtt

Returns a cached connected Net::MQTT::Simple or Net::MQTT::Simple::SSL object.

BUGS

Please log on GitHub

AUTHOR

Michael R. Davis

COPYRIGHT

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.

SEE ALSO