Python Automation using Functions

What I want to do?

For every room on a Temperature Change there is allways needed to change if Window is open.

To be more effective in programming I now tried to create a python function for it.

But I allways got errors at sendCommand. Tried different kinds of Syntax, but none work.

Anybody an Idea?

The actual Version / Last try:

from core.rules import rule
from core.triggers import when
from core.log import logging, LOG_PREFIX

log=logging.getLogger("{}.110_FUNC_changeTemperature".format(LOG_PREFIX))

def ChangeTemperature(T_ITEM, NEW_T, WINDOW, T_TEMP):
    log.info("TEST-Function {0} {1} {2} {3}".format(T_ITEM, NEW_T, WINDOW, T_TEMP))
    if WINDOW != "NONE":
        if items[WINDOW] == OPEN:
            log.info("110  ...{0} reduziert auf {1}".format(T_TEMP, NEW_T))
            events.sendCommand(T_TEMP, NEW_T)
        else:
            log.info("110  ...{0} reduziert auf {1}".format(T_ITEM, NEW_T))
            events.sendCommand(T_ITEM, NEW_T)
    else:
        log.info("110  ...{0} reduziert auf {1}".format(T_ITEM, NEW_T))
        events.sendCommand(T_ITEM, NEW_T)


@rule("110 Status Heat-Control away", description="Changes Heating behaviour if Status is set to away")
#@when("Item Status_Home changed to 3")
@when("Time cron /30 * * ? * * *")
def HeatControlAway(event):
    HeatControlAway.log.info(u"110 Temperaturen abwesend --> Reduzieren")

    ChangeTemperature("EG_Flur_Thermo_Soll_Temp", items.Pref_EG_Flur_Soll_Temp.floatValue() - items.Pref_Away_Soll_Decrease.floatValue() , "NONE", "NONE")
    ChangeTemperature("EG_Bad_Thermo_Soll_Temp", items.Pref_EG_Bad_Soll_Temp.floatValue() - items.Pref_Away_Soll_Decrease.floatValue() , "EG_Bad_Window_Contact", "TMP_EG_Bad_Soll_Temp")

The Syntax for explanation:
ChangeTemperature(>Item to change temperature for<, >New Temperature< , >Item of Window to be checked<, >Item to store temperature if Window is open<)

It would be very difficult to help without knowing the definitions of the Items used in the rules (mainly, whether the states are QuantityTypes) and the errors you are receiving.

My guess is that oince you provide this info, my recommendation will be to review the supported arguments for sendCommand…

Items:

Number:Temperature	EG_Flur_Thermo_Soll_Temp 	"EG-Flur Heizkörper Soll [%.1f °C]" 	<temperature_hot> 	(gT3, LC13, gEGFlur, gSolltemperatur, gEGFlurChart, gEGSollTemp,	gHistoryMi, gHistoryCh)	{ channel="zwave:device:smartholhome:node9:thermostat_setpoint_heating" }
Number:Temperature	EG_Bad_Thermo_Soll_Temp 	"EG-Bad Heizkörper Soll [%.1f °C]" 	<temperature_hot> 	(gT8, LC13, gEGBad, gSolltemperatur, gEGBadChart, gEGSollTemp,		gHistoryMi, gHistoryCh)	{ channel="zwave:device:smartholhome:node10:thermostat_setpoint_heating" }

Number:Temperature	Pref_EG_Flur_Soll_Temp 		"EG-Flur Set Soll Temp." 		<heating_hot> 	(gPrefs, gEGFlur, 	gSetTemp,	gHistoryCh)
Number:Temperature	Pref_EG_Bad_Soll_Temp 		"EG-Bad Set Soll Temp." 		<heating_hot> 	(gPrefs, gEGBad, 	gSetTemp,	gHistoryCh)

Number:Temperature	Pref_Away_Soll_Decrease 	"Abwesend Set Absenkung" 		<heating_hot> 	(gePrefs,	 	gSetTemp,	gHistoryCh)

