An open-source .NET Core utility for automatically controlling device power via MQTT.
I recently authored a utility called SolarEdge.Monitor. This utility is able to monitor solar generation and, optionally, electrcity import/export statistics from Solar Edge HD Wave inverters (import/export statistics require an additional Energy Meter). Statistics gathered from the inverter and meter are published to an MQTT broker for consumption by a monitoring platform (I use Node-Red, InfluxDb & Grafana).
In adition to being able to monitor solar energy production, I was keen to be able to maximise the use of electricity generated by the solar panels (lowering overall export) and minimize the use of electricity from the grid (lowering overall import). To do this, I realised that I needed a means to turn on devices when we start exporting electricity and turn off devices when we start importing electricity. And thus, PowerFull was born.
PowerFull receives "real power" readings from MQTT (positive when exporting, negative when importing) and publishes messages back to MQTT when devices should be powered on or off. While designed to my use case - i.e. receiving messages from SolarEdge.Monitor and controlling Sonoff devices flashed with Tasmota - it is extremely flexible through the use of configurable topic and payload settings.
PowerFull is a .NET Core 3.0 application. As such it is run with the following command:
$> dotnet .\PowerFull.dll
When run like this you will see the following error:
One or more configuration errors occured:
The following fields must be specified when a Theme has not been specified: PowerStateRequestTopic, PowerStateResponseTopic, PowerStateResponseOnPayloadRegex, PowerStateResponseOffPayloadRegex, PowerOnRequestTopic, PowerOffRequestTopic
The Broker field is required.
The PowerReadingTopic field is required.
The PowerReadingPayloadValueRegex field is required.
The Devices field is required.
These configuration values can be provided via the command line or environment variables as outlined below. A 'env_file_defaults' file is supplied showing a common configuration of these values (which can also be used with the docker image - see the Docker section)
The service configuration defines the high-level behavior of the service and the devices the service is to control.
The core setting of the service, this provides the respective deviceId's of the devices to control. The order of the devices in this setting dictate the order in which they will be turned on or off when there is an surplus or deficit of electricity.
To prevent devices from rapidly turning on and off, and to ensure that a representative current power reading is ascertained (i.e. not inadvertently skewed by short spikes), power readings are averaged across a time period defined by this setting. Only after this time will a device be turned on if the average power reading is greater than the ThresholdToTurnOnDeviceWatts setting, or turned off if the average power reading is less than the ThresholdToTurnOffDeviceWatts setting.
The minimum power reading required to turn on a device. If the average power reading across the PowerChangeAfterMinutes duration is greater than or equal to this value, the next powered-off device from the Devices list will be turned on. There is no action if all devices in the devices list are already on.
The minimum power reading required to turn off a device. If the average power reading across the PowerChangeAfterMinutes duration is less than or equal to this value, the next powered-on device from the Devices list will be turned off. There is no action if all devices in the devices list are already off.
The messaging configuration provides the settings required to allow the service to communicate with an MQTT broker and receive current power readings
The address or hostname of the MQTT broker to use for communication
The port number used to connect to the MQTT broker
The client id supplied to the MQTT broker for this connection.
The username supplied to the MQTT broker for authentication
The password supplied to th eMQTT broker for authentication
The topic on which to receive messages containing the current power reading.
The regex used to extract a power reading from the payload of messages received from the PowerReadingTopic. The regex must make a single capture a group named 'RealPower' containing a string representation of a double value. If the group is not found or the value can't be parsed into a double then no power readings will be captured.
All the device configuration values support pattern substitition meaning they can be tailored to the specific device. For example, when the service is run with two devices named 'sonoff-battery' and 'sonoff-lights' with a PowerStateRequestTopic of 'cmnd/%deviceId%/POWER
', the power state request message will be published to the 'cmnd/sonoff-battery/POWER
' and 'cmnd/sonoff-lights/POWER
' topics respectively.
To save configuration work, Themes are provided to support common devices. At the moment only a single theme has been implemented - 'Tasmota' - to control sonoff devices flashed with the Tasmota firmware.
If you supply a Theme, no other device configuration values are required. Currently only a single theme for all devices is supported; this may change in future.
The topic on which to publish a message requesting the current power state of the device.
The payload of the message published to the PowerStateRequestTopic used to request the current power state of the device.
The topic on which to listen for messagings containing a response to the power state request message.
The regex used to match an 'On' state from the payload of the message published to the PowerStateResponseTopic. No special capture values are required from this regex, it must simply match the payload that represents on (and not match the payload that represents off).
The regex used to match an 'Off' state from the payload of the message published to the PowerStateResponseTopic.
The topic on which to publish a message requesting a device to turn on.
The payload of the message published to the PowerOnRequestTopic used to request that the device turn on.
The topic on which to publish a message requesting a device to turn on.
The payload of the message published to the PowerOffRequestTopic used to request that the device turn on.
A docker image has been provided on docker hub. It can be run with the following command:
docker run --env-file=[environment_variables_file] ibebbs/powerfull
where [environment_variables_file]
is a path to a file containing required environment variables as described above. Example defaults are provided in the env_file_defaults
file which can be used with the following command:
docker run --env-file=./env_file_defaults ibebbs/powerfull
If you encounter any issues using PowerFull or have any feature suggestions, please raise an issue on Github.