Alert when Washing Machine Finished

logo

This rule template will create a rule that monitors the power consumption of a washing machine (for example, with a smart plug that can report consumption), and send an alert command when it gets below a threshold, meaning it has finished.

Configuration

Parameter Type Description
Power Item Item Item that holds the power (in watts) of the washing machine. Can be a quantity type (Number:Power).
Threshold Decimal When the power measurement was at or above the threshold and crosses below it, trigger the alert.
Alert Item Item Item to send a command to when the measured power gets below the threshold. For instance, a Hue light advanced Alert channel.
Alert Command Text Command to send to the alert item (for an item linked to a Hue light alert channel, LSELECT will flash the light for a few seconds).

Changelog

Version 0.1

  • initial release

Resources

uid: ysc:washing_machine_alert
label: Alert when Washing Machine Finished
description: This will monitor the power consumption of a washing machine and send an alert command when it gets below a threshold, meaning it has finished.
configDescriptions:
 - name: powerItem
   type: TEXT
   context: item
   label: Power Item
   required: true
   description: Item that holds the power (in watts) of the washing machine. Can be a quantity type (Number:Power).
 - name: threshold
   type: DECIMAL
   label: Threshold
   required: true
   defaultValue: 2
   description: When the power measurement was at or above the threshold and crosses below it, trigger the alert.
 - name: alertItem
   type: TEXT
   context: item
   label: Alert Item
   required: true
   description: Item to send a command to when the measured power gets below the threshold. For instance, a Hue light advanced Alert channel.
 - name: alertCommand
   type: TEXT
   label: Alert Command
   required: true
   defaultValue: LSELECT
   description: Command to send to the alert item (for an item linked to a Hue light alert channel, LSELECT will flash the light for a few seconds).
triggers:
 - id: "1"
   configuration:
     itemName: "{{powerItem}}"
     state: ""
   type: core.ItemStateChangeTrigger
conditions: []
actions:
 - inputs: {}
   id: "2"
   configuration:
     type: application/javascript
     script: |
       var from = parseFloat(oldState.toString().split(' ')[0]);
       var to = parseFloat(newState.toString().split(' ')[0]);

       print(from + '>' + to);

       if (to < {{threshold}} && from >= {{threshold}}) {
         events.sendCommand('{{alertItem}}', '{{alertCommand}}');
       }
   type: script.ScriptAction
5 Likes

Nice, I have created a binding that handles this as well as keeping track of the consumption. I never got it merged since I didn’t have support for gui similar to the pidcontroller, but I might convert it/add support if it’s of interest. This seems like a nice approach which solves the problem in simple manner.

Regards S

What do I need to specify in the alert item to send a telegram message?
I have the telegram bot up and running

Probably a rule that triggers when the alert Item is commanded that uses the Telegram actions to send the message.

Found it bu adapting your script slightly. thanks!

Continuing the discussion from Alert when Washing Machine Finished:

Hi Mr. Schaus
I like that Add-on you made.

But i need a little bit Help.

Could you tell me, how i could send an Notification over Broadcast to all devices Openhab is installed?
If you could tell me that, it would be very usefully for me!

Kind regards.

Mordi

Once the rule is created from this template, edit the Action, and modify the script to:

  1. declare the NotificationAction:
var notifications = Java.type('org.openhab.io.openhabcloud.NotificationAction');

docs here: Actions | openHAB

  1. use it in your script:
       if (to < {{threshold}} && from >= {{threshold}}) {
         // replace or overwrite: events.sendCommand('{{alertItem}}', '{{alertCommand}}');
         notifications.sendBroadcastNotification('message','icon','info');
       }

replace the events.sendCommand (or add a new statement alongside it).

When using the template and trying to play it, I always receive an error log line:

[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'fff1193ead' failed: ReferenceError: "oldState" is not defined in <eval> at line number 1

Any ideas why oldState is not defined? It comes from the template right?

I’m a bit confused how to do this correctly… What is an alert item in this case? Do I need to create a dummy item that receives the alert message from this rule? And another rule listens for updates on this item?

Edit: I just realized, that I can just edit the script after creating it from the template. So I simply added the Telegram code to send a message manually.

If you want to be notified about more than one machine (second washing mashine, tumble dryer, …), instead of creating separate rules, all of them can be managed in one rule. At least for me that’s easier to manage. Here is an example how to be notified by a prepared Telegram bot (enter its item id!) about two different machines.

var from = parseFloat(oldState.toString().split(' ')[0]);
var to = parseFloat(newState.toString().split(' ')[0]);

print(from + '>' + to);

if (to < 5.0 && from >= 5.0) {
  var message = 'n/a';
  if (event.getItemName() == 'ShellyPlugWaschmaschine_Leistung')
    message = "🧺 Waschmaschine fertig!";
  else if (event.getItemName() == 'ShellyPlugTrockner_Leistung')
    message = "🌬️ Trockner fertig!";
  actions.get("telegram", "telegram:telegramBot:____________").sendTelegram(message);
}

I have no idea where oldState and newState are defined and where to find them in the documentation. I found a lot of similar things as well (previousState, itemName, …) but none of them were defined in this context. If there is a more direct way to access the item name instead of event.getItemName(), please let me know.

same for me :frowning:

Make sure your are using Nashorn JS add-on to run this and not JS Scripting. You may need to update the type under configuration if the rule to do so. This technique hasn’t been updated since it was changed to give JS Scripting application/javascript and Nashorn was changed to application/javascript;version=ECMAScript5.1.

The code written in JS Scripting would be something like this assuming:

var from = event.itemState.floatValue();
var to = event.prevState.floatValue();

console.info(from + '>' + to);

if(to < {{ threshold }} && from >= {{threshold}}) {
  items.{{alertItem}}.sendCommand('{{alert command}}');
}