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: Group 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 give us the electricity that your house is spending. required: true - name: TheLoad type: TEXT context: item label: Load to control description: The load 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 threshold to 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 threshold to switch off the load required: true - name: NotificationUser type: TEXT context: email filterCriteria: - name: type value: number label: Openhab User to send change notifications defaultValue: nouser@mail.account description: Leave the default value if you dont want to receive an Openhab notification on changes 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 notificationsuser = '{{NotificationUser}}'; var logger = log('oh_solar_sw.'+ruleUID); var solarswitch = items.getItem("{{SolarSwitch}}"); var laststate; var onthreshold = items.getItem("{{SwitchOnThreshold}}").state; var offthreshold = items.getItem("{{SwitchOffThreshold}}").state; var gridpower = -items.getItem("{{GridPower}}").state; var theload = items.getItem("{{TheLoad}}"); //laststate posible values: //ON, JUSTON, OFF, JUSTOFF, SOLARSWITCHOFF var LASTSTATE_KEY = ruleUID+'_laststate'; if (cache.private.exists(LASTSTATE_KEY)) { laststate=cache.private.get(LASTSTATE_KEY); logger.info('This is the first if with latstate cache value = '+laststate); } else { laststate="SOLARSWITCHOFF"; cache.private.put(LASTSTATE_KEY,laststate); logger.info('This is the else with latstate cache value = '+laststate); } if(solarswitch.state=='OFF') { if(notificationsuser != 'nouser@mail.account') {actions.NotificationAction.sendNotification(notificationsuser, "Solar switch off")}; theload.sendCommandIfDifferent('OFF'); cache.private.put(LASTSTATE_KEY, 'SOLARSWITCHOFF'); } else { if(laststate=='SOLARSWITCHOFF') { logger.info('Someone just initiated Solar Switch'); if(notificationsuser != 'nouser@mail.account') {actions.NotificationAction.sendNotification(notificationsuser, "Solar switch off")}; cache.private.put(LASTSTATE_KEY, theload.state); } if(laststate=='ON') { if (theload.state=='OFF') { if(notificationsuser != 'nouser@mail.account') {actions.NotificationAction.sendNotification(notificationsuser, "Solar Switch: Someone switched off manually the device, deactivating the rule")}; cache.private.put(LASTSTATE_KEY, 'SOLARSWITCHOFF'); solarswitch.sendCommand('OFF'); } else { if (gridpoweronthreshold) { theload.sendCommandIfDifferent('ON'); cache.private.put(LASTSTATE_KEY, 'JUSTON'); if(notificationsuser != 'nouser@mail.account') {actions.NotificationAction.sendNotification(notificationsuser, "Solar Switch: solar power is higher than the on threshold, switching on the device")}; } } } if(laststate=='JUSTON') { //put here the action if juston cache.private.put(LASTSTATE_KEY, 'ON'); } if(laststate=='JUSTOFF') { //put here the action if justoff cache.private.put(LASTSTATE_KEY, 'OFF'); } } type: script.ScriptAction