uid: uhm:solar_switch label: Solar Switch description: This template creates a very simple rule that will allow you to control the state of a device based on the electricity that your house is spending. configDescriptions: - name: SolarSwitch type: TEXT context: item filterCriteria: - name: type value: Switch label: Group of Triggering Items description: Group whose members should trigger this rule. At least SolarSwitch should be here. required: true - name: GridPower type: TEXT context: item label: House Power Meter description: Item that will tell us the electricity that your house is spending. required: true - name: TheLoad type: TEXT context: item label: Load to control description: The load switch that this rule will control. required: true - name: SwitchOnThreshold type: TEXT context: item filterCriteria: - name: type value: number label: Switch On Threshold description: Switch that sets the on threshold. If the house spends less power the rule will switch on the load. required: true - name: SwitchOffThreshold type: TEXT context: item filterCriteria: - name: type value: number label: Switch Off Threshold description: Switch that sets the off threshold. If the house spends more power the rule will switch off the load. required: true - name: NotificationUser type: TEXT context: email label: Openhab User to send change notifications defaultValue: default description: Where to send notifications. Leave default if you do not want to receive notifications. required: false triggers: - id: "2" configuration: itemName: "{{SolarSwitch}}" type: core.ItemStateChangeTrigger conditions: [] actions: - inputs: {} id: "1" configuration: type: application/javascript;version=ECMAScript-2021 script: >- var notificUser = '{{NotificationUser}}'; var logger = log('oh_solar_sw.'+ruleUID); var solarswitch = items.getItem("{{SolarSwitch}}"); var laststate; var onthreshold = items.getItem("{{SwitchOnThreshold}}").numericState; var offthreshold = items.getItem("{{SwitchOffThreshold}}").numericState; var gridpower = items.getItem("{{GridPower}}").numericState; var theload = items.getItem("{{TheLoad}}"); //laststate posible values: //ON, OFF, SOLARSWITCHOFF var LASTSTATE_KEY = ruleUID+'_laststate'; function validEmail(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } if (cache.private.exists(LASTSTATE_KEY)) { laststate=cache.private.get(LASTSTATE_KEY); } else { laststate="SOLARSWITCHOFF"; cache.private.put(LASTSTATE_KEY,laststate); } if(solarswitch.state=='OFF') { if(validEmail(notificUser)) {actions.NotificationAction.sendNotification(notificUser, "Solar switch off")}; theload.sendCommandIfDifferent('OFF'); cache.private.put(LASTSTATE_KEY, 'SOLARSWITCHOFF'); } else { if(laststate=='SOLARSWITCHOFF') { if(validEmail(notificUser)) {actions.NotificationAction.sendNotification(notificUser, "Solar switch on")}; cache.private.put(LASTSTATE_KEY, theload.state); laststate=theload.state; } if(laststate=='ON') { if (theload.state=='OFF') { if(validEmail(notificUser)) {actions.NotificationAction.sendNotification(notificUser, "Solar Switch: Someone switched off manually the device, turning off the rule")}; cache.private.put(LASTSTATE_KEY, 'SOLARSWITCHOFF'); solarswitch.sendCommand('OFF'); } else { if (gridpower>offthreshold) { theload.sendCommandIfDifferent('OFF'); cache.private.put(LASTSTATE_KEY, 'OFF'); if(validEmail(notificUser)) {actions.NotificationAction.sendNotification(notificUser, "Solar Switch: Power spending is too high, switching off the device")}; } } } if(laststate=='OFF') { if (theload.state=='ON') { if(validEmail(notificUser)) {actions.NotificationAction.sendNotification(notificUser, "Solar Switch: Someone switched on manually the device, turning off the rule")}; cache.private.put(LASTSTATE_KEY, 'SOLARSWITCHOFF'); solarswitch.sendCommand('OFF'); } else { if (gridpower