Number:Temperature TMP_EG_Bad_Soll_Temp          				(gHistoryCh)
Number:Temperature TMP_EG_Kueche_Soll_Temp     

Contact 		EG_Bad_Window_Contact 		"EG-Bad Fenster LF [MAP(WindowDoor.map):%s]" 		<window> 	(gD5, SENE1110, gEGBad, gWindow, gEGBadChart,		gHistoryMi, gHistoryCh)	{ channel="zwave:device:smartholhome:node22:sensor_door" }

I tried in Function different Versions:
events.sendCommand(T_ITEM, NEW_T) ==> Shouldnt work cause of no trigger event
ir.getItem(T_ITEM).sendCommand(NEW_T)
(ir.getItem(T_ITEM)).sendCommand(NEW_T)

I can not exactly say which error came for which try, I tried all variations I found in documentation under github and linked Jython Documentations.

AttributeError: ‘org.eclipse.smarthome.core.library.types.QuantityT’ object has no attribute ‘sendCommand’

AttributeError: ‘str’ object has no attribute ‘sendCommand’

TypeError: sendCommand(): 1st arg can’t be coerced to String, org.eclipse.smarthome.core.items.Item

AttributeError: ‘float’ object has no attribute ‘floatValue’

Just want to give the Item Name to the function and then in function want to send Command to this Item.

There also appears to be a bug in ChangeTemperature, since T_TEMP is not an Item and yet you have…

events.sendCommand(T_TEMP, NEW_T)

Once you fix that, make sure that both arguments are strings, which is the easiest.

Personally though, I’d leave everything as QuantityTypes and use the methods provided rather than converting to float. For example…

    ChangeTemperature("EG_Flur_Thermo_Soll_Temp", items.Pref_EG_Flur_Soll_Temp.subtract(items.Pref_Away_Soll_Decrease) , "NONE", "NONE")
1 Like

Got it working! For others needing an example for using Items in Functions to keep Rules readable:

from core.rules import rule
from core.triggers import when
from core.log import logging, LOG_PREFIX

log=logging.getLogger("{}.110_FUNC_changeTemperature".format(LOG_PREFIX))

def ChangeTemperature(T_ITEM, NEW_T, WINDOW, T_TEMP):
    log.info("TEST-Function {0} {1} {2} {3}".format(T_ITEM, NEW_T, WINDOW, T_TEMP))
    if WINDOW != "NONE":
        if items[WINDOW] == OPEN:
            log.info("110  ...{0} reduziert auf {1}".format(T_TEMP, NEW_T))
            events.sendCommand(ir(getItem(T_TEMP), NEW_T)
        else:
            log.info("110  ...{0} reduziert auf {1}".format(T_ITEM, NEW_T))
            events.sendCommand(ir(getItem(T_ITEM), NEW_T)
    else:
        log.info("110  ...{0} reduziert auf {1}".format(T_ITEM, NEW_T))
        events.sendCommand(ir(getItem(T_ITEM), NEW_T)
    return;

@rule("110 Status Heat-Control away", description="Changes Heating behaviour if Status is set to away")
@when("Item Status_Home changed to 3")
def HeatControlAway(event):
    HeatControlAway.log.info(u"110 Temperaturen abwesend --> Reduzieren")

    ChangeTemperature("EG_Flur_Thermo_Soll_Temp", items.Pref_EG_Flur_Soll_Temp.subtract(items.Pref_Away_Soll_Decrease) , "NONE", "NONE")
    ChangeTemperature("EG_Bad_Thermo_Soll_Temp", items.Pref_EG_Bad_Soll_Temp.subtract(items.Pref_Away_Soll_Decrease) , "EG_Bad_Window_Contact", "TMP_EG_Bad_Soll_Temp")

I just tested and it should work

Maybe an anonymized example of functions with Items usefull in Documentation on github too? If you think so: Let me know here and I will try to modify it there :slight_smile:

Edit: So now have to search where to save function that I can use it in all python - Rules :slight_smile: